AbstractTime.st
changeset 699 12f456343eea
parent 528 a083413dfbe8
child 1227 e89b39909085
equal deleted inserted replaced
698:04533375e12c 699:12f456343eea
     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 'From Smalltalk/X, Version:2.10.4 on 7-feb-1995 at 11:53:03 pm'!
       
    14 
       
    15 Magnitude subclass:#AbstractTime
    13 Magnitude subclass:#AbstractTime
    16 	 instanceVariableNames:''
    14 	 instanceVariableNames:''
    17 	 classVariableNames:''
    15 	 classVariableNames:''
    18 	 poolDictionaries:''
    16 	 poolDictionaries:''
    19 	 category:'Magnitude-General'
    17 	 category:'Magnitude-General'
    38 documentation
    36 documentation
    39 "
    37 "
    40     This is an abstract class; there are no instances in the system.
    38     This is an abstract class; there are no instances in the system.
    41     It is meant as a home for methods common to time handling classes.
    39     It is meant as a home for methods common to time handling classes.
    42 "
    40 "
    43 !
       
    44 
       
    45 version
       
    46     ^ '$Header: /cvs/stx/stx/libbasic/AbstractTime.st,v 1.7 1995-11-11 14:26:24 cg Exp $'
       
    47 ! !
    41 ! !
    48 
    42 
    49 !AbstractTime class methodsFor:'instance creation'!
    43 !AbstractTime class methodsFor:'instance creation'!
    50 
    44 
    51 now
    45 dateAndTimeNow
    52     "return an instance of myself representing this moment"
    46     "return an array filled with date and time"
    53 
    47 
    54     ^ self basicNew fromOSTime:(OperatingSystem getTimeParts)
    48     ^ Array with:(Date today) with:(Time now)
    55 
    49 
    56     "
    50     "
    57      AbsoluteTime now   
    51      Time dateAndTimeNow
    58      Time now   
       
    59     "
       
    60 !
       
    61 
       
    62 fromSeconds:seconds
       
    63     "return an instance that is constructed from seconds.
       
    64      This method is only allowed for second values as returned by
       
    65      getSeconds, possibly adding/subtracting to that. Never
       
    66      depend on any specific interpretation of the seconds."
       
    67 
       
    68    ^ self basicNew setSeconds:seconds
       
    69 
       
    70     "
       
    71      Time fromSeconds:0             should return midnight
       
    72      AbsoluteTime fromSeconds:0     on UNIX: returns 1st. Jan 1970
       
    73 				    on others: dont know
       
    74     "
    52     "
    75 !
    53 !
    76 
    54 
    77 fromOSTime:osTime
    55 fromOSTime:osTime
    78     "return a time, representing the time given by the operatingSystem time.
    56     "return a time, representing the time given by the operatingSystem time.
    98      Not meant for public use."
    76      Not meant for public use."
    99 
    77 
   100     ^ self basicNew fromOSTimeLow:osTimeLow and:osTimeHigh
    78     ^ self basicNew fromOSTimeLow:osTimeLow and:osTimeHigh
   101 !
    79 !
   102 
    80 
   103 dateAndTimeNow
    81 fromSeconds:seconds
   104     "return an array filled with date and time"
    82     "return an instance that is constructed from seconds.
   105 
    83      This method is only allowed for second values as returned by
   106     ^ Array with:(Date today) with:(Time now)
    84      getSeconds, possibly adding/subtracting to that. Never
   107 
    85      depend on any specific interpretation of the seconds."
   108     "
    86 
   109      Time dateAndTimeNow
    87    ^ self basicNew setSeconds:seconds
   110     "
    88 
       
    89     "
       
    90      Time fromSeconds:0             should return midnight
       
    91      AbsoluteTime fromSeconds:0     on UNIX: returns 1st. Jan 1970
       
    92 				    on others: dont know
       
    93     "
       
    94 !
       
    95 
       
    96 now
       
    97     "return an instance of myself representing this moment"
       
    98 
       
    99     ^ self basicNew fromOSTime:(OperatingSystem getTimeParts)
       
   100 
       
   101     "
       
   102      AbsoluteTime now   
       
   103      Time now   
       
   104     "
       
   105 ! !
       
   106 
       
   107 !AbstractTime class methodsFor:'ST-80 compatibility'!
       
   108 
       
   109 totalSeconds
       
   110     "returns an internal second clock. Dont interpret the returned
       
   111      value - if at all, use it to compute time deltas, by subtracting
       
   112      returned values."
       
   113 
       
   114     ^ self secondClock
   111 ! !
   115 ! !
   112 
   116 
   113 !AbstractTime class methodsFor:'obsolete'!
   117 !AbstractTime class methodsFor:'obsolete'!
   114 
   118 
   115 fromUnixTimeLow:low and:hi
   119 fromUnixTimeLow:low and:hi
   116     "return an instance of myself, given the unix time.
   120     "return an instance of myself, given the unix time.
   117      Internal interface - not for public use."
   121      Internal interface - not for public use."
   118 
   122 
   119     ^ self fromOSTimeLow:low and:hi
   123     ^ self fromOSTimeLow:low and:hi
       
   124 ! !
       
   125 
       
   126 !AbstractTime class methodsFor:'queries'!
       
   127 
       
   128 millisecondClockValue
       
   129     "return the millisecond clock - since this one overruns
       
   130      regularly, use the value only for short timing deltas.
       
   131      Also remember that it wraps when compares these values."
       
   132 
       
   133     ^ OperatingSystem getMillisecondTime.
       
   134 
       
   135     "
       
   136      Time millisecondClockValue 
       
   137     "
       
   138 !
       
   139 
       
   140 secondClock
       
   141     "return seconds of now - for GNU-ST compatibility"
       
   142 
       
   143     ^ OperatingSystem getTime
       
   144 
       
   145     "
       
   146      AbstractTime secondClock    
       
   147     "
       
   148 ! !
       
   149 
       
   150 !AbstractTime class methodsFor:'timing evaluations'!
       
   151 
       
   152 millisecondsToRun:aBlock
       
   153     "evaluate the argument, aBlock; return the number of milliseconds it took"
       
   154 
       
   155     |startTime endTime|
       
   156 
       
   157     startTime := self millisecondClockValue.
       
   158     aBlock value.
       
   159     endTime := self millisecondClockValue.
       
   160     ^ endTime - startTime
       
   161 
       
   162     "
       
   163      Time millisecondsToRun:[100 factorial]  
       
   164     "
       
   165 !
       
   166 
       
   167 secondsToRun:aBlock
       
   168     "evaluate the argument, aBlock; return the number of seconds it took"
       
   169 
       
   170     |startTime endTime|
       
   171 
       
   172     startTime := self secondClock.
       
   173     aBlock value.
       
   174     endTime := self secondClock.
       
   175     ^ endTime - startTime
       
   176 
       
   177     "
       
   178      Time secondsToRun:[1000 factorial]  
       
   179     "
       
   180 ! !
       
   181 
       
   182 !AbstractTime methodsFor:'accessing'!
       
   183 
       
   184 hourInDay
       
   185     "return the hours (0..23)"
       
   186 
       
   187     ^ self hours
       
   188 
       
   189     "
       
   190      AbsoluteTime now hourInDay 
       
   191      Time now hourInDay 
       
   192     "
       
   193 !
       
   194 
       
   195 minuteInDay
       
   196     "return the minutes (0..59)"
       
   197 
       
   198     ^ self minutes.
       
   199 
       
   200     "
       
   201      AbsoluteTime now minuteInDay 
       
   202      Time now minuteInDay 
       
   203     "
       
   204 !
       
   205 
       
   206 secondDay
       
   207     "return the seconds (0..59)"
       
   208 
       
   209     ^ self seconds
       
   210 
       
   211     "
       
   212      AbsoluteTime now secondInDay 
       
   213      Time now seconds 
       
   214     "
       
   215 ! !
       
   216 
       
   217 !AbstractTime methodsFor:'arithmetic'!
       
   218 
       
   219 - aTime
       
   220     "return the delta in seconds between 2 times."
       
   221 
       
   222     ^ self getSeconds - (aTime getSeconds)
       
   223 
       
   224     "
       
   225      |t1 t2|
       
   226 
       
   227      t1 := AbsoluteTime now.
       
   228      (Delay forSeconds:5) wait.
       
   229      t2 := AbsoluteTime now.
       
   230      'seconds passed: ' print. (t2 - t1) printNL.
       
   231     "
       
   232 !
       
   233 
       
   234 addHours:numberOfHours
       
   235     "return a new instance of myself, numberOfHours afterwards."
       
   236 
       
   237     ^ self addSeconds:(numberOfHours * (60 * 60))
       
   238 
       
   239     "
       
   240      |t|
       
   241 
       
   242      t := AbsoluteTime now.
       
   243      t printNL.
       
   244      (t addHours:50) printNL
       
   245     "
       
   246 !
       
   247 
       
   248 addMinutes:numberOfMinutes
       
   249     "return a new instance of myself, numberOfMinutes afterwards."
       
   250 
       
   251     ^ self addSeconds:(numberOfMinutes * 60)
       
   252 
       
   253     "
       
   254      |t|
       
   255 
       
   256      t := AbsoluteTime now. t printNL. (t addMinutes:50) printNL
       
   257 
       
   258      t := Time now. t printNL. (t addMinutes:50) printNL
       
   259     "
       
   260 !
       
   261 
       
   262 addSeconds:numberOfSeconds
       
   263     "return a new instance of myself, numberOfSeconds afterwards."
       
   264 
       
   265     ^ self species basicNew setSeconds:(self getSeconds + numberOfSeconds)
       
   266 
       
   267     "
       
   268      |t|
       
   269 
       
   270      t := AbsoluteTime now. t printNL. (t addSeconds:50) printNL.
       
   271 
       
   272      t := Time now. t printNL. (t addSeconds:50) printNL
       
   273     "
       
   274 !
       
   275 
       
   276 addTime:timeAmount
       
   277     "return a new instance of myself, timeAmount seconds afterwards.
       
   278      AddTime is a bad name - it does not add a time, but expects
       
   279      a number. Use any of addSeconds/addHours etc."
       
   280 
       
   281     ^ self species basicNew setSeconds:(self getSeconds + timeAmount)
       
   282 !
       
   283 
       
   284 subtractHours:numberOfHours
       
   285     "return a new instance of myself, numberOfHours before."
       
   286 
       
   287     ^ self subtractSeconds:(numberOfHours * (60 * 60))
       
   288 
       
   289     "
       
   290      |t|
       
   291 
       
   292      t := AbsoluteTime now.
       
   293      t printNL.
       
   294      (t subtractHours:50) printNL
       
   295     "
       
   296 !
       
   297 
       
   298 subtractMinutes:numberOfMinutes
       
   299     "return a new instance of myself, numberOfMinutes before."
       
   300 
       
   301     ^ self subtractSeconds:(numberOfMinutes * 60)
       
   302 
       
   303     "
       
   304      |t|
       
   305 
       
   306      t := AbsoluteTime now.
       
   307      t printNL.
       
   308      (t subtractMinutes:50) printNL
       
   309     "
       
   310 !
       
   311 
       
   312 subtractSeconds:numberOfSeconds
       
   313     "return a new instance of myself, numberOfSeconds before."
       
   314 
       
   315     ^ self species basicNew setSeconds:(self getSeconds - numberOfSeconds)
       
   316 
       
   317     "
       
   318      |t|
       
   319 
       
   320      t := AbsoluteTime now.
       
   321      t printNL.
       
   322      (t subtractSeconds:50) printNL
       
   323     "
       
   324 !
       
   325 
       
   326 subtractTime:timeAmount
       
   327     "return a new instance opf myself, timeAmount seconds before myself.
       
   328      SubtractTime is a bad name - it does not subtract a time, but expects
       
   329      a number. Use any of subtractSeconds/subtractHours etc."
       
   330 
       
   331     ^ self species basicNew setSeconds:(self getSeconds - timeAmount)
   120 ! !
   332 ! !
   121 
   333 
   122 !AbstractTime methodsFor:'private'!
   334 !AbstractTime methodsFor:'private'!
   123 
   335 
   124 fromOSTime:timeParts
   336 fromOSTime:timeParts
   133      represented), this must be done by a concrete class."
   345      represented), this must be done by a concrete class."
   134 
   346 
   135     ^ self subclassResponsibility
   347     ^ self subclassResponsibility
   136 !
   348 !
   137 
   349 
       
   350 getSeconds
       
   351     "get the seconds.
       
   352      Since I am abstract (not knowing how the time is actually
       
   353      represented), this must be done by a concrete class."
       
   354 
       
   355     ^ self subclassResponsibility
       
   356 !
       
   357 
   138 setSeconds:secs
   358 setSeconds:secs
   139     "set the seconds.
   359     "set the seconds.
   140      Since I am abstract (not knowing how the time is actually
   360      Since I am abstract (not knowing how the time is actually
   141      represented), this must be done by a concrete class."
   361      represented), this must be done by a concrete class."
   142 
   362 
   143     ^ self subclassResponsibility
   363     ^ self subclassResponsibility
   144 !
   364 ! !
   145 
   365 
   146 getSeconds
   366 !AbstractTime class methodsFor:'documentation'!
   147     "get the seconds.
   367 
   148      Since I am abstract (not knowing how the time is actually
   368 version
   149      represented), this must be done by a concrete class."
   369     ^ '$Header: /cvs/stx/stx/libbasic/AbstractTime.st,v 1.8 1995-12-07 21:31:40 cg Exp $'
   150 
   370 ! !
   151     ^ self subclassResponsibility
       
   152 ! !
       
   153 
       
   154 !AbstractTime methodsFor:'accessing'!
       
   155 
       
   156 hourInDay
       
   157     "return the hours (0..23)"
       
   158 
       
   159     ^ self hours
       
   160 
       
   161     "
       
   162      AbsoluteTime now hourInDay 
       
   163      Time now hourInDay 
       
   164     "
       
   165 !
       
   166 
       
   167 minuteInDay
       
   168     "return the minutes (0..59)"
       
   169 
       
   170     ^ self minutes.
       
   171 
       
   172     "
       
   173      AbsoluteTime now minuteInDay 
       
   174      Time now minuteInDay 
       
   175     "
       
   176 !
       
   177 
       
   178 secondDay
       
   179     "return the seconds (0..59)"
       
   180 
       
   181     ^ self seconds
       
   182 
       
   183     "
       
   184      AbsoluteTime now secondInDay 
       
   185      Time now seconds 
       
   186     "
       
   187 ! !
       
   188 
       
   189 !AbstractTime methodsFor:'arithmetic'!
       
   190 
       
   191 - aTime
       
   192     "return the delta in seconds between 2 times."
       
   193 
       
   194     ^ self getSeconds - (aTime getSeconds)
       
   195 
       
   196     "
       
   197      |t1 t2|
       
   198 
       
   199      t1 := AbsoluteTime now.
       
   200      (Delay forSeconds:5) wait.
       
   201      t2 := AbsoluteTime now.
       
   202      'seconds passed: ' print. (t2 - t1) printNL.
       
   203     "
       
   204 !
       
   205 
       
   206 addSeconds:numberOfSeconds
       
   207     "return a new instance of myself, numberOfSeconds afterwards."
       
   208 
       
   209     ^ self species basicNew setSeconds:(self getSeconds + numberOfSeconds)
       
   210 
       
   211     "
       
   212      |t|
       
   213 
       
   214      t := AbsoluteTime now. t printNL. (t addSeconds:50) printNL.
       
   215 
       
   216      t := Time now. t printNL. (t addSeconds:50) printNL
       
   217     "
       
   218 !
       
   219 
       
   220 addMinutes:numberOfMinutes
       
   221     "return a new instance of myself, numberOfMinutes afterwards."
       
   222 
       
   223     ^ self addSeconds:(numberOfMinutes * 60)
       
   224 
       
   225     "
       
   226      |t|
       
   227 
       
   228      t := AbsoluteTime now. t printNL. (t addMinutes:50) printNL
       
   229 
       
   230      t := Time now. t printNL. (t addMinutes:50) printNL
       
   231     "
       
   232 !
       
   233 
       
   234 addHours:numberOfHours
       
   235     "return a new instance of myself, numberOfHours afterwards."
       
   236 
       
   237     ^ self addSeconds:(numberOfHours * (60 * 60))
       
   238 
       
   239     "
       
   240      |t|
       
   241 
       
   242      t := AbsoluteTime now.
       
   243      t printNL.
       
   244      (t addHours:50) printNL
       
   245     "
       
   246 !
       
   247 
       
   248 subtractSeconds:numberOfSeconds
       
   249     "return a new instance of myself, numberOfSeconds before."
       
   250 
       
   251     ^ self species basicNew setSeconds:(self getSeconds - numberOfSeconds)
       
   252 
       
   253     "
       
   254      |t|
       
   255 
       
   256      t := AbsoluteTime now.
       
   257      t printNL.
       
   258      (t subtractSeconds:50) printNL
       
   259     "
       
   260 !
       
   261 
       
   262 subtractMinutes:numberOfMinutes
       
   263     "return a new instance of myself, numberOfMinutes before."
       
   264 
       
   265     ^ self subtractSeconds:(numberOfMinutes * 60)
       
   266 
       
   267     "
       
   268      |t|
       
   269 
       
   270      t := AbsoluteTime now.
       
   271      t printNL.
       
   272      (t subtractMinutes:50) printNL
       
   273     "
       
   274 !
       
   275 
       
   276 subtractHours:numberOfHours
       
   277     "return a new instance of myself, numberOfHours before."
       
   278 
       
   279     ^ self subtractSeconds:(numberOfHours * (60 * 60))
       
   280 
       
   281     "
       
   282      |t|
       
   283 
       
   284      t := AbsoluteTime now.
       
   285      t printNL.
       
   286      (t subtractHours:50) printNL
       
   287     "
       
   288 !
       
   289 
       
   290 addTime:timeAmount
       
   291     "return a new instance of myself, timeAmount seconds afterwards.
       
   292      AddTime is a bad name - it does not add a time, but expects
       
   293      a number. Use any of addSeconds/addHours etc."
       
   294 
       
   295     ^ self species basicNew setSeconds:(self getSeconds + timeAmount)
       
   296 !
       
   297 
       
   298 subtractTime:timeAmount
       
   299     "return a new instance opf myself, timeAmount seconds before myself.
       
   300      SubtractTime is a bad name - it does not subtract a time, but expects
       
   301      a number. Use any of subtractSeconds/subtractHours etc."
       
   302 
       
   303     ^ self species basicNew setSeconds:(self getSeconds - timeAmount)
       
   304 ! !
       
   305 
       
   306 !AbstractTime class methodsFor:'queries'!
       
   307 
       
   308 secondClock
       
   309     "return seconds of now - for GNU-ST compatibility"
       
   310 
       
   311     ^ OperatingSystem getTime
       
   312 
       
   313     "
       
   314      AbstractTime secondClock    
       
   315     "
       
   316 !
       
   317 
       
   318 millisecondClockValue
       
   319     "return the millisecond clock - since this one overruns
       
   320      regularly, use the value only for short timing deltas.
       
   321      Also remember that it wraps when compares these values."
       
   322 
       
   323     ^ OperatingSystem getMillisecondTime.
       
   324 
       
   325     "
       
   326      Time millisecondClockValue 
       
   327     "
       
   328 ! !
       
   329 
       
   330 !AbstractTime class methodsFor:'timing evaluations'!
       
   331 
       
   332 secondsToRun:aBlock
       
   333     "evaluate the argument, aBlock; return the number of seconds it took"
       
   334 
       
   335     |startTime endTime|
       
   336 
       
   337     startTime := self secondClock.
       
   338     aBlock value.
       
   339     endTime := self secondClock.
       
   340     ^ endTime - startTime
       
   341 
       
   342     "
       
   343      Time secondsToRun:[1000 factorial]  
       
   344     "
       
   345 !
       
   346 
       
   347 millisecondsToRun:aBlock
       
   348     "evaluate the argument, aBlock; return the number of milliseconds it took"
       
   349 
       
   350     |startTime endTime|
       
   351 
       
   352     startTime := self millisecondClockValue.
       
   353     aBlock value.
       
   354     endTime := self millisecondClockValue.
       
   355     ^ endTime - startTime
       
   356 
       
   357     "
       
   358      Time millisecondsToRun:[100 factorial]  
       
   359     "
       
   360 ! !
       
   361 
       
   362 !AbstractTime class methodsFor:'ST-80 compatibility'!
       
   363 
       
   364 totalSeconds
       
   365     "returns an internal second clock. Dont interpret the returned
       
   366      value - if at all, use it to compute time deltas, by subtracting
       
   367      returned values."
       
   368 
       
   369     ^ self secondClock
       
   370 ! !
       
   371