author | Claus Gittinger <cg@exept.de> |
Thu, 07 Dec 1995 22:32:39 +0100 | |
changeset 699 | 12f456343eea |
parent 569 | 7134eb78cf48 |
child 795 | ff477bad0f2d |
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 |
275 | 14 |
instanceVariableNames:'osTime' |
241 | 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 |
||
92 | 59 |
See Time for more details. |
30 | 60 |
" |
61 |
! ! |
|
62 |
||
63 |
!AbsoluteTime class methodsFor:'instance creation'! |
|
64 |
||
241 | 65 |
day:d month:m year:y hour:h minutes:min seconds:s |
66 |
"return an instance of the receiver" |
|
30 | 67 |
|
423 | 68 |
^ self basicNew |
69 |
fromOSTime:(OperatingSystem |
|
242 | 70 |
computeTimePartsFromYear:y month:m day:d |
71 |
hour:h minute:min seconds:s) |
|
30 | 72 |
|
241 | 73 |
" |
74 |
AbsoluteTime day:2 month:1 year:1991 hour:12 minutes:30 seconds:0 |
|
75 |
AbsoluteTime day:8 month:1 year:1995 hour:0 minutes:43 seconds:48 |
|
76 |
" |
|
275 | 77 |
! |
78 |
||
423 | 79 |
fromDate:aDate andTime:aTime |
80 |
"return an instance of the receiver, initialized from a time and a date |
|
81 |
object." |
|
82 |
||
83 |
^ self |
|
84 |
day:aDate day |
|
85 |
month:aDate month |
|
86 |
year:aDate year |
|
87 |
hour:aTime hours |
|
88 |
minutes:aTime minutes |
|
89 |
seconds:aTime seconds |
|
90 |
||
91 |
" |
|
92 |
AbsoluteTime date:Date today time:Time now |
|
93 |
" |
|
94 |
||
95 |
"Modified: 8.9.1995 / 15:07:30 / claus" |
|
96 |
! |
|
97 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
98 |
readFrom:aStringOrStream onError:exceptionBlock |
275 | 99 |
"return a new AbsoluteTime, reading a printed representation from aStream. |
100 |
The string is interpreted as 24 hour format, as printed. |
|
101 |
Notice, that this is not the storeString format and |
|
102 |
is different from the format expected by readFrom:." |
|
103 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
104 |
|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
|
105 |
|
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
106 |
str := aStringOrStream readStream. |
275 | 107 |
|
108 |
ex := [^ exceptionBlock value]. |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
109 |
day := Integer readFrom:str onError:ex. |
392 | 110 |
(day between:1 and:31) ifFalse:[ex value]. |
275 | 111 |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
112 |
[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
|
113 |
month := Integer readFrom:str onError:ex. |
392 | 114 |
(month between:1 and:12) ifFalse:[ex value]. |
275 | 115 |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
116 |
[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
|
117 |
year := Integer readFrom:str onError:ex. |
275 | 118 |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
119 |
[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
|
120 |
hour := Integer readFrom:str onError:ex. |
392 | 121 |
(hour between:0 and:24) ifFalse:[ex 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 |
min := Integer readFrom:str onError:ex. |
392 | 125 |
(min between:0 and:59) ifFalse:[ex 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 |
sec := Integer readFrom:str onError:ex. |
392 | 129 |
(sec between:0 and:59) ifFalse:[ex value]. |
275 | 130 |
|
131 |
"special check" |
|
132 |
hour == 24 ifTrue:[ |
|
133 |
(min ~~ 0 or:[sec ~~ 0]) ifTrue:[ex value]. |
|
134 |
]. |
|
135 |
||
136 |
^ self day:day month:month year:year hour:hour minutes:min seconds:sec |
|
137 |
||
138 |
" |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
139 |
AbsoluteTime readFrom:'20-2-1995 13:11:06' |
275 | 140 |
" |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
141 |
|
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
142 |
"Modified: 16.11.1995 / 22:49:39 / cg" |
241 | 143 |
! ! |
30 | 144 |
|
145 |
!AbsoluteTime methodsFor:'accessing'! |
|
146 |
||
147 |
day |
|
92 | 148 |
"return the day-in-month of the receiver (1..31). |
275 | 149 |
For compatibility, use instances of Date for this." |
30 | 150 |
|
241 | 151 |
|d| |
152 |
||
275 | 153 |
OperatingSystem computeDatePartsOf:osTime for:[:year :month :day | |
242 | 154 |
d := day |
241 | 155 |
]. |
156 |
^ d |
|
54 | 157 |
|
92 | 158 |
" |
241 | 159 |
AbsoluteTime now day |
92 | 160 |
" |
30 | 161 |
! |
162 |
||
241 | 163 |
hours |
164 |
"return the hours (0..23)" |
|
165 |
||
166 |
|hr| |
|
167 |
||
275 | 168 |
OperatingSystem computeTimePartsOf:osTime for:[:hours :minutes :secs | |
242 | 169 |
hr := hours |
241 | 170 |
]. |
171 |
^ hr |
|
54 | 172 |
|
92 | 173 |
" |
241 | 174 |
AbsoluteTime now hours |
175 |
" |
|
176 |
||
177 |
! |
|
178 |
||
179 |
minutes |
|
180 |
"return the minutes (0..59)" |
|
181 |
||
182 |
|m| |
|
183 |
||
275 | 184 |
OperatingSystem computeTimePartsOf:osTime for:[:hours :minutes :secs | |
242 | 185 |
m := minutes |
241 | 186 |
]. |
187 |
^ m |
|
188 |
||
189 |
" |
|
190 |
AbsoluteTime now minutes |
|
92 | 191 |
" |
241 | 192 |
|
193 |
! |
|
194 |
||
699 | 195 |
month |
196 |
"return the month of the receiver (1..12). |
|
197 |
For compatibility, use instances of Date for this." |
|
198 |
||
199 |
|m| |
|
200 |
||
201 |
OperatingSystem computeDatePartsOf:osTime for:[ :year :month :day | |
|
202 |
m := month |
|
203 |
]. |
|
204 |
^ m |
|
205 |
||
206 |
" |
|
207 |
AbsoluteTime now month |
|
208 |
" |
|
209 |
! |
|
210 |
||
241 | 211 |
seconds |
212 |
"return the seconds (0..59)" |
|
213 |
||
214 |
|s| |
|
215 |
||
275 | 216 |
OperatingSystem computeTimePartsOf:osTime for:[:hours :minutes :secs | |
242 | 217 |
s := secs |
241 | 218 |
]. |
219 |
^ s |
|
220 |
||
221 |
" |
|
222 |
AbsoluteTime now seconds |
|
223 |
" |
|
224 |
||
92 | 225 |
! |
226 |
||
699 | 227 |
year |
228 |
"return the year of the receiver i.e. 1992. |
|
229 |
For compatibility, use instances of Date for this." |
|
241 | 230 |
|
699 | 231 |
|y| |
241 | 232 |
|
699 | 233 |
OperatingSystem computeDatePartsOf:osTime for:[:year :month :day | |
234 |
y := year |
|
235 |
]. |
|
236 |
^ y |
|
241 | 237 |
|
238 |
" |
|
699 | 239 |
AbsoluteTime now year |
241 | 240 |
" |
241 |
! ! |
|
242 |
||
30 | 243 |
!AbsoluteTime methodsFor:'arithmetic'! |
244 |
||
245 |
- aTime |
|
242 | 246 |
"return the delta in seconds between 2 times." |
30 | 247 |
|
248 |
^ self getSeconds - (aTime getSeconds) |
|
249 |
! |
|
250 |
||
242 | 251 |
addDays:numberOfDays |
252 |
"return a new instance of myself, numberOfDays afterwards." |
|
253 |
||
254 |
^ self addSeconds:(numberOfDays * (60 * 60 * 24)) |
|
255 |
||
256 |
" |
|
257 |
|t| |
|
258 |
||
259 |
t := AbsoluteTime now. |
|
260 |
t printNL. |
|
261 |
(t addDays:7) printNL |
|
262 |
" |
|
263 |
! |
|
264 |
||
265 |
subtractDays:numberOfDays |
|
266 |
"return a new instance of myself, numberOfDays before." |
|
267 |
||
268 |
^ self subtractSeconds:(numberOfDays * (60 * 60 * 24)) |
|
269 |
||
270 |
" |
|
271 |
|t| |
|
272 |
||
273 |
t := AbsoluteTime now. |
|
274 |
t printNL. |
|
275 |
(t subtractDays:50) printNL |
|
276 |
" |
|
30 | 277 |
! ! |
278 |
||
699 | 279 |
!AbsoluteTime methodsFor:'comparing'! |
280 |
||
281 |
< aTime |
|
282 |
"return true if the argument, aTime is before the receiver" |
|
283 |
||
284 |
|myHi otherHi| |
|
285 |
||
286 |
myHi := self secondsHi. |
|
287 |
otherHi := aTime secondsHi. |
|
288 |
myHi < otherHi ifTrue:[^ true]. |
|
289 |
myHi > otherHi ifTrue:[^ false]. |
|
290 |
^ self secondsLow < aTime secondsLow |
|
291 |
! |
|
292 |
||
293 |
= aTime |
|
294 |
"return true if the argument, aTime represents the same time" |
|
295 |
||
296 |
(aTime species == self species) ifFalse:[^ false]. |
|
297 |
^ (self secondsLow == aTime secondsLow) and:[self secondsHi == aTime secondsHi] |
|
298 |
! |
|
299 |
||
300 |
> aTime |
|
301 |
"return true if the argument, aTime is after the receiver" |
|
302 |
||
303 |
|myHi otherHi| |
|
304 |
||
305 |
myHi := self secondsHi. |
|
306 |
otherHi := aTime secondsHi. |
|
307 |
myHi > otherHi ifTrue:[^ true]. |
|
308 |
myHi < otherHi ifTrue:[^ false]. |
|
309 |
^ self secondsLow > aTime secondsLow |
|
310 |
! |
|
311 |
||
312 |
hash |
|
313 |
"return an integer useful for hashing on times" |
|
314 |
||
315 |
^ self getSeconds |
|
316 |
! ! |
|
317 |
||
318 |
!AbsoluteTime methodsFor:'converting'! |
|
319 |
||
320 |
asAbsoluteTime |
|
321 |
"return an AbsoluteTime object from the receiver - thats the receiver." |
|
322 |
||
323 |
^ self |
|
324 |
! |
|
325 |
||
326 |
asDate |
|
327 |
"return a Date object from the receiver. |
|
328 |
The returned date will only represent the day - not the timeOfDay." |
|
329 |
||
330 |
^ Date fromOSTime:osTime |
|
331 |
||
332 |
" |
|
333 |
AbsoluteTime now |
|
334 |
AbsoluteTime now asDate |
|
335 |
(AbsoluteTime now addTime:3600) asDate |
|
336 |
(AbsoluteTime now addTime:3600) asTime |
|
337 |
AbsoluteTime fromSeconds:(AbsoluteTime now asSeconds + 3600) |
|
338 |
(AbsoluteTime fromSeconds:(AbsoluteTime now asSeconds + 3600)) asDate |
|
339 |
" |
|
340 |
||
341 |
! |
|
342 |
||
343 |
asSeconds |
|
344 |
"return the number of seconds elapsed since whatever time the |
|
345 |
OperatingSystem bases its time upon. Since this is totally |
|
346 |
OS-dependent, do not interpret the value returned by this method. |
|
347 |
You can use it to add/subtract seconds or get time deltas, though." |
|
348 |
||
349 |
^ self getSeconds |
|
350 |
||
351 |
" |
|
352 |
AbsoluteTime now asSeconds |
|
353 |
AbsoluteTime fromSeconds:(AbsoluteTime now asSeconds + 3600) |
|
354 |
Time hour:23 minutes:33 seconds:0 |
|
355 |
Time fromSeconds:((Time hour:23 minutes:33 seconds:0) asSeconds + 3600) |
|
356 |
" |
|
357 |
! |
|
358 |
||
359 |
asTime |
|
360 |
"return a Time object from the receiver. |
|
361 |
The returned time will only represent the timeOfDay - not the day." |
|
362 |
||
363 |
^ Time fromOSTime:osTime |
|
364 |
||
365 |
" |
|
366 |
AbsoluteTime now |
|
367 |
AbsoluteTime now asTime |
|
368 |
(AbsoluteTime now addTime:3600) asTime |
|
369 |
" |
|
370 |
! ! |
|
371 |
||
213 | 372 |
!AbsoluteTime methodsFor:'printing & storing'! |
30 | 373 |
|
241 | 374 |
printOn:aStream |
275 | 375 |
"append a user readable representation of the receiver to aStream. |
376 |
The format is compatible with readFromString:, but not with readFrom:." |
|
377 |
||
241 | 378 |
|h min s d m y| |
30 | 379 |
|
275 | 380 |
OperatingSystem computeDatePartsOf:osTime for:[ |
242 | 381 |
:year :month :day | d := day. m := month. y := year. |
241 | 382 |
]. |
275 | 383 |
OperatingSystem computeTimePartsOf:osTime for:[ |
242 | 384 |
:hours :minutes :secs | h := hours. min := minutes. s := secs. |
241 | 385 |
]. |
386 |
d printOn:aStream. |
|
387 |
aStream nextPut:$-. |
|
388 |
m printOn:aStream. |
|
389 |
aStream nextPut:$-. |
|
390 |
y printOn:aStream. |
|
391 |
aStream space. |
|
392 |
h printOn:aStream leftPaddedTo:2 with:$0. |
|
393 |
aStream nextPut:$:. |
|
394 |
min printOn:aStream leftPaddedTo:2 with:$0. |
|
395 |
aStream nextPut:$:. |
|
396 |
s printOn:aStream leftPaddedTo:2 with:$0. |
|
92 | 397 |
|
398 |
" |
|
275 | 399 |
AbsoluteTime now printOn:Transcript. Transcript cr. |
400 |
(AbsoluteTime fromSeconds:0) printOn:Transcript. Transcript cr. |
|
401 |
Time now printOn:Transcript. Transcript cr. |
|
402 |
Date today printOn:Transcript. Transcript cr. |
|
92 | 403 |
" |
30 | 404 |
! |
405 |
||
241 | 406 |
storeOn:aStream |
275 | 407 |
"store the receiver in a format suitable for reconstruction of the |
408 |
receiver via readFrom:" |
|
409 |
||
241 | 410 |
aStream nextPut:$(; |
242 | 411 |
nextPutAll:self class name; |
275 | 412 |
nextPutAll:' new setSeconds:'. |
413 |
self getSeconds storeOn:aStream. |
|
241 | 414 |
aStream nextPut:$). |
54 | 415 |
|
241 | 416 |
" |
275 | 417 |
AbsoluteTime now storeString |
30 | 418 |
|
275 | 419 |
AbsoluteTime readFrom:(AbsoluteTime now storeString) readStream |
241 | 420 |
" |
30 | 421 |
! ! |
699 | 422 |
|
423 |
!AbsoluteTime methodsFor:'private'! |
|
424 |
||
425 |
fromOSTimeLow:secsLow and:secsHi |
|
426 |
"strictly private: set the seconds from an OS time (since whatever)" |
|
427 |
||
428 |
osTime := Array with:secsLow with:secsHi |
|
429 |
! |
|
430 |
||
431 |
getSeconds |
|
432 |
"strictly private: return the seconds (since whatever)" |
|
433 |
||
434 |
^ ((osTime at:2) * 16r10000) + (osTime at:1) |
|
435 |
! |
|
436 |
||
437 |
secondsHi |
|
438 |
"strictly private: return the hi part of the seconds" |
|
439 |
||
440 |
^ osTime at:2 |
|
441 |
! |
|
442 |
||
443 |
secondsLow |
|
444 |
"strictly private: return the low part of the seconds" |
|
445 |
||
446 |
^ osTime at:1 |
|
447 |
! |
|
448 |
||
449 |
setSeconds:secs |
|
450 |
"strictly private: set the seconds (since whatever)" |
|
451 |
||
452 |
osTime := Array with:(secs // 16r10000) with:(secs \\ 16r10000) |
|
453 |
! |
|
454 |
||
455 |
setSecondsLow:secsLow and:secsHi |
|
456 |
"strictly private: set the seconds (since whatever)" |
|
457 |
||
458 |
osTime := Array with:secsLow with:secsHi |
|
459 |
! ! |
|
460 |
||
461 |
!AbsoluteTime class methodsFor:'documentation'! |
|
462 |
||
463 |
version |
|
464 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/AbsTime.st,v 1.18 1995-12-07 21:31:29 cg Exp $' |
|
465 |
! ! |