author | Claus Gittinger <cg@exept.de> |
Thu, 25 Apr 1996 18:20:46 +0200 | |
changeset 1290 | 15ba3221b89b |
parent 1282 | 3f5eda57c516 |
child 1306 | c0189edbd96c |
permissions | -rw-r--r-- |
30 | 1 |
" |
2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
154 | 3 |
All Rights Reserved |
30 | 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 |
||
241 | 13 |
AbstractTime subclass:#AbsoluteTime |
795 | 14 |
instanceVariableNames:'osTime' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-General' |
|
30 | 18 |
! |
19 |
||
20 |
!AbsoluteTime class methodsFor:'documentation'! |
|
21 |
||
82 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
154 | 25 |
All Rights Reserved |
82 | 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 |
||
30 | 36 |
documentation |
37 |
" |
|
77 | 38 |
This class represents time values in seconds from 1st. Jan 1970, as |
39 |
used in the Unix operating system. Its implementation is not the same |
|
40 |
as in ST-80 (which represents Time as seconds from 1. Jan 1901. |
|
241 | 41 |
|
42 |
Actually, the implementation does not depend or even know which time/date |
|
43 |
the OperatingSystem bases its time upon - it is simply keeping the value(s) |
|
44 |
as return from the OS. |
|
92 | 45 |
For conversion, these values are given back to the OS, which will know |
46 |
how to convert these times. |
|
47 |
This has the advantage, that time-stamps on files (such as last-access- |
|
48 |
time or last-modifiaction-time) can be handled transparent to other |
|
49 |
times (especially comparison). |
|
30 | 50 |
|
77 | 51 |
Since unix-times are 32 bit which does not fit into a SmallInteger, |
92 | 52 |
we keep low and hi 16bits of the time separately (it could have been |
53 |
implemented using LargeIntegers though). |
|
30 | 54 |
|
241 | 55 |
This class should not be confused with Time (which only represents the |
56 |
time within one day). Time instances cannot be used to compare times across |
|
57 |
midnight; instances of AbsoluteTime can. |
|
58 |
||
1282 | 59 |
[See also:] |
60 |
Time Date |
|
61 |
Delay ProcessorScheduler |
|
30 | 62 |
" |
63 |
! ! |
|
64 |
||
65 |
!AbsoluteTime class methodsFor:'instance creation'! |
|
66 |
||
241 | 67 |
day:d month:m year:y hour:h minutes:min seconds:s |
1228 | 68 |
"return an instance of the receiver, given individual components. |
69 |
See also `AbsoluteTime now' and other protocol inherited |
|
70 |
from my superclass." |
|
30 | 71 |
|
423 | 72 |
^ self basicNew |
1228 | 73 |
fromOSTime:(OperatingSystem |
74 |
computeTimePartsFromYear:y month:m day:d |
|
75 |
hour:h minute:min seconds:s) |
|
30 | 76 |
|
241 | 77 |
" |
78 |
AbsoluteTime day:2 month:1 year:1991 hour:12 minutes:30 seconds:0 |
|
79 |
AbsoluteTime day:8 month:1 year:1995 hour:0 minutes:43 seconds:48 |
|
80 |
" |
|
1228 | 81 |
|
82 |
"Modified: 19.4.1996 / 15:21:12 / cg" |
|
275 | 83 |
! |
84 |
||
423 | 85 |
fromDate:aDate andTime:aTime |
86 |
"return an instance of the receiver, initialized from a time and a date |
|
1228 | 87 |
object. |
88 |
See also `AbsoluteTime now' and other protocol inherited |
|
89 |
from my superclass." |
|
423 | 90 |
|
91 |
^ self |
|
1228 | 92 |
day:aDate day |
93 |
month:aDate month |
|
94 |
year:aDate year |
|
95 |
hour:aTime hours |
|
96 |
minutes:aTime minutes |
|
97 |
seconds:aTime seconds |
|
423 | 98 |
|
99 |
" |
|
1228 | 100 |
AbsoluteTime fromDate:(Date today) andTime:(Time now) |
101 |
AbsoluteTime fromDate:(Date today plusDays:1) andTime:(Time now) |
|
102 |
AbsoluteTime now |
|
423 | 103 |
" |
104 |
||
105 |
"Modified: 8.9.1995 / 15:07:30 / claus" |
|
1228 | 106 |
"Modified: 19.4.1996 / 15:25:46 / cg" |
423 | 107 |
! |
108 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
109 |
readFrom:aStringOrStream onError:exceptionBlock |
275 | 110 |
"return a new AbsoluteTime, reading a printed representation from aStream. |
111 |
The string is interpreted as 24 hour format, as printed. |
|
112 |
Notice, that this is not the storeString format and |
|
113 |
is different from the format expected by readFrom:." |
|
114 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
115 |
|str day month year hour min sec ex| |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
116 |
|
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
117 |
str := aStringOrStream readStream. |
275 | 118 |
|
119 |
ex := [^ exceptionBlock value]. |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
120 |
day := Integer readFrom:str onError:ex. |
1133 | 121 |
(day between:1 and:31) ifFalse:[^ exceptionBlock value]. |
275 | 122 |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
123 |
[str peek isDigit] whileFalse:[str next]. |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
124 |
month := Integer readFrom:str onError:ex. |
1133 | 125 |
(month between:1 and:12) ifFalse:[^ exceptionBlock value]. |
275 | 126 |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
127 |
[str peek isDigit] whileFalse:[str next]. |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
128 |
year := Integer readFrom:str onError:ex. |
275 | 129 |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
130 |
[str peek isDigit] whileFalse:[str next]. |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
131 |
hour := Integer readFrom:str onError:ex. |
1133 | 132 |
(hour between:0 and:24) ifFalse:[^ exceptionBlock value]. |
275 | 133 |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
134 |
[str peek isDigit] whileFalse:[str next]. |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
135 |
min := Integer readFrom:str onError:ex. |
1133 | 136 |
(min between:0 and:59) ifFalse:[^ exceptionBlock value]. |
275 | 137 |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
138 |
[str peek isDigit] whileFalse:[str next]. |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
139 |
sec := Integer readFrom:str onError:ex. |
1133 | 140 |
(sec between:0 and:59) ifFalse:[^ exceptionBlock value]. |
275 | 141 |
|
142 |
"special check" |
|
143 |
hour == 24 ifTrue:[ |
|
1133 | 144 |
(min ~~ 0 or:[sec ~~ 0]) ifTrue:[^ exceptionBlock value]. |
275 | 145 |
]. |
146 |
||
147 |
^ self day:day month:month year:year hour:hour minutes:min seconds:sec |
|
148 |
||
149 |
" |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
150 |
AbsoluteTime readFrom:'20-2-1995 13:11:06' |
275 | 151 |
" |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
152 |
|
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
153 |
"Modified: 16.11.1995 / 22:49:39 / cg" |
241 | 154 |
! ! |
30 | 155 |
|
156 |
!AbsoluteTime methodsFor:'accessing'! |
|
157 |
||
158 |
day |
|
92 | 159 |
"return the day-in-month of the receiver (1..31). |
275 | 160 |
For compatibility, use instances of Date for this." |
30 | 161 |
|
241 | 162 |
|d| |
163 |
||
275 | 164 |
OperatingSystem computeDatePartsOf:osTime for:[:year :month :day | |
242 | 165 |
d := day |
241 | 166 |
]. |
167 |
^ d |
|
54 | 168 |
|
92 | 169 |
" |
241 | 170 |
AbsoluteTime now day |
92 | 171 |
" |
30 | 172 |
! |
173 |
||
241 | 174 |
hours |
175 |
"return the hours (0..23)" |
|
176 |
||
177 |
|hr| |
|
178 |
||
275 | 179 |
OperatingSystem computeTimePartsOf:osTime for:[:hours :minutes :secs | |
242 | 180 |
hr := hours |
241 | 181 |
]. |
182 |
^ hr |
|
54 | 183 |
|
92 | 184 |
" |
241 | 185 |
AbsoluteTime now hours |
186 |
" |
|
187 |
||
188 |
! |
|
189 |
||
190 |
minutes |
|
191 |
"return the minutes (0..59)" |
|
192 |
||
193 |
|m| |
|
194 |
||
275 | 195 |
OperatingSystem computeTimePartsOf:osTime for:[:hours :minutes :secs | |
242 | 196 |
m := minutes |
241 | 197 |
]. |
198 |
^ m |
|
199 |
||
200 |
" |
|
201 |
AbsoluteTime now minutes |
|
92 | 202 |
" |
241 | 203 |
|
204 |
! |
|
205 |
||
699 | 206 |
month |
207 |
"return the month of the receiver (1..12). |
|
208 |
For compatibility, use instances of Date for this." |
|
209 |
||
210 |
|m| |
|
211 |
||
212 |
OperatingSystem computeDatePartsOf:osTime for:[ :year :month :day | |
|
213 |
m := month |
|
214 |
]. |
|
215 |
^ m |
|
216 |
||
217 |
" |
|
218 |
AbsoluteTime now month |
|
219 |
" |
|
220 |
! |
|
221 |
||
241 | 222 |
seconds |
223 |
"return the seconds (0..59)" |
|
224 |
||
225 |
|s| |
|
226 |
||
275 | 227 |
OperatingSystem computeTimePartsOf:osTime for:[:hours :minutes :secs | |
242 | 228 |
s := secs |
241 | 229 |
]. |
230 |
^ s |
|
231 |
||
232 |
" |
|
233 |
AbsoluteTime now seconds |
|
234 |
" |
|
235 |
||
92 | 236 |
! |
237 |
||
799 | 238 |
utcOffset |
239 |
"return the difference between UTC (Greenwich Mean Time) and the local time |
|
240 |
if daylight saving time applies to ourself, take that into account. |
|
241 |
Add utcOffset bevor converting to get UTC-Time resp. UTC-Date. |
|
242 |
" |
|
243 |
||
244 |
^ (OperatingSystem computeTimeAndDateFrom:osTime) at:7 |
|
245 |
||
246 |
" |
|
247 |
AbsoluteTime now utcOffset |
|
248 |
(AbsoluteTime day:1 month:7 year:1995 hour:12 minutes:0 seconds:0) utcOffset |
|
249 |
" |
|
250 |
||
251 |
"Modified: 20.12.1995 / 17:28:49 / stefan" |
|
252 |
! |
|
253 |
||
699 | 254 |
year |
255 |
"return the year of the receiver i.e. 1992. |
|
256 |
For compatibility, use instances of Date for this." |
|
241 | 257 |
|
699 | 258 |
|y| |
241 | 259 |
|
699 | 260 |
OperatingSystem computeDatePartsOf:osTime for:[:year :month :day | |
261 |
y := year |
|
262 |
]. |
|
263 |
^ y |
|
241 | 264 |
|
265 |
" |
|
699 | 266 |
AbsoluteTime now year |
241 | 267 |
" |
268 |
! ! |
|
269 |
||
30 | 270 |
!AbsoluteTime methodsFor:'arithmetic'! |
271 |
||
272 |
- aTime |
|
242 | 273 |
"return the delta in seconds between 2 times." |
30 | 274 |
|
275 |
^ self getSeconds - (aTime getSeconds) |
|
276 |
! |
|
277 |
||
242 | 278 |
addDays:numberOfDays |
279 |
"return a new instance of myself, numberOfDays afterwards." |
|
280 |
||
281 |
^ self addSeconds:(numberOfDays * (60 * 60 * 24)) |
|
282 |
||
283 |
" |
|
284 |
|t| |
|
285 |
||
286 |
t := AbsoluteTime now. |
|
287 |
t printNL. |
|
288 |
(t addDays:7) printNL |
|
289 |
" |
|
290 |
! |
|
291 |
||
292 |
subtractDays:numberOfDays |
|
293 |
"return a new instance of myself, numberOfDays before." |
|
294 |
||
295 |
^ self subtractSeconds:(numberOfDays * (60 * 60 * 24)) |
|
296 |
||
297 |
" |
|
298 |
|t| |
|
299 |
||
300 |
t := AbsoluteTime now. |
|
301 |
t printNL. |
|
302 |
(t subtractDays:50) printNL |
|
303 |
" |
|
30 | 304 |
! ! |
305 |
||
699 | 306 |
!AbsoluteTime methodsFor:'comparing'! |
307 |
||
308 |
< aTime |
|
309 |
"return true if the argument, aTime is before the receiver" |
|
310 |
||
311 |
|myHi otherHi| |
|
312 |
||
313 |
myHi := self secondsHi. |
|
314 |
otherHi := aTime secondsHi. |
|
315 |
myHi < otherHi ifTrue:[^ true]. |
|
316 |
myHi > otherHi ifTrue:[^ false]. |
|
317 |
^ self secondsLow < aTime secondsLow |
|
318 |
! |
|
319 |
||
320 |
= aTime |
|
321 |
"return true if the argument, aTime represents the same time" |
|
322 |
||
323 |
(aTime species == self species) ifFalse:[^ false]. |
|
324 |
^ (self secondsLow == aTime secondsLow) and:[self secondsHi == aTime secondsHi] |
|
325 |
! |
|
326 |
||
327 |
> aTime |
|
328 |
"return true if the argument, aTime is after the receiver" |
|
329 |
||
330 |
|myHi otherHi| |
|
331 |
||
332 |
myHi := self secondsHi. |
|
333 |
otherHi := aTime secondsHi. |
|
334 |
myHi > otherHi ifTrue:[^ true]. |
|
335 |
myHi < otherHi ifTrue:[^ false]. |
|
336 |
^ self secondsLow > aTime secondsLow |
|
337 |
! |
|
338 |
||
339 |
hash |
|
340 |
"return an integer useful for hashing on times" |
|
341 |
||
342 |
^ self getSeconds |
|
343 |
! ! |
|
344 |
||
345 |
!AbsoluteTime methodsFor:'converting'! |
|
346 |
||
347 |
asAbsoluteTime |
|
348 |
"return an AbsoluteTime object from the receiver - thats the receiver." |
|
349 |
||
350 |
^ self |
|
351 |
! |
|
352 |
||
353 |
asDate |
|
354 |
"return a Date object from the receiver. |
|
355 |
The returned date will only represent the day - not the timeOfDay." |
|
356 |
||
357 |
^ Date fromOSTime:osTime |
|
358 |
||
359 |
" |
|
360 |
AbsoluteTime now |
|
361 |
AbsoluteTime now asDate |
|
362 |
(AbsoluteTime now addTime:3600) asDate |
|
363 |
(AbsoluteTime now addTime:3600) asTime |
|
364 |
AbsoluteTime fromSeconds:(AbsoluteTime now asSeconds + 3600) |
|
365 |
(AbsoluteTime fromSeconds:(AbsoluteTime now asSeconds + 3600)) asDate |
|
366 |
" |
|
367 |
||
368 |
! |
|
369 |
||
370 |
asSeconds |
|
371 |
"return the number of seconds elapsed since whatever time the |
|
372 |
OperatingSystem bases its time upon. Since this is totally |
|
373 |
OS-dependent, do not interpret the value returned by this method. |
|
374 |
You can use it to add/subtract seconds or get time deltas, though." |
|
375 |
||
376 |
^ self getSeconds |
|
377 |
||
378 |
" |
|
379 |
AbsoluteTime now asSeconds |
|
380 |
AbsoluteTime fromSeconds:(AbsoluteTime now asSeconds + 3600) |
|
381 |
Time hour:23 minutes:33 seconds:0 |
|
382 |
Time fromSeconds:((Time hour:23 minutes:33 seconds:0) asSeconds + 3600) |
|
383 |
" |
|
384 |
! |
|
385 |
||
386 |
asTime |
|
387 |
"return a Time object from the receiver. |
|
388 |
The returned time will only represent the timeOfDay - not the day." |
|
389 |
||
390 |
^ Time fromOSTime:osTime |
|
391 |
||
392 |
" |
|
393 |
AbsoluteTime now |
|
394 |
AbsoluteTime now asTime |
|
395 |
(AbsoluteTime now addTime:3600) asTime |
|
396 |
" |
|
397 |
! ! |
|
398 |
||
213 | 399 |
!AbsoluteTime methodsFor:'printing & storing'! |
30 | 400 |
|
241 | 401 |
printOn:aStream |
275 | 402 |
"append a user readable representation of the receiver to aStream. |
403 |
The format is compatible with readFromString:, but not with readFrom:." |
|
404 |
||
241 | 405 |
|h min s d m y| |
30 | 406 |
|
275 | 407 |
OperatingSystem computeDatePartsOf:osTime for:[ |
242 | 408 |
:year :month :day | d := day. m := month. y := year. |
241 | 409 |
]. |
275 | 410 |
OperatingSystem computeTimePartsOf:osTime for:[ |
242 | 411 |
:hours :minutes :secs | h := hours. min := minutes. s := secs. |
241 | 412 |
]. |
413 |
d printOn:aStream. |
|
414 |
aStream nextPut:$-. |
|
415 |
m printOn:aStream. |
|
416 |
aStream nextPut:$-. |
|
417 |
y printOn:aStream. |
|
418 |
aStream space. |
|
419 |
h printOn:aStream leftPaddedTo:2 with:$0. |
|
420 |
aStream nextPut:$:. |
|
421 |
min printOn:aStream leftPaddedTo:2 with:$0. |
|
422 |
aStream nextPut:$:. |
|
423 |
s printOn:aStream leftPaddedTo:2 with:$0. |
|
92 | 424 |
|
425 |
" |
|
275 | 426 |
AbsoluteTime now printOn:Transcript. Transcript cr. |
427 |
(AbsoluteTime fromSeconds:0) printOn:Transcript. Transcript cr. |
|
428 |
Time now printOn:Transcript. Transcript cr. |
|
429 |
Date today printOn:Transcript. Transcript cr. |
|
92 | 430 |
" |
30 | 431 |
! |
432 |
||
241 | 433 |
storeOn:aStream |
275 | 434 |
"store the receiver in a format suitable for reconstruction of the |
435 |
receiver via readFrom:" |
|
436 |
||
241 | 437 |
aStream nextPut:$(; |
242 | 438 |
nextPutAll:self class name; |
275 | 439 |
nextPutAll:' new setSeconds:'. |
440 |
self getSeconds storeOn:aStream. |
|
241 | 441 |
aStream nextPut:$). |
54 | 442 |
|
241 | 443 |
" |
275 | 444 |
AbsoluteTime now storeString |
30 | 445 |
|
275 | 446 |
AbsoluteTime readFrom:(AbsoluteTime now storeString) readStream |
241 | 447 |
" |
30 | 448 |
! ! |
699 | 449 |
|
450 |
!AbsoluteTime methodsFor:'private'! |
|
451 |
||
452 |
fromOSTimeLow:secsLow and:secsHi |
|
453 |
"strictly private: set the seconds from an OS time (since whatever)" |
|
454 |
||
455 |
osTime := Array with:secsLow with:secsHi |
|
456 |
! |
|
457 |
||
458 |
getSeconds |
|
459 |
"strictly private: return the seconds (since whatever)" |
|
460 |
||
461 |
^ ((osTime at:2) * 16r10000) + (osTime at:1) |
|
462 |
! |
|
463 |
||
464 |
secondsHi |
|
465 |
"strictly private: return the hi part of the seconds" |
|
466 |
||
467 |
^ osTime at:2 |
|
468 |
! |
|
469 |
||
470 |
secondsLow |
|
471 |
"strictly private: return the low part of the seconds" |
|
472 |
||
473 |
^ osTime at:1 |
|
474 |
! |
|
475 |
||
476 |
setSeconds:secs |
|
477 |
"strictly private: set the seconds (since whatever)" |
|
478 |
||
795 | 479 |
osTime := Array with:(secs \\ 16r10000) with:(secs // 16r10000) |
480 |
||
481 |
"Modified: 20.12.1995 / 11:46:36 / stefan" |
|
699 | 482 |
! |
483 |
||
484 |
setSecondsLow:secsLow and:secsHi |
|
485 |
"strictly private: set the seconds (since whatever)" |
|
486 |
||
487 |
osTime := Array with:secsLow with:secsHi |
|
488 |
! ! |
|
489 |
||
490 |
!AbsoluteTime class methodsFor:'documentation'! |
|
491 |
||
492 |
version |
|
1282 | 493 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/AbsoluteTime.st,v 1.23 1996-04-25 11:08:07 cg Exp $' |
699 | 494 |
! ! |