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