author | md |
Thu, 08 Nov 2001 17:04:46 +0100 | |
changeset 6164 | 8335251d4cf9 |
parent 6163 | 78087543714b |
child 6165 | 0ac589c3d92a |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
6164 | 3 |
All Rights Reserved |
1 | 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 |
||
6163 | 13 |
"{ Package: '__NoProject__' }" |
5390 | 14 |
|
6163 | 15 |
Time subclass:#TimeDuration |
6164 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Magnitude-General' |
|
1 | 20 |
! |
21 |
||
6163 | 22 |
!TimeDuration class methodsFor:'documentation'! |
88 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
6164 | 27 |
All Rights Reserved |
88 | 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 |
Instances of time represent a particular time-of-day. |
|
409 | 41 |
Since they only store hours, minutes and seconds within a day, |
42 |
they cannot be used to compare times across midnight |
|
43 |
(i.e. they should not be used as timeStamps). |
|
1228 | 44 |
|
241 | 45 |
Use instances of AbsoluteTime (and read the comment there) to do this. |
46 |
||
47 |
Examples: |
|
1228 | 48 |
|t| |
241 | 49 |
|
1228 | 50 |
t := Time now. |
1422 | 51 |
Transcript showCR:t. |
241 | 52 |
|
53 |
||
1228 | 54 |
|t1 t2| |
241 | 55 |
|
1228 | 56 |
t1 := Time now. |
57 |
(Delay forSeconds:10) wait. |
|
58 |
t2 := Time now. |
|
59 |
t2 - t1 |
|
1295 | 60 |
|
61 |
[author:] |
|
62 |
Claus Gittinger |
|
63 |
||
64 |
[see also:] |
|
1302 | 65 |
Date AbsoluteTime AbstractTime OperatingSystem |
1295 | 66 |
Filename |
88 | 67 |
" |
68 |
! ! |
|
1 | 69 |
|
6163 | 70 |
!TimeDuration class methodsFor:'instance creation'! |
4411
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
71 |
|
6163 | 72 |
hours:h minutes:m seconds:s millis:millis |
4031 | 73 |
"return an instance of Time representing the given time. |
74 |
See also Time now / Date today / AbsoluteTime now." |
|
75 |
||
6163 | 76 |
^ self basicNew setHours:h minutes:m seconds:s millis:millis |
4031 | 77 |
|
78 |
" |
|
6163 | 79 |
TimeDuration hours:2 minutes:33 seconds:0 millis:123 |
95 | 80 |
" |
1 | 81 |
! ! |
82 |
||
6163 | 83 |
!TimeDuration class methodsFor:'format strings'! |
4411
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
84 |
|
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
85 |
formatString12us |
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
86 |
"return the format string used to format US times (and other areas)" |
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
87 |
|
6163 | 88 |
^ '%u:%m:%s.%i %a' |
4411
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
89 |
! |
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
90 |
|
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
91 |
formatString24 |
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
92 |
"return the format string used to format european times (and other areas)" |
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
93 |
|
6164 | 94 |
^ '%h:%m:%s' |
95 |
! ! |
|
96 |
||
97 |
!Time methodsFor:'Compatibility - Squeak'! |
|
98 |
||
99 |
intervalString |
|
100 |
"Treat the time as a difference. |
|
101 |
Give it in hours and minutes with two digits of accuracy." |
|
102 |
||
103 |
|hours minutes seconds hh mm ss | |
|
104 |
||
105 |
hours := self hours. |
|
106 |
minutes := self minutes. |
|
107 |
seconds := self seconds. |
|
108 |
hh := hours = 0 ifTrue: [''] ifFalse: [' ', hours printString, ' hours']. |
|
109 |
mm := minutes = 0 ifTrue: [''] ifFalse: [' ', minutes printString, ' minutes']. |
|
110 |
ss := seconds = 0 ifTrue: [''] ifFalse: [' ', seconds printString, ' seconds']. |
|
111 |
hh size > 0 ifTrue: [ss := '']. |
|
112 |
^ hh, mm, ss |
|
113 |
||
114 |
" |
|
115 |
(Time fromSeconds:3600) intervalString |
|
116 |
(Time fromSeconds:3605) intervalString |
|
117 |
(Time fromSeconds:5) intervalString |
|
118 |
(Time fromSeconds:65) intervalString |
|
119 |
" |
|
120 |
! |
|
121 |
||
122 |
print24:prnt24Format showSeconds:doSeconds on:aStream |
|
123 |
"print me either US or 24hr format, optionally with |
|
124 |
seconds on a stream" |
|
125 |
||
126 |
|format| |
|
127 |
||
128 |
prnt24Format ifTrue:[ |
|
129 |
doSeconds ifTrue:[ |
|
130 |
format := '%h:%m:%s' |
|
131 |
] ifFalse:[ |
|
132 |
format := '%h:%m' |
|
133 |
]. |
|
134 |
] ifFalse:[ |
|
135 |
"/ US format |
|
136 |
doSeconds ifTrue:[ |
|
137 |
format := '%u:%m:%s %a' |
|
138 |
] ifFalse:[ |
|
139 |
format := '%u:%m %a' |
|
140 |
]. |
|
141 |
]. |
|
142 |
^ self |
|
143 |
printOn:aStream |
|
144 |
format:format. |
|
145 |
||
146 |
" |
|
147 |
Time now print24:true showSeconds:true on:Transcript. Transcript cr. |
|
148 |
Time now print24:false showSeconds:true on:Transcript. Transcript cr. |
|
149 |
Time now print24:true showSeconds:false on:Transcript. Transcript cr. |
|
150 |
Time now print24:false showSeconds:false on:Transcript. Transcript cr. |
|
151 |
" |
|
4411
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
152 |
! ! |
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
153 |
|
6164 | 154 |
!Time methodsFor:'accessing'! |
155 |
||
156 |
day |
|
157 |
"catch day access - Time does not know about it" |
|
158 |
||
159 |
^ self shouldNotImplement |
|
160 |
! |
|
161 |
||
162 |
hours |
|
163 |
"return the number of hours since midnight (i.e. 0..23)" |
|
164 |
||
165 |
^ self getSeconds // 3600 |
|
166 |
||
167 |
" |
|
168 |
Time now hours |
|
169 |
" |
|
170 |
! |
|
275 | 171 |
|
4845 | 172 |
milliseconds |
6164 | 173 |
"time does not keep milliseconds |
174 |
- for compatibility with absoluteTime" |
|
175 |
||
176 |
^ 0 |
|
177 |
! |
|
178 |
||
179 |
minutes |
|
180 |
"return the number of minutes within the hour (i.e. 0..59)" |
|
181 |
||
182 |
^ (self getSeconds \\ 3600) // 60 |
|
183 |
||
184 |
" |
|
185 |
Time now minutes |
|
186 |
" |
|
187 |
! |
|
188 |
||
189 |
month |
|
190 |
"catch month access - Time does not know about it" |
|
191 |
||
192 |
^ self shouldNotImplement |
|
193 |
! |
|
194 |
||
195 |
seconds |
|
196 |
"return the number of seconds within the minute (i.e. 0..59)" |
|
197 |
||
198 |
^ (self getSeconds \\ 3600) \\ 60 |
|
199 |
||
200 |
" |
|
201 |
Time now seconds |
|
202 |
" |
|
203 |
! |
|
204 |
||
205 |
year |
|
206 |
"catch year access - Time does not know about it" |
|
207 |
||
208 |
^ self shouldNotImplement |
|
1 | 209 |
! ! |
210 |
||
6164 | 211 |
!Time methodsFor:'comparing'! |
212 |
||
213 |
< aTime |
|
214 |
"return true if the receiver is before the argument" |
|
215 |
||
216 |
^ timeEncoding < aTime timeEncoding |
|
217 |
! |
|
218 |
||
219 |
= aTime |
|
220 |
"return true if the argument, aTime represents the same timeOfDay" |
|
221 |
||
222 |
aTime class == self class ifTrue:[ |
|
223 |
^ timeEncoding == aTime timeEncoding |
|
224 |
]. |
|
225 |
(aTime species == self species) ifFalse:[^ false]. |
|
226 |
^ self asSeconds == aTime asSeconds |
|
227 |
! |
|
228 |
||
229 |
> aTime |
|
230 |
"return true if the receiver is before the argument" |
|
231 |
||
232 |
^ timeEncoding > aTime timeEncoding |
|
233 |
! |
|
234 |
||
235 |
hash |
|
236 |
"return an integer useful for hashing on times" |
|
237 |
||
238 |
^ timeEncoding |
|
239 |
! ! |
|
240 |
||
241 |
!Time methodsFor:'converting'! |
|
242 |
||
243 |
asAbsoluteTime |
|
244 |
"return an AbsoluteTime object from the receiver. |
|
245 |
The date components are taken from today." |
|
246 |
||
247 |
|today| |
|
248 |
||
249 |
today := Date today. |
|
250 |
^ AbsoluteTime year:today year month:today month day:today day |
|
251 |
hour:(self hours) minute:(self minutes) second:(self seconds) |
|
252 |
millisecond:(self milliseconds) |
|
253 |
" |
|
254 |
Time now asAbsoluteTime |
|
255 |
" |
|
256 |
||
257 |
"Modified: / 13.7.1999 / 12:32:08 / stefan" |
|
258 |
! |
|
259 |
||
260 |
asSeconds |
|
261 |
"return the number of seconds elapsed since midnight" |
|
262 |
||
263 |
^ self getSeconds |
|
264 |
||
265 |
" |
|
266 |
Time now asSeconds |
|
267 |
" |
|
268 |
! |
|
95 | 269 |
|
699 | 270 |
asTime |
271 |
"return a Time object from the receiver - thats the receiver." |
|
272 |
||
6164 | 273 |
^ self |
699 | 274 |
! ! |
275 |
||
6164 | 276 |
!Time methodsFor:'printing & storing'! |
277 |
||
278 |
print12HourFormatOn:aStream |
|
279 |
"append a printed representation of the receiver to aStream. |
|
280 |
Format is hh:mm:ss am/pm (i.e. 12-hour american format)." |
|
281 |
||
282 |
^ self |
|
283 |
printOn:aStream |
|
284 |
format:(self class formatString12us) |
|
285 |
||
286 |
"/ |h m s ampm| |
|
287 |
"/ |
|
288 |
"/ h := self hours. |
|
289 |
"/ |
|
290 |
"/ "/ 0 -> 12 am |
|
291 |
"/ "/ 12 -> 12 pm |
|
292 |
"/ |
|
293 |
"/ h // 12 == 0 ifTrue:[ |
|
294 |
"/ ampm := ' am'. |
|
295 |
"/ ] ifFalse:[ |
|
296 |
"/ ampm := ' pm'. |
|
297 |
"/ ]. |
|
298 |
"/ |
|
299 |
"/ h == 0 ifFalse:[ |
|
300 |
"/ h := h - 1 \\ 12 + 1. |
|
301 |
"/ ]. |
|
302 |
"/ |
|
303 |
"/ h printOn:aStream. |
|
304 |
"/ aStream nextPut:$:. |
|
305 |
"/ m := self minutes. |
|
306 |
"/ (m < 10) ifTrue:[aStream nextPut:$0]. |
|
307 |
"/ m printOn:aStream. |
|
308 |
"/ aStream nextPut:$:. |
|
309 |
"/ s := self seconds. |
|
310 |
"/ (s < 10) ifTrue:[aStream nextPut:$0]. |
|
311 |
"/ s printOn:aStream. |
|
312 |
"/ aStream nextPutAll:ampm |
|
313 |
||
314 |
" |
|
315 |
Time now print12HourFormatOn:Transcript. Transcript cr |
|
316 |
(Time now subtractHours:12) print12HourFormatOn:Transcript. Transcript cr |
|
317 |
(Time hour:24 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
318 |
(Time hour:12 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
319 |
(Time hour:0 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
320 |
0 to:24 do:[:h | |
|
321 |
(Time hour:h minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
322 |
] |
|
323 |
" |
|
324 |
! |
|
325 |
||
326 |
print24HourFormatOn:aStream |
|
327 |
"append a printed representation of the receiver to aStream. |
|
328 |
Format is hh:mm:ss (i.e. 24-hour european format)." |
|
329 |
||
330 |
^ self |
|
331 |
printOn:aStream |
|
332 |
format:(self class formatString24). |
|
333 |
||
334 |
"/ |h m s| |
|
335 |
"/ |
|
336 |
"/ h := self hours. |
|
337 |
"/ (h < 10) ifTrue:[aStream nextPut:$0]. |
|
338 |
"/ h printOn:aStream. |
|
339 |
"/ aStream nextPut:$:. |
|
340 |
"/ m := self minutes. |
|
341 |
"/ (m < 10) ifTrue:[aStream nextPut:$0]. |
|
342 |
"/ m printOn:aStream. |
|
343 |
"/ aStream nextPut:$:. |
|
344 |
"/ s := self seconds. |
|
345 |
"/ (s < 10) ifTrue:[aStream nextPut:$0]. |
|
346 |
"/ s printOn:aStream |
|
347 |
||
348 |
" |
|
349 |
Time now print24HourFormatOn:Transcript. Transcript cr |
|
350 |
" |
|
351 |
! |
|
352 |
||
353 |
printOn:aStream |
|
354 |
"append a user printed representation of the receiver to aStream. |
|
355 |
The format is suitable for a human - not meant to be read back. |
|
356 |
||
357 |
Format is hh:mm:ss either in 12-hour or 24-hour format. |
|
358 |
depending on the setting of LanguageTerritory. |
|
359 |
I dont know what ST-80 does here (12-hour format ?)" |
|
360 |
||
361 |
LanguageTerritory == #us ifTrue:[ |
|
362 |
self print12HourFormatOn:aStream |
|
363 |
] ifFalse:[ |
|
364 |
self print24HourFormatOn:aStream |
|
365 |
] |
|
366 |
||
367 |
" |
|
368 |
Time now printOn:Transcript. Transcript cr |
|
369 |
" |
|
370 |
||
371 |
"Modified: 22.2.1996 / 16:58:30 / cg" |
|
372 |
! |
|
373 |
||
374 |
printString12HourFormat |
|
375 |
"return a printed representation in 12 hour format" |
|
376 |
||
377 |
^ self printStringFormat:(self class formatString12us) |
|
378 |
||
379 |
" |
|
380 |
Time now printString12HourFormat |
|
381 |
" |
|
382 |
! |
|
383 |
||
384 |
printString24HourFormat |
|
385 |
"return a printed representation in 24 hour format" |
|
386 |
||
387 |
^ self printStringFormat:(self class formatString24) |
|
388 |
||
389 |
" |
|
390 |
Time now printString24HourFormat |
|
391 |
" |
|
392 |
||
393 |
! |
|
394 |
||
395 |
shortPrintString |
|
396 |
"dummy - for now" |
|
397 |
||
398 |
^ self printString |
|
399 |
||
400 |
"Created: 20.6.1997 / 17:17:01 / cg" |
|
401 |
! ! |
|
402 |
||
403 |
!Time methodsFor:'private'! |
|
404 |
||
405 |
fromOSTime:osTime |
|
406 |
"set my time, given an osTime" |
|
407 |
||
408 |
|h m s| |
|
409 |
||
410 |
OperatingSystem computeTimePartsOf:osTime for:[ |
|
411 |
:hours :minutes :secs :millis | |
|
412 |
||
413 |
h := hours. |
|
414 |
m := minutes. |
|
415 |
s := secs. |
|
416 |
]. |
|
417 |
self setHours:h minutes:m seconds:s |
|
418 |
||
419 |
"Modified: 1.7.1996 / 15:21:06 / cg" |
|
420 |
! |
|
275 | 421 |
|
3477 | 422 |
getMilliseconds |
6046 | 423 |
"return the number of milliseconds since midnight" |
424 |
||
6163 | 425 |
^ timeEncoding |
3477 | 426 |
! |
427 |
||
275 | 428 |
getSeconds |
6046 | 429 |
"return the number of seconds since midnight" |
430 |
||
6163 | 431 |
^ timeEncoding // 1000 |
275 | 432 |
! |
433 |
||
6163 | 434 |
setHours:h minutes:m seconds:s millis:millis |
699 | 435 |
"set my time given individual values" |
436 |
||
6163 | 437 |
self setMilliseconds:(((h\\24) * 60 * 60 ) + (m * 60) + s) * 1000 + millis. |
3477 | 438 |
! |
439 |
||
440 |
setMilliseconds:millis |
|
441 |
"set my time given milliseconds since midnight" |
|
442 |
||
6163 | 443 |
timeEncoding := millis |
699 | 444 |
! |
445 |
||
275 | 446 |
setSeconds:secs |
447 |
"set my time given seconds since midnight" |
|
448 |
||
6163 | 449 |
self setMilliseconds:(secs * 1000). |
699 | 450 |
! ! |
451 |
||
6163 | 452 |
!TimeDuration class methodsFor:'documentation'! |
241 | 453 |
|
699 | 454 |
version |
6164 | 455 |
^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.51 2001-11-08 16:04:46 md Exp $' |
241 | 456 |
! ! |