AbsTime.st
changeset 241 6f30be88e314
parent 213 3b56a17534fd
child 242 0190f298e56c
equal deleted inserted replaced
240:f5ff68fffb92 241:6f30be88e314
     8  be provided or otherwise made available to, or used by, any
     8  be provided or otherwise made available to, or used by, any
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
    12 
    13 Magnitude subclass:#AbsoluteTime
    13 'From Smalltalk/X, Version:2.10.4 on 8-feb-1995 at 12:46:45 pm'!
    14        instanceVariableNames:'secondsLow secondsHi'
    14 
    15        classVariableNames:''
    15 AbstractTime subclass:#AbsoluteTime
    16        poolDictionaries:''
    16 	 instanceVariableNames:'secondsLow secondsHi'
    17        category:'Magnitude-General'
    17 	 classVariableNames:''
       
    18 	 poolDictionaries:''
       
    19 	 category:'Magnitude-General'
    18 !
    20 !
    19 
    21 
    20 AbsoluteTime comment:'
    22 AbsoluteTime comment:'
    21 COPYRIGHT (c) 1989 by Claus Gittinger
    23 COPYRIGHT (c) 1989 by Claus Gittinger
    22 	      All Rights Reserved
    24 	      All Rights Reserved
    23 
    25 
    24 $Header: /cvs/stx/stx/libbasic/Attic/AbsTime.st,v 1.7 1994-11-28 20:32:07 claus Exp $
    26 $Header: /cvs/stx/stx/libbasic/Attic/AbsTime.st,v 1.8 1995-02-08 03:10:47 claus Exp $
    25 '!
    27 '!
    26 
    28 
    27 !AbsoluteTime class methodsFor:'documentation'!
    29 !AbsoluteTime class methodsFor:'documentation'!
    28 
    30 
    29 copyright
    31 copyright
    40 "
    42 "
    41 !
    43 !
    42 
    44 
    43 version
    45 version
    44 "
    46 "
    45 $Header: /cvs/stx/stx/libbasic/Attic/AbsTime.st,v 1.7 1994-11-28 20:32:07 claus Exp $
    47 $Header: /cvs/stx/stx/libbasic/Attic/AbsTime.st,v 1.8 1995-02-08 03:10:47 claus Exp $
    46 "
    48 "
    47 !
    49 !
    48 
    50 
    49 documentation
    51 documentation
    50 "
    52 "
    51     This class represents time values in seconds from 1st. Jan 1970, as
    53     This class represents time values in seconds from 1st. Jan 1970, as
    52     used in the Unix operating system. Its implementation is not the same
    54     used in the Unix operating system. Its implementation is not the same
    53     as in ST-80 (which represents Time as seconds from 1. Jan 1901.
    55     as in ST-80 (which represents Time as seconds from 1. Jan 1901.
    54     Actually, the implementation does not even know which time/date the
    56 
    55     OperatingSystem bases its time upon - it is simply keeping the value(s)
    57     Actually, the implementation does not depend or even know which time/date 
    56     as return from the OS when asked for the time.
    58     the OperatingSystem bases its time upon - it is simply keeping the value(s)
       
    59     as return from the OS.
    57     For conversion, these values are given back to the OS, which will know
    60     For conversion, these values are given back to the OS, which will know
    58     how to convert these times.
    61     how to convert these times.
    59     This has the advantage, that time-stamps on files (such as last-access-
    62     This has the advantage, that time-stamps on files (such as last-access-
    60     time or last-modifiaction-time) can be handled transparent to other
    63     time or last-modifiaction-time) can be handled transparent to other
    61     times (especially comparison).
    64     times (especially comparison).
    62 
    65 
    63     Since unix-times are 32 bit which does not fit into a SmallInteger, 
    66     Since unix-times are 32 bit which does not fit into a SmallInteger, 
    64     we keep low and hi 16bits of the time separately (it could have been 
    67     we keep low and hi 16bits of the time separately (it could have been 
    65     implemented using LargeIntegers though).
    68     implemented using LargeIntegers though).
    66 
    69 
    67     This class is typically abstract (it does not have to be, though).
    70     This class should not be confused with Time (which only represents the
       
    71     time within one day). Time instances cannot be used to compare times across
       
    72     midnight; instances of AbsoluteTime can.
       
    73 
    68     See Time for more details.
    74     See Time for more details.
    69 "
    75 "
    70 ! !
    76 ! !
    71 
    77 
    72 !AbsoluteTime class methodsFor:'instance creation'!
    78 !AbsoluteTime class methodsFor:'instance creation'!
    73 
    79 
    74 secondClock
    80 day:d month:m year:y hour:h minutes:min seconds:s
    75     "return seconds of now - for GNU-ST compatibility"
    81     "return an instance of the receiver"
    76 
    82 
    77     ^ OperatingSystem getTime
    83     ^ self fromOSTime:(OperatingSystem 
    78 !
    84                         computeTimePartsFromYear:y month:m day:d 
    79 
    85                                             hour:h minute:min seconds:s)
    80 millisecondClockValue
    86 
    81     "return the millisecond clock - since this one overruns
    87     "
    82      regularly, use only for short timing deltas."
    88      AbsoluteTime day:2 month:1 year:1991 hour:12 minutes:30 seconds:0 
    83 
    89      AbsoluteTime day:8 month:1 year:1995 hour:0 minutes:43 seconds:48 
    84     ^ OperatingSystem getMillisecondTime.
    90     "
    85 !
    91 ! !
    86 
    92 
    87 fromUnixTimeLow:low and:hi
    93 !AbsoluteTime methodsFor:'private'!
    88     "return an instance of Time, given the unix time.
    94 
    89      Internal interface - not for public use."
    95 secondsLow
    90 
    96     ^ secondsLow
    91     ^ self basicNew setSecondsLow:low and:hi
    97 !
    92 !
    98 
    93 
    99 setSecondsLow:secsLow and:secsHi
    94 dateAndTimeNow
   100     secondsHi := secsHi.
    95     "return an array filled with date and time"
   101     secondsLow := secsLow
    96 
   102 !
    97     ^ Array with:(Date today) with:(Time now)
   103 
    98 ! !
   104 fromOSTimeLow:secsLow and:secsHi
    99 
   105     secondsHi := secsHi.
   100 !AbsoluteTime class methodsFor:'timing evaluations'!
   106     secondsLow := secsLow
   101 
   107 !
   102 secondsToRun:aBlock
   108 
   103     "evaluate the argument, aBlock; return the number of seconds it took"
   109 secondsHi
   104 
   110     ^ secondsHi
   105     |startTime endTime|
   111 !
   106 
   112 
   107     startTime := self now.
   113 setSeconds:secs
   108     aBlock value.
   114     secondsHi := secs // 16r10000.
   109     endTime := self now.
   115     secondsLow := secs \\ 16r10000
   110     ^ endTime - startTime
   116 !
   111 !
   117 
   112 
   118 getSeconds
   113 millisecondsToRun:aBlock
   119     ^ (secondsHi * 16r10000) + secondsLow
   114     "evaluate the argument, aBlock; return the number of milliseconds it took"
   120 !
   115 
   121 
   116     |startTime endTime|
   122 fromOSTime:secs
   117 
   123     secondsHi := secs // 16r10000.
   118     startTime := self millisecondClockValue.
   124     secondsLow := secs \\ 16r10000
   119     aBlock value.
       
   120     endTime := self millisecondClockValue.
       
   121     ^ endTime - startTime
       
   122 ! !
   125 ! !
   123 
   126 
   124 !AbsoluteTime methodsFor:'accessing'!
   127 !AbsoluteTime methodsFor:'accessing'!
   125 
   128 
   126 hourInDay
   129 hourInDay
   127     "return the hour-part"
   130     "return the hours (0..23)"
   128 
   131 
   129     |hr|
   132     ^ self hours
   130 
   133 
   131     OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
   134     "
   132 	:hours :minutes :secs |
   135      AbsoluteTime now hours 
   133 
   136     "
   134 	hr := hours
   137 
   135     ].
       
   136     ^ hr
       
   137 !
   138 !
   138 
   139 
   139 minuteInDay
   140 minuteInDay
   140     "return the minute-part"
   141     "return the minute-part"
   141 
   142 
   142     |m|
   143     |m|
   143 
   144 
   144     OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
   145     OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
   145 	:hours :minutes :secs |
   146         :hours :minutes :secs |
   146 
   147 
   147 	m := minutes
   148         m := minutes
   148     ].
   149     ].
   149     ^ m
   150     ^ m
       
   151 
       
   152     "
       
   153      AbsoluteTime now minuteInDay 
       
   154     "
       
   155 
   150 !
   156 !
   151 
   157 
   152 secondInDay
   158 secondInDay
   153     "return the second-part"
   159     "return the second-part"
   154 
   160 
   155     |s|
   161     |s|
   156 
   162 
   157     OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
   163     OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
   158 	:hours :minutes :secs |
   164         :hours :minutes :secs |
   159 
   165 
   160 	s := secs
   166         s := secs
   161     ].
   167     ].
   162     ^ s
   168     ^ s
       
   169 
       
   170     "
       
   171      AbsoluteTime now secondInDay 
       
   172     "
       
   173 
   163 !
   174 !
   164 
   175 
   165 day
   176 day
   166     "return the day-in-month of the receiver (1..31).
   177     "return the day-in-month of the receiver (1..31).
   167      Obsolete; use instances of Date for this."
   178      Obsolete; use instances of Date for this."
   168 
   179 
       
   180     |d|
       
   181 
   169     OperatingSystem computeDatePartsOf:secondsLow and:secondsHi
   182     OperatingSystem computeDatePartsOf:secondsLow and:secondsHi
   170 				   for:[:year :month :day |
   183                                    for:[:year :month :day |
   171 	^ day
   184         d := day
   172     ]
   185     ].
   173 
   186     ^ d
   174     "
   187 
   175      Time now day
   188     "
       
   189      AbsoluteTime now day 
   176     "
   190     "
   177 !
   191 !
   178 
   192 
   179 month
   193 month
   180     "return the month of the receiver (1..12).
   194     "return the month of the receiver (1..12).
   181      Obsolete; use instances of Date for this."
   195      Obsolete; use instances of Date for this."
   182 
   196 
       
   197     |m|
       
   198 
   183     OperatingSystem computeDatePartsOf:secondsLow and:secondsHi
   199     OperatingSystem computeDatePartsOf:secondsLow and:secondsHi
   184 				   for:[:year :month :day |
   200                                    for:[:year :month :day |
   185 	^ month
   201         m := month
   186     ]
   202     ].
   187 
   203     ^ m
   188     "
   204 
   189      Time now month
   205     "
       
   206      AbsoluteTime now month
   190     "
   207     "
   191 !
   208 !
   192 
   209 
   193 year
   210 year
   194     "return the year of the receiver i.e. 1992.
   211     "return the year of the receiver i.e. 1992.
   195      Obsolete; use instances of Date for this."
   212      Obsolete; use instances of Date for this."
   196 
   213 
       
   214     |y|
       
   215 
   197     OperatingSystem computeDatePartsOf:secondsLow and:secondsHi
   216     OperatingSystem computeDatePartsOf:secondsLow and:secondsHi
   198 				   for:[:year :month :day |
   217                                    for:[:year :month :day |
   199 	^ year
   218         y := year
   200     ]
   219     ].
   201 
   220     ^ y
   202     "
   221 
   203      Time now year
   222     "
   204     "
   223      AbsoluteTime now year
       
   224     "
       
   225 !
       
   226 
       
   227 hours
       
   228     "return the hours (0..23)"
       
   229 
       
   230     |hr|
       
   231 
       
   232     OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
       
   233         :hours :minutes :secs |
       
   234 
       
   235         hr := hours
       
   236     ].
       
   237     ^ hr
       
   238 
       
   239     "
       
   240      AbsoluteTime now hours 
       
   241     "
       
   242 
       
   243 !
       
   244 
       
   245 minutes
       
   246     "return the minutes (0..59)"
       
   247 
       
   248     |m|
       
   249 
       
   250     OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
       
   251         :hours :minutes :secs |
       
   252 
       
   253         m := minutes
       
   254     ].
       
   255     ^ m
       
   256 
       
   257     "
       
   258      AbsoluteTime now minutes 
       
   259     "
       
   260 
       
   261 !
       
   262 
       
   263 seconds
       
   264     "return the seconds (0..59)"
       
   265 
       
   266     |s|
       
   267 
       
   268     OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
       
   269         :hours :minutes :secs |
       
   270 
       
   271         s := secs
       
   272     ].
       
   273     ^ s
       
   274 
       
   275     "
       
   276      AbsoluteTime now seconds 
       
   277     "
       
   278 
   205 ! !
   279 ! !
   206 
   280 
   207 !AbsoluteTime methodsFor:'comparing'!
   281 !AbsoluteTime methodsFor:'comparing'!
   208 
   282 
   209 > aTime
   283 > aTime
   227     "return an integer useful for hashing on times"
   301     "return an integer useful for hashing on times"
   228 
   302 
   229     ^ (secondsLow bitShift:16) bitOr:secondsLow
   303     ^ (secondsLow bitShift:16) bitOr:secondsLow
   230 ! !
   304 ! !
   231 
   305 
   232 !AbsoluteTime methodsFor:'arithmetic'!
       
   233 
       
   234 - aTime
       
   235     "return delta in seconds between 2 times/dates."
       
   236 
       
   237     ^ self getSeconds - (aTime getSeconds)
       
   238 !
       
   239 
       
   240 addTime:timeAmount
       
   241     "return a new Time/Date timeAmount seconds from myself"
       
   242 
       
   243     ^ self class new setSeconds:(self getSeconds + timeAmount)
       
   244 !
       
   245 
       
   246 subtractTime:timeAmount
       
   247     "return a new Time/Date timeAmount seconds before myself"
       
   248 
       
   249     ^ self class new setSeconds:(self getSeconds - timeAmount)
       
   250 ! !
       
   251 
       
   252 !AbsoluteTime methodsFor:'printing & storing'!
       
   253 
       
   254 storeString
       
   255     |string|
       
   256 
       
   257     string := '(' , self class name , ' new setSecondsLow:'.
       
   258     string := string , secondsLow storeString.
       
   259     string := string , ' and:' , secondsHi storeString.
       
   260     string := string , ')'.
       
   261     ^ string
       
   262 ! !
       
   263 
       
   264 !AbsoluteTime methodsFor:'converting'!
   306 !AbsoluteTime methodsFor:'converting'!
   265 
   307 
   266 asSeconds
   308 asSeconds
   267     "return the number of seconds elapsed since whatever time the
   309     "return the number of seconds elapsed since whatever time the
   268      OperatingSystem bases its time upon. Since this is totally
   310      OperatingSystem bases its time upon. Since this is totally
   269      OS-dependent, do not use this method. (see Time>>asSeconds)"
   311      OS-dependent, do not interpret the value returned by this method.
       
   312      You can use it to add/subtract seconds or get time deltas, though."
   270 
   313 
   271     ^ (secondsHi * 16r10000) + secondsLow
   314     ^ (secondsHi * 16r10000) + secondsLow
   272 
   315 
   273     "
   316     "
   274      AbsoluteTime asSeconds
   317      AbsoluteTime now asSeconds
       
   318      AbsoluteTime fromSeconds:(AbsoluteTime now asSeconds + 3600) 
       
   319      Time hour:23 minutes:33 seconds:0         
       
   320      Time fromSeconds:((Time hour:23 minutes:33 seconds:0) asSeconds + 3600) 
   275     "
   321     "
   276 !
   322 !
   277 
   323 
   278 asDate
   324 asDate
   279     "return a Date object from the receiver"
   325     "return a Date object from the receiver"
   280 
   326 
   281     ^ Date fromOSTime:(Array with:secondsLow with:secondsHi) 
   327     ^ Date fromOSTime:(Array with:secondsLow with:secondsHi) 
       
   328 
       
   329     "
       
   330      AbsoluteTime now  
       
   331      AbsoluteTime now asDate
       
   332      (AbsoluteTime now addTime:3600) asDate 
       
   333      (AbsoluteTime now addTime:3600) asTime 
       
   334      AbsoluteTime fromSeconds:(AbsoluteTime now asSeconds + 3600) 
       
   335      (AbsoluteTime fromSeconds:(AbsoluteTime now asSeconds + 3600)) asDate  
       
   336     "
       
   337 
   282 !
   338 !
   283 
   339 
   284 asTime
   340 asTime
   285     ^ Time fromOSTime:(Array with:secondsLow with:secondsHi)
   341     ^ Time fromOSTime:(Array with:secondsLow with:secondsHi)
   286 ! !
   342 
   287 
   343     "
   288 !AbsoluteTime methodsFor:'private'!
   344      AbsoluteTime now  
   289 
   345      AbsoluteTime now asTime
   290 secondsLow
   346      (AbsoluteTime now addTime:3600) asTime 
   291     ^ secondsLow
   347     "
   292 !
   348 
   293 
   349 ! !
   294 secondsHi
   350 
   295     ^ secondsHi
   351 !AbsoluteTime methodsFor:'arithmetic'!
   296 !
   352 
   297 
   353 - aTime
   298 getSeconds
   354     "return delta in seconds between 2 times/dates."
   299     ^ (secondsHi * 16r10000) + secondsLow
   355 
   300 !
   356     ^ self getSeconds - (aTime getSeconds)
   301 
   357 !
   302 setSeconds:secs
   358 
   303     secondsHi := secs // 16r10000.
   359 addTime:timeAmount
   304     secondsLow := secs \\ 16r10000
   360     "return a new instance of myself, timeAmount seconds afterwards"
   305 !
   361 
   306 
   362     ^ self class new setSeconds:(self getSeconds + timeAmount)
   307 setSecondsLow:secsLow and:secsHi
   363 !
   308     secondsHi := secsHi.
   364 
   309     secondsLow := secsLow
   365 subtractTime:timeAmount
   310 ! !
   366     "return a new instance opf myself, timeAmount seconds before myself"
       
   367 
       
   368     ^ self class new setSeconds:(self getSeconds - timeAmount)
       
   369 ! !
       
   370 
       
   371 !AbsoluteTime methodsFor:'printing & storing'!
       
   372 
       
   373 printOn:aStream
       
   374     |h min s d m y|
       
   375 
       
   376     OperatingSystem computeDatePartsOf:secondsLow and:secondsHi for:[
       
   377         :year :month :day | d := day. m := month. y := year.
       
   378     ].
       
   379     OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
       
   380         :hours :minutes :secs | h := hours. min := minutes. s := secs.
       
   381     ].
       
   382     d printOn:aStream.
       
   383     aStream nextPut:$-.
       
   384     m printOn:aStream.
       
   385     aStream nextPut:$-.
       
   386     y printOn:aStream.
       
   387     aStream space.
       
   388     h printOn:aStream leftPaddedTo:2 with:$0. 
       
   389     aStream nextPut:$:.
       
   390     min printOn:aStream leftPaddedTo:2 with:$0.
       
   391     aStream nextPut:$:.
       
   392     s printOn:aStream leftPaddedTo:2 with:$0.
       
   393 
       
   394     "
       
   395      AbsoluteTime now 
       
   396      AbsoluteTime fromSeconds:0 
       
   397      Time now            
       
   398      Date today         
       
   399     "
       
   400 !
       
   401 
       
   402 storeOn:aStream
       
   403     aStream nextPut:$(; 
       
   404             nextPutAll:self class name; 
       
   405             nextPutAll:' new setSecondsLow:'.
       
   406     secondsLow storeOn:aStream.
       
   407     aStream nextPutAll:' and:'.
       
   408     secondsHi storeOn:aStream.
       
   409     aStream nextPut:$).
       
   410 
       
   411     "
       
   412      AbsoluteTime now storeString '(AbsoluteTime new setSecondsLow:39757 and:12087)'
       
   413 
       
   414      AbsoluteTime readFromString:(AbsoluteTime now storeString) 
       
   415     "
       
   416 ! !
       
   417