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