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