author | Claus Gittinger <cg@exept.de> |
Fri, 17 Nov 1995 00:28:50 +0100 | |
changeset 569 | 7134eb78cf48 |
parent 530 | 07d0bce293c9 |
child 699 | 12f456343eea |
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 |
||
241 | 13 |
'From Smalltalk/X, Version:2.10.4 on 8-feb-1995 at 12:46:41 pm'! |
14 |
||
275 | 15 |
AbstractTime subclass:#Time |
241 | 16 |
instanceVariableNames:'timeEncoding' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Magnitude-General' |
|
1 | 20 |
! |
21 |
||
88 | 22 |
!Time class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
159 | 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 |
version |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
39 |
^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.22 1995-11-16 23:28:50 cg Exp $' |
88 | 40 |
! |
41 |
||
42 |
documentation |
|
43 |
" |
|
44 |
Instances of time represent a particular time-of-day. |
|
409 | 45 |
Since they only store hours, minutes and seconds within a day, |
46 |
they cannot be used to compare times across midnight |
|
47 |
(i.e. they should not be used as timeStamps). |
|
241 | 48 |
Use instances of AbsoluteTime (and read the comment there) to do this. |
49 |
||
50 |
Examples: |
|
275 | 51 |
|t| |
241 | 52 |
|
275 | 53 |
t := Time now. |
54 |
Transcript showCr:t. |
|
241 | 55 |
|
56 |
||
275 | 57 |
|t1 t2| |
241 | 58 |
|
275 | 59 |
t1 := Time now. |
60 |
(Delay forSeconds:10) wait. |
|
61 |
t2 := Time now. |
|
62 |
t2 - t1 |
|
88 | 63 |
" |
64 |
! ! |
|
1 | 65 |
|
66 |
!Time class methodsFor:'instance creation'! |
|
67 |
||
239 | 68 |
hour:h minutes:m seconds:s |
69 |
"return an instance of Time representing the given values" |
|
70 |
||
275 | 71 |
^ self basicNew setHour:h minutes:m seconds:s |
1 | 72 |
|
239 | 73 |
" |
74 |
Time hour:2 minutes:33 seconds:0 |
|
241 | 75 |
Time hour:0 minutes:0 seconds:0 |
76 |
Time hour:24 minutes:0 seconds:0 |
|
77 |
Time hour:23 minutes:59 seconds:59 |
|
95 | 78 |
" |
77 | 79 |
! |
80 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
81 |
readFrom:aStringOrStream onError:exceptionBlock |
241 | 82 |
"return a new Time, reading a printed representation from aStream. |
83 |
If no pm follows the time, the string is interpreted as either 24 hour format |
|
409 | 84 |
or being am." |
239 | 85 |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
86 |
|str hour min sec ex| |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
87 |
|
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
88 |
str := aStringOrStream readStream. |
216 | 89 |
|
275 | 90 |
ex := [^ exceptionBlock value]. |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
91 |
hour := Integer readFrom:str onError:ex. |
275 | 92 |
(hour between:0 and:24) ifFalse:[ex value]. |
93 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
94 |
[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
|
95 |
min := Integer readFrom:str onError:ex. |
275 | 96 |
(min between:0 and:59) ifFalse:[ex value]. |
97 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
98 |
[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
|
99 |
sec := Integer readFrom:str onError:ex. |
275 | 100 |
(sec between:0 and:59) ifFalse:[ex value]. |
101 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
102 |
[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
|
103 |
(str peek == $p) ifTrue:[ |
275 | 104 |
"pm" |
105 |
hour := hour + 12 |
|
239 | 106 |
]. |
275 | 107 |
^ self basicNew setHour:hour minutes:min seconds:sec |
216 | 108 |
|
239 | 109 |
" |
409 | 110 |
Time readFrom:'18:22:00' |
111 |
Time readFrom:'14:00:11' |
|
112 |
Time readFrom:'7:00:11' |
|
113 |
Time readFrom:'6:22:00 pm' |
|
114 |
Time readFrom:'2:00:11 pm' |
|
115 |
Time readFrom:'7:00:11 am' |
|
239 | 116 |
" |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
117 |
|
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
118 |
"Modified: 16.11.1995 / 22:51:20 / cg" |
1 | 119 |
! ! |
120 |
||
95 | 121 |
!Time methodsFor:'accessing'! |
77 | 122 |
|
123 |
minutes |
|
95 | 124 |
"return the number of minutes within the hour (i.e. 0..59)" |
125 |
||
241 | 126 |
^ (timeEncoding \\ 3600) // 60 |
77 | 127 |
|
95 | 128 |
" |
129 |
Time now minutes |
|
130 |
" |
|
77 | 131 |
! |
132 |
||
133 |
seconds |
|
95 | 134 |
"return the number of seconds within the minute (i.e. 0..59)" |
135 |
||
241 | 136 |
^ (timeEncoding \\ 3600) \\ 60 |
77 | 137 |
|
95 | 138 |
" |
139 |
Time now seconds |
|
140 |
" |
|
1 | 141 |
! |
142 |
||
241 | 143 |
hours |
144 |
"return the number of hours since midnight (i.e. 0..23)" |
|
95 | 145 |
|
241 | 146 |
^ timeEncoding // 3600 |
147 |
||
148 |
" |
|
149 |
Time now hours |
|
150 |
" |
|
275 | 151 |
! |
152 |
||
153 |
day |
|
154 |
"catch day access - Time does not know about it" |
|
155 |
||
156 |
^ self shouldNotImplement |
|
157 |
! |
|
158 |
||
159 |
month |
|
160 |
"catch month access - Time does not know about it" |
|
161 |
||
162 |
^ self shouldNotImplement |
|
163 |
! |
|
164 |
||
165 |
year |
|
166 |
"catch year access - Time does not know about it" |
|
167 |
||
168 |
^ self shouldNotImplement |
|
1 | 169 |
! ! |
170 |
||
95 | 171 |
!Time methodsFor:'printing & storing'! |
1 | 172 |
|
62 | 173 |
printOn:aStream |
95 | 174 |
"append a printed representation of the receiver to aStream. |
175 |
Format is hh:mm:ss either in 12-hour or 24-hour format. |
|
176 |
depending on the setting of LanguageTerritory. |
|
177 |
I dont know what ST-80 does here (12-hour format ?)" |
|
178 |
||
179 |
LanguageTerritory = 'usa' ifTrue:[ |
|
180 |
self print12HourFormatOn:aStream |
|
181 |
] ifFalse:[ |
|
182 |
self print24HourFormatOn:aStream |
|
183 |
] |
|
184 |
||
185 |
" |
|
216 | 186 |
Time now printOn:Transcript. Transcript cr |
95 | 187 |
" |
62 | 188 |
! |
189 |
||
95 | 190 |
print24HourFormatOn:aStream |
191 |
"append a printed representation of the receiver to aStream. |
|
409 | 192 |
Format is hh:mm:ss (i.e. 24-hour european format)." |
193 |
||
194 |
|h m s| |
|
1 | 195 |
|
409 | 196 |
h := self hours. |
197 |
(h < 10) ifTrue:[aStream nextPut:$0]. |
|
198 |
h printOn:aStream. |
|
241 | 199 |
aStream nextPut:$:. |
200 |
m := self minutes. |
|
201 |
(m < 10) ifTrue:[aStream nextPut:$0]. |
|
202 |
m printOn:aStream. |
|
203 |
aStream nextPut:$:. |
|
204 |
s := self seconds. |
|
205 |
(s < 10) ifTrue:[aStream nextPut:$0]. |
|
206 |
s printOn:aStream |
|
95 | 207 |
|
208 |
" |
|
216 | 209 |
Time now print24HourFormatOn:Transcript. Transcript cr |
95 | 210 |
" |
211 |
! |
|
212 |
||
213 |
print12HourFormatOn:aStream |
|
214 |
"append a printed representation of the receiver to aStream. |
|
241 | 215 |
Format is hh:mm:ss am/pm (i.e. 12-hour american format)." |
95 | 216 |
|
409 | 217 |
|h m s ampm| |
95 | 218 |
|
241 | 219 |
h := self hours. |
220 |
h > 12 ifTrue:[ |
|
275 | 221 |
h := h - 12. |
241 | 222 |
] ifFalse:[ |
275 | 223 |
h < 1 ifTrue:[ |
224 |
h := 12 |
|
225 |
] |
|
241 | 226 |
]. |
227 |
h printOn:aStream. |
|
228 |
aStream nextPut:$:. |
|
229 |
m := self minutes. |
|
230 |
(m < 10) ifTrue:[aStream nextPut:$0]. |
|
231 |
m printOn:aStream. |
|
232 |
aStream nextPut:$:. |
|
233 |
s := self seconds. |
|
234 |
(s < 10) ifTrue:[aStream nextPut:$0]. |
|
409 | 235 |
s printOn:aStream. |
241 | 236 |
h >= 12 ifTrue:[ |
409 | 237 |
ampm := ' am' |
241 | 238 |
] ifFalse:[ |
409 | 239 |
ampm := ' pm' |
240 |
]. |
|
241 |
aStream nextPutAll:ampm |
|
95 | 242 |
|
243 |
" |
|
216 | 244 |
Time now print12HourFormatOn:Transcript. Transcript cr |
95 | 245 |
" |
1 | 246 |
! ! |
241 | 247 |
|
248 |
!Time methodsFor:'converting'! |
|
249 |
||
250 |
asSeconds |
|
251 |
"return the number of seconds elapsed since midnight" |
|
252 |
||
253 |
^ timeEncoding |
|
254 |
||
255 |
" |
|
256 |
Time now asSeconds |
|
257 |
" |
|
275 | 258 |
! |
259 |
||
260 |
asTime |
|
261 |
"return a Time object from the receiver - thats the receiver." |
|
262 |
||
263 |
^ self |
|
264 |
! |
|
265 |
||
266 |
asAbsoluteTime |
|
267 |
"return an AbsoluteTime object from the receiver. |
|
268 |
The date components are taken from today." |
|
269 |
||
270 |
|today| |
|
271 |
||
272 |
today := Date today. |
|
273 |
^ AbsoluteTime day:today day month:today month year:today year |
|
274 |
hour:self hours minutes:self minutes seconds:self seconds |
|
275 |
" |
|
276 |
Time now asAbsoluteTime |
|
277 |
" |
|
241 | 278 |
! ! |
279 |
||
280 |
!Time methodsFor:'comparing'! |
|
281 |
||
282 |
> aTime |
|
283 |
"return true if the argument, aTime is after the receiver" |
|
284 |
||
275 | 285 |
^ timeEncoding > aTime timeEncoding |
241 | 286 |
! |
287 |
||
288 |
< aTime |
|
289 |
"return true if the argument, aTime is before the receiver" |
|
290 |
||
275 | 291 |
^ timeEncoding < aTime timeEncoding |
292 |
! |
|
293 |
||
294 |
= aTime |
|
295 |
"return true if the argument, aTime represents the same timeOfDay" |
|
296 |
||
297 |
aTime class == self class ifTrue:[ |
|
298 |
^ timeEncoding == aTime timeEncoding |
|
241 | 299 |
]. |
275 | 300 |
(aTime species == self species) ifFalse:[^ false]. |
301 |
^ self asSeconds == aTime asSeconds |
|
302 |
! |
|
303 |
||
304 |
hash |
|
305 |
"return an integer useful for hashing on times" |
|
306 |
||
307 |
^ timeEncoding |
|
241 | 308 |
! ! |
309 |
||
310 |
!Time methodsFor:'private'! |
|
311 |
||
275 | 312 |
fromOSTimeLow:lowOSTime and:hiOSTime |
313 |
"set my time, given operatingSystems time parts" |
|
314 |
||
315 |
|h m s| |
|
316 |
OperatingSystem computeTimePartsOf:lowOSTime and:hiOSTime for:[ |
|
317 |
:hours :minutes :secs | |
|
318 |
||
319 |
h := hours. |
|
320 |
m := minutes. |
|
321 |
s := secs. |
|
322 |
]. |
|
323 |
self setHour:h minutes:m seconds:s |
|
324 |
||
325 |
! |
|
326 |
||
327 |
getSeconds |
|
328 |
^ timeEncoding |
|
329 |
! |
|
330 |
||
331 |
setSeconds:secs |
|
332 |
"set my time given seconds since midnight" |
|
333 |
||
334 |
timeEncoding := secs |
|
335 |
! |
|
336 |
||
337 |
setHour:h minutes:m seconds:s |
|
338 |
"set my time given individual values" |
|
339 |
||
340 |
timeEncoding := ((h\\24) * 60 * 60 ) + (m * 60) + s. |
|
341 |
! |
|
342 |
||
241 | 343 |
timeEncoding:encoding |
344 |
"the internal encoding is stricktly private, |
|
345 |
and should not be used outside." |
|
346 |
||
347 |
timeEncoding := encoding |
|
348 |
! |
|
349 |
||
350 |
timeEncoding |
|
351 |
"the internal encoding is stricktly private, |
|
352 |
and should not be used outside." |
|
353 |
||
354 |
^ timeEncoding |
|
355 |
! ! |