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