author | Claus Gittinger <cg@exept.de> |
Fri, 20 Jun 1997 17:29:38 +0200 | |
changeset 2706 | 878723bc99fc |
parent 1701 | 80d13adb2e77 |
child 3477 | ac8613ab02c9 |
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]. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
99 |
hour := Integer readFrom:str onError:ex. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
100 |
(hour between:0 and:24) ifFalse:[ex value]. |
275 | 101 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
102 |
[str peek isDigit] whileFalse:[str next]. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
103 |
min := Integer readFrom:str onError:ex. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
104 |
(min between:0 and:59) ifFalse:[ex value]. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
105 |
|
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
106 |
[str peek isDigit] whileFalse:[str next]. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
107 |
sec := Integer readFrom:str onError:ex. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
108 |
(sec between:0 and:59) ifFalse:[ex value]. |
275 | 109 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
110 |
[str peek == Character space] whileTrue:[str next]. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
111 |
(str peek == $p) ifTrue:[ |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
112 |
"pm" |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
113 |
hour := hour + 12 |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
114 |
]. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
115 |
^ self basicNew setHour:hour minutes:min seconds:sec |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
116 |
] |
216 | 117 |
|
239 | 118 |
" |
409 | 119 |
Time readFrom:'18:22:00' |
120 |
Time readFrom:'14:00:11' |
|
121 |
Time readFrom:'7:00:11' |
|
122 |
Time readFrom:'6:22:00 pm' |
|
123 |
Time readFrom:'2:00:11 pm' |
|
124 |
Time readFrom:'7:00:11 am' |
|
239 | 125 |
" |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
126 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1502
diff
changeset
|
127 |
"Modified: 8.10.1996 / 19:32:11 / cg" |
1 | 128 |
! ! |
129 |
||
95 | 130 |
!Time methodsFor:'accessing'! |
77 | 131 |
|
699 | 132 |
day |
133 |
"catch day access - Time does not know about it" |
|
77 | 134 |
|
699 | 135 |
^ self shouldNotImplement |
1 | 136 |
! |
137 |
||
241 | 138 |
hours |
139 |
"return the number of hours since midnight (i.e. 0..23)" |
|
95 | 140 |
|
241 | 141 |
^ timeEncoding // 3600 |
142 |
||
143 |
" |
|
144 |
Time now hours |
|
145 |
" |
|
275 | 146 |
! |
147 |
||
699 | 148 |
minutes |
149 |
"return the number of minutes within the hour (i.e. 0..59)" |
|
275 | 150 |
|
699 | 151 |
^ (timeEncoding \\ 3600) // 60 |
152 |
||
153 |
" |
|
154 |
Time now minutes |
|
155 |
" |
|
275 | 156 |
! |
157 |
||
158 |
month |
|
159 |
"catch month access - Time does not know about it" |
|
160 |
||
161 |
^ self shouldNotImplement |
|
162 |
! |
|
163 |
||
699 | 164 |
seconds |
165 |
"return the number of seconds within the minute (i.e. 0..59)" |
|
166 |
||
167 |
^ (timeEncoding \\ 3600) \\ 60 |
|
168 |
||
169 |
" |
|
170 |
Time now seconds |
|
171 |
" |
|
172 |
! |
|
173 |
||
275 | 174 |
year |
175 |
"catch year access - Time does not know about it" |
|
176 |
||
177 |
^ self shouldNotImplement |
|
1 | 178 |
! ! |
179 |
||
699 | 180 |
!Time methodsFor:'comparing'! |
181 |
||
182 |
< aTime |
|
183 |
"return true if the argument, aTime is before the receiver" |
|
184 |
||
185 |
^ timeEncoding < aTime timeEncoding |
|
186 |
! |
|
1 | 187 |
|
699 | 188 |
= aTime |
189 |
"return true if the argument, aTime represents the same timeOfDay" |
|
190 |
||
191 |
aTime class == self class ifTrue:[ |
|
192 |
^ timeEncoding == aTime timeEncoding |
|
193 |
]. |
|
194 |
(aTime species == self species) ifFalse:[^ false]. |
|
195 |
^ self asSeconds == aTime asSeconds |
|
196 |
! |
|
197 |
||
198 |
> aTime |
|
199 |
"return true if the argument, aTime is after the receiver" |
|
95 | 200 |
|
699 | 201 |
^ timeEncoding > aTime timeEncoding |
202 |
! |
|
203 |
||
204 |
hash |
|
205 |
"return an integer useful for hashing on times" |
|
206 |
||
207 |
^ timeEncoding |
|
208 |
! ! |
|
209 |
||
210 |
!Time methodsFor:'converting'! |
|
95 | 211 |
|
699 | 212 |
asAbsoluteTime |
213 |
"return an AbsoluteTime object from the receiver. |
|
214 |
The date components are taken from today." |
|
215 |
||
216 |
|today| |
|
217 |
||
218 |
today := Date today. |
|
219 |
^ AbsoluteTime day:today day month:today month year:today year |
|
220 |
hour:self hours minutes:self minutes seconds:self seconds |
|
95 | 221 |
" |
699 | 222 |
Time now asAbsoluteTime |
95 | 223 |
" |
62 | 224 |
! |
225 |
||
699 | 226 |
asSeconds |
227 |
"return the number of seconds elapsed since midnight" |
|
1 | 228 |
|
699 | 229 |
^ timeEncoding |
95 | 230 |
|
231 |
" |
|
699 | 232 |
Time now asSeconds |
95 | 233 |
" |
234 |
! |
|
235 |
||
699 | 236 |
asTime |
237 |
"return a Time object from the receiver - thats the receiver." |
|
238 |
||
239 |
^ self |
|
240 |
! ! |
|
241 |
||
242 |
!Time methodsFor:'printing & storing'! |
|
243 |
||
95 | 244 |
print12HourFormatOn:aStream |
245 |
"append a printed representation of the receiver to aStream. |
|
241 | 246 |
Format is hh:mm:ss am/pm (i.e. 12-hour american format)." |
95 | 247 |
|
409 | 248 |
|h m s ampm| |
95 | 249 |
|
241 | 250 |
h := self hours. |
251 |
h > 12 ifTrue:[ |
|
275 | 252 |
h := h - 12. |
241 | 253 |
] ifFalse:[ |
275 | 254 |
h < 1 ifTrue:[ |
255 |
h := 12 |
|
256 |
] |
|
241 | 257 |
]. |
258 |
h printOn:aStream. |
|
259 |
aStream nextPut:$:. |
|
260 |
m := self minutes. |
|
261 |
(m < 10) ifTrue:[aStream nextPut:$0]. |
|
262 |
m printOn:aStream. |
|
263 |
aStream nextPut:$:. |
|
264 |
s := self seconds. |
|
265 |
(s < 10) ifTrue:[aStream nextPut:$0]. |
|
409 | 266 |
s printOn:aStream. |
241 | 267 |
h >= 12 ifTrue:[ |
409 | 268 |
ampm := ' am' |
241 | 269 |
] ifFalse:[ |
409 | 270 |
ampm := ' pm' |
271 |
]. |
|
272 |
aStream nextPutAll:ampm |
|
95 | 273 |
|
274 |
" |
|
216 | 275 |
Time now print12HourFormatOn:Transcript. Transcript cr |
95 | 276 |
" |
699 | 277 |
! |
241 | 278 |
|
699 | 279 |
print24HourFormatOn:aStream |
280 |
"append a printed representation of the receiver to aStream. |
|
281 |
Format is hh:mm:ss (i.e. 24-hour european format)." |
|
282 |
||
283 |
|h m s| |
|
241 | 284 |
|
699 | 285 |
h := self hours. |
286 |
(h < 10) ifTrue:[aStream nextPut:$0]. |
|
287 |
h printOn:aStream. |
|
288 |
aStream nextPut:$:. |
|
289 |
m := self minutes. |
|
290 |
(m < 10) ifTrue:[aStream nextPut:$0]. |
|
291 |
m printOn:aStream. |
|
292 |
aStream nextPut:$:. |
|
293 |
s := self seconds. |
|
294 |
(s < 10) ifTrue:[aStream nextPut:$0]. |
|
295 |
s printOn:aStream |
|
241 | 296 |
|
297 |
" |
|
699 | 298 |
Time now print24HourFormatOn:Transcript. Transcript cr |
241 | 299 |
" |
275 | 300 |
! |
301 |
||
699 | 302 |
printOn:aStream |
303 |
"append a printed representation of the receiver to aStream. |
|
304 |
Format is hh:mm:ss either in 12-hour or 24-hour format. |
|
305 |
depending on the setting of LanguageTerritory. |
|
306 |
I dont know what ST-80 does here (12-hour format ?)" |
|
241 | 307 |
|
993
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
308 |
LanguageTerritory == #us ifTrue:[ |
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
309 |
self print12HourFormatOn:aStream |
699 | 310 |
] ifFalse:[ |
993
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
311 |
self print24HourFormatOn:aStream |
699 | 312 |
] |
275 | 313 |
|
699 | 314 |
" |
315 |
Time now printOn:Transcript. Transcript cr |
|
316 |
" |
|
993
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
317 |
|
feaa62827c41
check for territory being #us (instead of #usa) when printing
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
318 |
"Modified: 22.2.1996 / 16:58:30 / cg" |
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
319 |
! |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
320 |
|
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
321 |
shortPrintString |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
322 |
"dummy - for now" |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
323 |
|
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
324 |
^ self printString |
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
325 |
|
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
326 |
"Created: 20.6.1997 / 17:17:01 / cg" |
241 | 327 |
! ! |
328 |
||
329 |
!Time methodsFor:'private'! |
|
330 |
||
1502 | 331 |
fromOSTime:osTime |
332 |
"set my time, given an osTime" |
|
275 | 333 |
|
334 |
|h m s| |
|
1502 | 335 |
|
336 |
OperatingSystem computeTimePartsOf:osTime for:[ |
|
337 |
:hours :minutes :secs :millis | |
|
275 | 338 |
|
1502 | 339 |
h := hours. |
340 |
m := minutes. |
|
341 |
s := secs. |
|
275 | 342 |
]. |
343 |
self setHour:h minutes:m seconds:s |
|
344 |
||
1502 | 345 |
"Modified: 1.7.1996 / 15:21:06 / cg" |
275 | 346 |
! |
347 |
||
348 |
getSeconds |
|
349 |
^ timeEncoding |
|
350 |
! |
|
351 |
||
699 | 352 |
setHour:h minutes:m seconds:s |
353 |
"set my time given individual values" |
|
354 |
||
355 |
timeEncoding := ((h\\24) * 60 * 60 ) + (m * 60) + s. |
|
356 |
! |
|
357 |
||
275 | 358 |
setSeconds:secs |
359 |
"set my time given seconds since midnight" |
|
360 |
||
361 |
timeEncoding := secs |
|
362 |
! |
|
363 |
||
699 | 364 |
timeEncoding |
365 |
"the internal encoding is stricktly private, |
|
366 |
and should not be used outside." |
|
275 | 367 |
|
699 | 368 |
^ timeEncoding |
275 | 369 |
! |
370 |
||
241 | 371 |
timeEncoding:encoding |
372 |
"the internal encoding is stricktly private, |
|
373 |
and should not be used outside." |
|
374 |
||
375 |
timeEncoding := encoding |
|
699 | 376 |
! ! |
377 |
||
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
378 |
!Time class methodsFor:'documentation'! |
241 | 379 |
|
699 | 380 |
version |
2706
878723bc99fc
added #shortPrintString for ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1701
diff
changeset
|
381 |
^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.31 1997-06-20 15:29:38 cg Exp $' |
241 | 382 |
! ! |