author | Claus Gittinger <cg@exept.de> |
Fri, 22 May 1998 12:55:12 +0200 | |
changeset 3477 | ac8613ab02c9 |
parent 2706 | 878723bc99fc |
child 3478 | 3f1a83b35865 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
159 | 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 |
||
275 | 13 |
AbstractTime subclass:#Time |
993
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
14 |
instanceVariableNames:'timeEncoding' |
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
15 |
classVariableNames:'' |
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
16 |
poolDictionaries:'' |
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
17 |
category:'Magnitude-General' |
1 | 18 |
! |
19 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
20 |
!Time class methodsFor:'documentation'! |
88 | 21 |
|
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
159 | 25 |
All Rights Reserved |
88 | 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 |
Instances of time represent a particular time-of-day. |
|
409 | 39 |
Since they only store hours, minutes and seconds within a day, |
40 |
they cannot be used to compare times across midnight |
|
41 |
(i.e. they should not be used as timeStamps). |
|
1228 | 42 |
|
241 | 43 |
Use instances of AbsoluteTime (and read the comment there) to do this. |
44 |
||
45 |
Examples: |
|
1228 | 46 |
|t| |
241 | 47 |
|
1228 | 48 |
t := Time now. |
1422 | 49 |
Transcript showCR:t. |
241 | 50 |
|
51 |
||
1228 | 52 |
|t1 t2| |
241 | 53 |
|
1228 | 54 |
t1 := Time now. |
55 |
(Delay forSeconds:10) wait. |
|
56 |
t2 := Time now. |
|
57 |
t2 - t1 |
|
1295 | 58 |
|
59 |
[author:] |
|
60 |
Claus Gittinger |
|
61 |
||
62 |
[see also:] |
|
1302 | 63 |
Date AbsoluteTime AbstractTime OperatingSystem |
1295 | 64 |
Filename |
88 | 65 |
" |
66 |
! ! |
|
1 | 67 |
|
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
68 |
!Time class methodsFor:'instance creation'! |
1 | 69 |
|
239 | 70 |
hour:h minutes:m seconds:s |
1228 | 71 |
"return an instance of Time representing the given time. |
72 |
See also Time now / Date today / AbsoluteTime now." |
|
239 | 73 |
|
275 | 74 |
^ self basicNew setHour:h minutes:m seconds:s |
1 | 75 |
|
239 | 76 |
" |
77 |
Time hour:2 minutes:33 seconds:0 |
|
241 | 78 |
Time hour:0 minutes:0 seconds:0 |
79 |
Time hour:24 minutes:0 seconds:0 |
|
80 |
Time hour:23 minutes:59 seconds:59 |
|
95 | 81 |
" |
1228 | 82 |
|
83 |
"Modified: 19.4.1996 / 15:32:40 / cg" |
|
77 | 84 |
! |
85 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
86 |
readFrom:aStringOrStream onError:exceptionBlock |
241 | 87 |
"return a new Time, reading a printed representation from aStream. |
88 |
If no pm follows the time, the string is interpreted as either 24 hour format |
|
409 | 89 |
or being am." |
239 | 90 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
91 |
ErrorSignal handle:[:ex | |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
92 |
^ exceptionBlock value |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
93 |
] do:[ |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
94 |
|str hour min sec ex| |
216 | 95 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
96 |
str := aStringOrStream readStream. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
97 |
|
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
98 |
ex := [^ exceptionBlock value]. |
3477 | 99 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
100 |
hour := Integer readFrom:str onError:ex. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
101 |
(hour between:0 and:24) ifFalse:[ex value]. |
275 | 102 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
103 |
[str peek isDigit] whileFalse:[str next]. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
104 |
min := Integer readFrom:str onError:ex. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
105 |
(min between:0 and:59) ifFalse:[ex value]. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
106 |
|
3477 | 107 |
str atEnd ifFalse:[ |
108 |
[str peek isDigit] whileFalse:[str next]. |
|
109 |
sec := Integer readFrom:str onError:ex. |
|
110 |
(sec between:0 and:59) ifFalse:[ex value]. |
|
275 | 111 |
|
3477 | 112 |
[str peek == Character space] whileTrue:[str next]. |
113 |
(str peek == $p) ifTrue:[ |
|
114 |
"pm" |
|
115 |
hour := hour + 12 |
|
116 |
]. |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
117 |
]. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
118 |
^ self basicNew setHour:hour minutes:min seconds:sec |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
119 |
] |
216 | 120 |
|
239 | 121 |
" |
409 | 122 |
Time readFrom:'18:22:00' |
123 |
Time readFrom:'14:00:11' |
|
124 |
Time readFrom:'7:00:11' |
|
125 |
Time readFrom:'6:22:00 pm' |
|
126 |
Time readFrom:'2:00:11 pm' |
|
127 |
Time readFrom:'7:00:11 am' |
|
239 | 128 |
" |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
129 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
130 |
"Modified: 8.10.1996 / 19:32:11 / cg" |
1 | 131 |
! ! |
132 |
||
95 | 133 |
!Time methodsFor:'accessing'! |
77 | 134 |
|
699 | 135 |
day |
136 |
"catch day access - Time does not know about it" |
|
77 | 137 |
|
699 | 138 |
^ self shouldNotImplement |
1 | 139 |
! |
140 |
||
241 | 141 |
hours |
142 |
"return the number of hours since midnight (i.e. 0..23)" |
|
95 | 143 |
|
241 | 144 |
^ timeEncoding // 3600 |
145 |
||
146 |
" |
|
147 |
Time now hours |
|
148 |
" |
|
275 | 149 |
! |
150 |
||
699 | 151 |
minutes |
152 |
"return the number of minutes within the hour (i.e. 0..59)" |
|
275 | 153 |
|
699 | 154 |
^ (timeEncoding \\ 3600) // 60 |
155 |
||
156 |
" |
|
157 |
Time now minutes |
|
158 |
" |
|
275 | 159 |
! |
160 |
||
161 |
month |
|
162 |
"catch month access - Time does not know about it" |
|
163 |
||
164 |
^ self shouldNotImplement |
|
165 |
! |
|
166 |
||
699 | 167 |
seconds |
168 |
"return the number of seconds within the minute (i.e. 0..59)" |
|
169 |
||
170 |
^ (timeEncoding \\ 3600) \\ 60 |
|
171 |
||
172 |
" |
|
173 |
Time now seconds |
|
174 |
" |
|
175 |
! |
|
176 |
||
275 | 177 |
year |
178 |
"catch year access - Time does not know about it" |
|
179 |
||
180 |
^ self shouldNotImplement |
|
1 | 181 |
! ! |
182 |
||
699 | 183 |
!Time methodsFor:'comparing'! |
184 |
||
185 |
< aTime |
|
186 |
"return true if the argument, aTime is before the receiver" |
|
187 |
||
188 |
^ timeEncoding < aTime timeEncoding |
|
189 |
! |
|
1 | 190 |
|
699 | 191 |
= aTime |
192 |
"return true if the argument, aTime represents the same timeOfDay" |
|
193 |
||
194 |
aTime class == self class ifTrue:[ |
|
195 |
^ timeEncoding == aTime timeEncoding |
|
196 |
]. |
|
197 |
(aTime species == self species) ifFalse:[^ false]. |
|
198 |
^ self asSeconds == aTime asSeconds |
|
199 |
! |
|
200 |
||
201 |
> aTime |
|
202 |
"return true if the argument, aTime is after the receiver" |
|
95 | 203 |
|
699 | 204 |
^ timeEncoding > aTime timeEncoding |
205 |
! |
|
206 |
||
207 |
hash |
|
208 |
"return an integer useful for hashing on times" |
|
209 |
||
210 |
^ timeEncoding |
|
211 |
! ! |
|
212 |
||
213 |
!Time methodsFor:'converting'! |
|
95 | 214 |
|
699 | 215 |
asAbsoluteTime |
216 |
"return an AbsoluteTime object from the receiver. |
|
217 |
The date components are taken from today." |
|
218 |
||
219 |
|today| |
|
220 |
||
221 |
today := Date today. |
|
222 |
^ AbsoluteTime day:today day month:today month year:today year |
|
223 |
hour:self hours minutes:self minutes seconds:self seconds |
|
95 | 224 |
" |
699 | 225 |
Time now asAbsoluteTime |
95 | 226 |
" |
62 | 227 |
! |
228 |
||
699 | 229 |
asSeconds |
230 |
"return the number of seconds elapsed since midnight" |
|
1 | 231 |
|
699 | 232 |
^ timeEncoding |
95 | 233 |
|
234 |
" |
|
699 | 235 |
Time now asSeconds |
95 | 236 |
" |
237 |
! |
|
238 |
||
699 | 239 |
asTime |
240 |
"return a Time object from the receiver - thats the receiver." |
|
241 |
||
242 |
^ self |
|
243 |
! ! |
|
244 |
||
245 |
!Time methodsFor:'printing & storing'! |
|
246 |
||
95 | 247 |
print12HourFormatOn:aStream |
248 |
"append a printed representation of the receiver to aStream. |
|
241 | 249 |
Format is hh:mm:ss am/pm (i.e. 12-hour american format)." |
95 | 250 |
|
409 | 251 |
|h m s ampm| |
95 | 252 |
|
3477 | 253 |
ampm := ' am'. |
241 | 254 |
h := self hours. |
3477 | 255 |
|
256 |
"/ 0 -> 12 am |
|
257 |
"/ 12 -> 12 pm |
|
258 |
||
241 | 259 |
h > 12 ifTrue:[ |
3477 | 260 |
h := h - 12. |
261 |
ampm := ' pm'. |
|
241 | 262 |
] ifFalse:[ |
3477 | 263 |
h == 0 ifTrue:[ |
264 |
h := 12 |
|
265 |
] ifFalse:[ |
|
266 |
h == 12 ifTrue:[ |
|
267 |
ampm := ' pm' |
|
268 |
] |
|
269 |
] |
|
241 | 270 |
]. |
271 |
h printOn:aStream. |
|
272 |
aStream nextPut:$:. |
|
273 |
m := self minutes. |
|
274 |
(m < 10) ifTrue:[aStream nextPut:$0]. |
|
275 |
m printOn:aStream. |
|
276 |
aStream nextPut:$:. |
|
277 |
s := self seconds. |
|
278 |
(s < 10) ifTrue:[aStream nextPut:$0]. |
|
409 | 279 |
s printOn:aStream. |
280 |
aStream nextPutAll:ampm |
|
95 | 281 |
|
282 |
" |
|
216 | 283 |
Time now print12HourFormatOn:Transcript. Transcript cr |
3477 | 284 |
(Time now subtractHours:12) print12HourFormatOn:Transcript. Transcript cr |
285 |
(Time hour:24 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
286 |
(Time hour:12 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
287 |
(Time hour:0 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
95 | 288 |
" |
699 | 289 |
! |
241 | 290 |
|
699 | 291 |
print24HourFormatOn:aStream |
292 |
"append a printed representation of the receiver to aStream. |
|
293 |
Format is hh:mm:ss (i.e. 24-hour european format)." |
|
294 |
||
295 |
|h m s| |
|
241 | 296 |
|
699 | 297 |
h := self hours. |
298 |
(h < 10) ifTrue:[aStream nextPut:$0]. |
|
299 |
h printOn:aStream. |
|
300 |
aStream nextPut:$:. |
|
301 |
m := self minutes. |
|
302 |
(m < 10) ifTrue:[aStream nextPut:$0]. |
|
303 |
m printOn:aStream. |
|
304 |
aStream nextPut:$:. |
|
305 |
s := self seconds. |
|
306 |
(s < 10) ifTrue:[aStream nextPut:$0]. |
|
307 |
s printOn:aStream |
|
241 | 308 |
|
309 |
" |
|
699 | 310 |
Time now print24HourFormatOn:Transcript. Transcript cr |
241 | 311 |
" |
275 | 312 |
! |
313 |
||
699 | 314 |
printOn:aStream |
315 |
"append a printed representation of the receiver to aStream. |
|
316 |
Format is hh:mm:ss either in 12-hour or 24-hour format. |
|
317 |
depending on the setting of LanguageTerritory. |
|
318 |
I dont know what ST-80 does here (12-hour format ?)" |
|
241 | 319 |
|
993
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
320 |
LanguageTerritory == #us ifTrue:[ |
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
321 |
self print12HourFormatOn:aStream |
699 | 322 |
] ifFalse:[ |
993
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
323 |
self print24HourFormatOn:aStream |
699 | 324 |
] |
275 | 325 |
|
699 | 326 |
" |
327 |
Time now printOn:Transcript. Transcript cr |
|
328 |
" |
|
993
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
329 |
|
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
330 |
"Modified: 22.2.1996 / 16:58:30 / cg" |
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
331 |
! |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
332 |
|
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
333 |
shortPrintString |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
334 |
"dummy - for now" |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
335 |
|
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
336 |
^ self printString |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
337 |
|
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
338 |
"Created: 20.6.1997 / 17:17:01 / cg" |
241 | 339 |
! ! |
340 |
||
341 |
!Time methodsFor:'private'! |
|
342 |
||
1502 | 343 |
fromOSTime:osTime |
344 |
"set my time, given an osTime" |
|
275 | 345 |
|
346 |
|h m s| |
|
1502 | 347 |
|
348 |
OperatingSystem computeTimePartsOf:osTime for:[ |
|
349 |
:hours :minutes :secs :millis | |
|
275 | 350 |
|
1502 | 351 |
h := hours. |
352 |
m := minutes. |
|
353 |
s := secs. |
|
275 | 354 |
]. |
355 |
self setHour:h minutes:m seconds:s |
|
356 |
||
1502 | 357 |
"Modified: 1.7.1996 / 15:21:06 / cg" |
275 | 358 |
! |
359 |
||
3477 | 360 |
getMilliseconds |
361 |
^ self getSeconds * 1000 |
|
362 |
! |
|
363 |
||
275 | 364 |
getSeconds |
365 |
^ timeEncoding |
|
366 |
! |
|
367 |
||
699 | 368 |
setHour:h minutes:m seconds:s |
369 |
"set my time given individual values" |
|
370 |
||
3477 | 371 |
self setSeconds:(((h\\24) * 60 * 60 ) + (m * 60) + s). |
372 |
! |
|
373 |
||
374 |
setMilliseconds:millis |
|
375 |
"set my time given milliseconds since midnight" |
|
376 |
||
377 |
self setSeconds:(millis // 1000) |
|
699 | 378 |
! |
379 |
||
275 | 380 |
setSeconds:secs |
381 |
"set my time given seconds since midnight" |
|
382 |
||
3477 | 383 |
secs < 0 ifTrue:[ |
384 |
timeEncoding := (24 * 3600) - (secs negated \\ (24 * 3600)) |
|
385 |
] ifFalse:[ |
|
386 |
timeEncoding := secs |
|
387 |
] |
|
275 | 388 |
! |
389 |
||
699 | 390 |
timeEncoding |
391 |
"the internal encoding is stricktly private, |
|
392 |
and should not be used outside." |
|
275 | 393 |
|
699 | 394 |
^ timeEncoding |
275 | 395 |
! |
396 |
||
241 | 397 |
timeEncoding:encoding |
398 |
"the internal encoding is stricktly private, |
|
399 |
and should not be used outside." |
|
400 |
||
401 |
timeEncoding := encoding |
|
699 | 402 |
! ! |
403 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
404 |
!Time class methodsFor:'documentation'! |
241 | 405 |
|
699 | 406 |
version |
3477 | 407 |
^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.32 1998-05-22 10:55:12 cg Exp $' |
241 | 408 |
! ! |