Oz and Mozart Users Mailing List

Re: Date/Time - Functions


From: Adriano Volpones (adriano.volpones@arscomputandi.com)
Date: Fri Feb 28 2003 - 10:28:05 CET


Klaus,

attached you can find a very basic direct translation of the Date and Time
classes from Smalltalk/Express (a free and simple Smalltalk environment).

They can be extended with all the operations in the Smalltalk classes.

Adriano

%//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%// Created: av 12/19/02 17:43:57
%//
%//
%// declare D D1
%// D={New SDate today}
%// D1={New SDate fromString("28/12/1957")}
%//
%// {Browse {D year($)}}
%// {Browse {D monthIndex($)}}
%// {Browse {D firstDayInMonth($)}}
%// {Browse {D toStringDMY($)}}
%// {Browse {D1 toStringDMY($)}}
%// {Browse {{D addDays(30 $)} toStringDMY($)} }
%// {Browse {{D addDays(~30 $)} toStringDMY($)} }
%//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
class SDate from BaseObject
   feat
      d %// days aggiustati per baseDay
      baseDay: 30300 %// inizio
      className: 'SDate'

%//-----------------------------------------------------------------
%// init
%//-----------------------------------------------------------------
   meth today()
      T={OS.localTime}
      D=T.mDay
      M=T.mon+1
      Y=T.year+1900
   in
      {self fromDMY(day:D month:M year:Y)}
   end

   meth fromDMY(day:D month:M year:Y)
      Cum=0#31#59#90#120#151#181#212#243#273#304#334
      DToM=Cum.M
      DToM1 DToM2
   in
      if M > 2 andthen {self leapYear(Y $)}==1 then DToM1=DToM+1
      else DToM1=DToM end
      DToM2=DToM1+D
      {self fromDY(day:DToM2 year:Y)}
   end

   meth fromDY(day:D year:Y)
      Diff=(Y - 1901) * 365
      LeapY={self leapYearsTo(Y $)}
   in
      {self day(Diff + LeapY + D - 1)}
   end

   meth fromDays(Days)
      {self day(Days)}
   end

   meth fromString(Str) %// DD/MM/YYYY
      D|M|Y|_={String.tokens Str &/}
   in
      {self fromDMY(day:{StringToInt D}
                    month:{StringToInt M}
                    year:{StringToInt Y})}
   end

%//------------------------------------+----------------------------
%// attributes
%//------------------------------------+----------------------------
   %// giorno dal 1/1/1901
   meth day(V)
      if {IsDet V} then self.d=V - self.baseDay
      else V=self.d + self.baseDay
      end
   end

%//-----------------------------------------------------------------
%// operations
%//-----------------------------------------------------------------
   %// anno
   meth year($)
      NumOfDays={self day($)} + (1900*365+460)
      Temp0=400 * 365 + 97
      N400=NumOfDays div Temp0 * 400
      R400={Min (NumOfDays mod Temp0) (Temp0-2)}
      Temp1=100*365+24
      N100=(R400 div Temp1) * 100
      R100=R400 mod Temp1
      N4=(R100 div 1461) * 4
      R4={Min (R100 mod 1461) 1459}
      N1=R4 div 365
   in
      N400+N100+N4+N1+1
   end
   %// mese
   meth monthIndex(Index)
      DaysToMonth=[0 31 59 90 120 151 181 212 243 273 304 334]
      DayNum={self dayOfYear($)}
      DayNum1 UL
   in
      if DayNum > 59
      then DayNum1=DayNum-{self leapYear({self year($)} $)}
      else DayNum1=DayNum
      end
      UL={List.takeWhile DaysToMonth fun{$ X} X<DayNum1 end}
      Index={Length UL}
   end
   %// giorno dell'anno da 1 a 365
   meth dayOfYear(DY)
      Y={self year($)}
   in
      DY={self day($)} -
         (((Y-1901) * 365) + {self leapYearsTo(Y $)}) + 1
   end
   %// primo giorno nell'anno del mese corrente
   meth firstDayInMonth(DY)
      Index={self monthIndex($)}
      Cum=0#31#59#90#120#151#181#212#243#273#304#334
      DToM=Cum.Index
   in
      if Index > 2
      then DY=DToM + {self leapYear({self year($)} $)}
      else DY=DToM
      end
   end
   %// giorno del mese da 1 a 31
   meth dayOfMonth(DM)
      DM={self dayOfYear($)} - {self firstDayInMonth($)} + 1
   end
   %// Y e' bisestile 0=no 1=si
   meth leapYear(Y $)
      if (Y mod 4) == 0 andthen
      (Y mod 100) > 0 orelse
      (Y mod 400) == 0then 1 else 0
      end
   end
   %// N anni bisestili
   meth leapYearsTo(Y $)
      Prior=Y-1 in
      (Prior div 4) + (Prior div 400) -
      (Prior div 100) - 460
   end
   %// N giorno in settimana 1=lun
   meth dayIndex($)
      (({self day($)} + 1) mod 7) + 1
   end
   %// risponde un'altra data
   meth addDays(N NewDate)
      NewDate={New SDate fromDays({self day($)} + N)}
   end

