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