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