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