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