%//-----------------------------------------------------------------
%// conversions
%//-----------------------------------------------------------------
   %// default
   meth toString($)
      {self toStringDMY($)}
   end
   %// 10/02/2002
   meth toStringDMY($ Sep<="/" )
      D={Format2 {self dayOfMonth($)}}
      M={Format2 {self monthIndex($)}}
      Y={Int.toString {self year($)}}
   in
      case Sep of nil then D#M#Y
      else D#Sep#M#Sep#Y
      end
   end

%//-----------------------------------------------------------------
end

%//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%// Created: av 12/21/02 18:23:13
%//
%// declare T T1
%// T={New STime now}
%// T1={New STime fromString("10:12:33")}
%//
%// {Browse {T toString($)}}
%// {Browse {T1 toString($)}}
%// {Browse {{T addSeconds(3600 $)} toString($)}}
%// {Browse {{T addSeconds(~3600 $)} toString($)}}
%//
%//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
class STime from BaseObject
   feat
      s %// secondi da mezzanotte
      className: 'SDate'
%//------------------------------------+----------------------------
%// init
%//------------------------------------+----------------------------
   meth now
      T={OS.localTime}
      H=T.hour
      M=T.min
      S=T.sec
   in
      {self fromHMS(hour:H min:M sec:S)}
   end

   meth fromHMS(hour:H min:M sec:S)
      TS=(H*60+M)*60+S
   in
      {self fromSeconds(TS)}
   end

   meth fromSeconds(TS)
      {self secMD(TS)}
   end

   meth fromString(Str) %// "10:33:57"
      H|M|S|_={String.tokens Str &:}
   in
      {self fromHMS(hour:{StringToInt H}
                    min:{StringToInt M}
                    sec:{StringToInt S})}
   end

%//------------------------------------+----------------------------
%// attributes
%//------------------------------------+----------------------------
   %// Seconds from MidNight
   meth secMD(V)
      if {IsDet V} then self.s=V
      else V=self.s
      end
   end

%//------------------------------------+----------------------------
%// operations
%//------------------------------------+----------------------------
   meth hours($)
      {self toSeconds($)} div 3600
   end

   meth minutes($)
      ({self toSeconds($)} mod 3600) div 60
   end

   meth seconds($)
      {self toSeconds($)} mod 60
   end

   meth addSeconds(N NewTime)
      NewTime={New STime fromSeconds({self secMD($)} + N)}
   end

%//-----------------------------------------------------------------
%// conversions
%//-----------------------------------------------------------------
   meth toSeconds($)
      {self secMD($)}
   end

   %// default
   meth toString($)
      {self toStringHMS($)}
   end
   %// 10:12:33
   meth toStringHMS($ Sep<=":" )
      H={Format2 {self hours($)}}
      M={Format2 {self minutes($)}}
      S={Format2 {self seconds($)}}
   in
      case Sep of nil then H#M#S
      else H#Sep#M#Sep#S
      end
   end

%//------------------------------------+----------------------------
end

-
Please send submissions to users@mozart-oz.org
and administriva mail to users-request@mozart-oz.org.
The Mozart Oz web site is at http://www.mozart-oz.org/.
Please send bug reports to bugs@mozart-oz.org.



This archive was generated by hypermail 2b29.