author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 19 Jan 2012 11:46:00 +0000 | |
branch | jv |
changeset 17911 | a99f15c5efa5 |
parent 17910 | 8d796ca8bd1d |
child 18011 | deb0c3355881 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
6165 | 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 |
" |
|
6165 | 12 |
"{ Package: 'stx:libbasic' }" |
5390 | 13 |
|
6165 | 14 |
AbstractTime subclass:#Time |
15 |
instanceVariableNames:'timeEncoding' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
7672 | 18 |
category:'Magnitude-Time' |
1 | 19 |
! |
20 |
||
6165 | 21 |
!Time class methodsFor:'documentation'! |
88 | 22 |
|
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
6165 | 26 |
All Rights Reserved |
88 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
37 |
documentation |
|
38 |
" |
|
39 |
Instances of time represent a particular time-of-day. |
|
409 | 40 |
Since they only store hours, minutes and seconds within a day, |
41 |
they cannot be used to compare times across midnight |
|
42 |
(i.e. they should not be used as timeStamps). |
|
1228 | 43 |
|
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
44 |
Use instances of Timestamp (and read the comment there) to do this. |
241 | 45 |
|
17815 | 46 |
Time now returns the time in the local timezone. |
47 |
Use Time utcNow to get the time in the UTC zone. |
|
48 |
||
241 | 49 |
Examples: |
1228 | 50 |
|t| |
241 | 51 |
|
1228 | 52 |
t := Time now. |
1422 | 53 |
Transcript showCR:t. |
241 | 54 |
|
55 |
||
1228 | 56 |
|t1 t2| |
241 | 57 |
|
1228 | 58 |
t1 := Time now. |
59 |
(Delay forSeconds:10) wait. |
|
60 |
t2 := Time now. |
|
61 |
t2 - t1 |
|
1295 | 62 |
|
63 |
[author:] |
|
64 |
Claus Gittinger |
|
65 |
||
66 |
[see also:] |
|
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
67 |
Date Timestamp AbstractTime OperatingSystem |
1295 | 68 |
Filename |
88 | 69 |
" |
70 |
! ! |
|
1 | 71 |
|
6165 | 72 |
!Time class methodsFor:'instance creation'! |
73 |
||
17850 | 74 |
fromString: aString |
75 |
^ self readFrom: (ReadStream on: aString). |
|
76 |
||
77 |
"Modified (format): / 20-08-2011 / 16:46:39 / cg" |
|
78 |
! |
|
79 |
||
8749 | 80 |
hour:h minute:m |
81 |
"compatibility" |
|
82 |
||
83 |
^ self hours:h minutes:m seconds:0 |
|
84 |
||
85 |
" |
|
86 |
Time hour:2 minute:33 |
|
87 |
" |
|
88 |
! |
|
89 |
||
7818 | 90 |
hour:h minute:m second:s |
91 |
"compatibility" |
|
92 |
||
93 |
^ self hours:h minutes:m seconds:s |
|
94 |
||
95 |
" |
|
96 |
Time hour:2 minute:33 second:0 |
|
97 |
" |
|
98 |
! |
|
99 |
||
6165 | 100 |
hour:h minutes:m seconds:s |
101 |
"return an instance of Time representing the given time. |
|
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
102 |
See also Time now / Date today / Timestamp now. |
6165 | 103 |
Obsolete: please use #hours:minutes:seconds:" |
104 |
||
105 |
<resource:#obsolete> |
|
106 |
||
9061 | 107 |
self obsoleteMethodWarning:'use #hours:minutes:seconds:'. |
6165 | 108 |
|
109 |
^ self hours:h minutes:m seconds:s |
|
110 |
||
111 |
" |
|
112 |
Time hours:2 minutes:33 seconds:0 |
|
113 |
Time hours:0 minutes:0 seconds:0 |
|
114 |
Time hours:24 minutes:0 seconds:0 |
|
115 |
Time hours23 minutes:59 seconds:59 |
|
116 |
" |
|
117 |
||
118 |
"Modified: 19.4.1996 / 15:32:40 / cg" |
|
119 |
! |
|
120 |
||
121 |
hours:h minutes:m seconds:s |
|
4031 | 122 |
"return an instance of Time representing the given time. |
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
123 |
See also Time now / Date today / Timestamp now." |
4031 | 124 |
|
6165 | 125 |
^ self basicNew setHours:h minutes:m seconds:s |
4031 | 126 |
|
127 |
" |
|
6165 | 128 |
Time hours:2 minutes:33 seconds:0 |
129 |
Time hours:0 minutes:0 seconds:0 |
|
130 |
Time hours:24 minutes:0 seconds:0 |
|
131 |
Time hours:23 minutes:59 seconds:59 |
|
95 | 132 |
" |
6165 | 133 |
|
134 |
"Modified: 19.4.1996 / 15:32:40 / cg" |
|
135 |
! |
|
136 |
||
7293 | 137 |
midnight |
138 |
"Answer a new instance at midnight." |
|
139 |
||
17815 | 140 |
^ self fromSeconds: 0 |
7293 | 141 |
|
142 |
" |
|
143 |
Time midnight |
|
144 |
" |
|
145 |
! |
|
146 |
||
147 |
noon |
|
148 |
"Answer a new instance at noon." |
|
149 |
||
17815 | 150 |
^ self fromSeconds: 43200 "12*60*60" |
7293 | 151 |
|
152 |
" |
|
153 |
Time noon |
|
154 |
" |
|
155 |
! |
|
156 |
||
6165 | 157 |
readFrom:aStringOrStream onError:exceptionBlock |
158 |
"return a new Time, reading a printed representation from aStream. |
|
159 |
If no am/pm follows the time, the string is interpreted as |
|
160 |
either 24 hour format or being am." |
|
161 |
||
6624
886265eff14c
#readFrom:onError: - avoid block creation
Stefan Vogel <sv@exept.de>
parents:
6467
diff
changeset
|
162 |
^ [ |
11308 | 163 |
|str hour min sec peekC| |
6165 | 164 |
|
165 |
str := aStringOrStream readStream. |
|
166 |
||
167 |
hour := Integer readFrom:str. |
|
168 |
(hour between:0 and:24) ifFalse:[^ exceptionBlock value]. |
|
169 |
||
170 |
min := 0. |
|
171 |
sec := 0. |
|
172 |
str atEnd ifFalse:[ |
|
11308 | 173 |
peekC := str peek. |
174 |
(peekC == $:) ifTrue:[ |
|
6165 | 175 |
str next. |
176 |
min := Integer readFrom:str. |
|
177 |
(min between:0 and:59) ifFalse:[^ exceptionBlock value]. |
|
178 |
||
179 |
(str peek == $:) ifTrue:[ |
|
180 |
str next. |
|
181 |
sec := Integer readFrom:str. |
|
182 |
(sec between:0 and:59) ifFalse:[^ exceptionBlock value]. |
|
183 |
]. |
|
11308 | 184 |
peekC := str peek. |
6165 | 185 |
]. |
11308 | 186 |
[peekC == Character space] whileTrue:[str next. peekC := str peek]. |
187 |
(peekC == $p or:[peekC == $P]) ifTrue:[ |
|
6165 | 188 |
str next. |
189 |
(str peek == $m or:[str peek == $M]) ifTrue:[ |
|
190 |
str next |
|
191 |
]. |
|
192 |
(hour <= 0 or:[hour > 12]) ifTrue:[^ exceptionBlock value]. |
|
193 |
||
194 |
"pm" |
|
195 |
hour ~~ 12 ifTrue:[ |
|
196 |
hour := hour + 12 |
|
11308 | 197 |
]. |
198 |
peekC := str peek |
|
6165 | 199 |
] ifFalse:[ |
11308 | 200 |
(peekC == $a or:[peekC == $A]) ifTrue:[ |
6165 | 201 |
str next. |
202 |
(str peek == $m or:[str peek == $M]) ifTrue:[ |
|
203 |
str next |
|
204 |
]. |
|
7240
341d1ac49d42
Handle 12:10 am correct when reading times.
Stefan Vogel <sv@exept.de>
parents:
7118
diff
changeset
|
205 |
hour == 12 ifTrue:[ |
6165 | 206 |
hour := 0. |
7240
341d1ac49d42
Handle 12:10 am correct when reading times.
Stefan Vogel <sv@exept.de>
parents:
7118
diff
changeset
|
207 |
]. |
341d1ac49d42
Handle 12:10 am correct when reading times.
Stefan Vogel <sv@exept.de>
parents:
7118
diff
changeset
|
208 |
hour > 12 ifTrue:[^ exceptionBlock value]. |
11308 | 209 |
peekC := str peek |
210 |
] ifFalse:[ |
|
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
211 |
"/ cg: dont be too picky here - we do not care, what comes after the |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
212 |
"/ time string. (Needed to be able to read rfc822 strings where a timezone |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
213 |
"/ follows (-/+0700 GMT, for example) |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
214 |
"/ peekC notNil ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
215 |
"/ peekC isSeparator ifFalse:[^ exceptionBlock value]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
216 |
"/ ] |
11308 | 217 |
]. |
6165 | 218 |
] |
219 |
]. |
|
6624
886265eff14c
#readFrom:onError: - avoid block creation
Stefan Vogel <sv@exept.de>
parents:
6467
diff
changeset
|
220 |
self basicNew setHours:hour minutes:min seconds:sec |
886265eff14c
#readFrom:onError: - avoid block creation
Stefan Vogel <sv@exept.de>
parents:
6467
diff
changeset
|
221 |
] on:Error do:exceptionBlock. |
6165 | 222 |
|
223 |
" |
|
224 |
Time readFrom:'0:00' |
|
225 |
Time readFrom:'2:00' |
|
226 |
Time readFrom:'12:00' |
|
227 |
Time readFrom:'14:00' |
|
228 |
Time readFrom:'23:00' |
|
229 |
Time readFrom:'24:00' |
|
230 |
Time readFrom:'2:30 am' |
|
231 |
Time readFrom:'2:30 pm' |
|
232 |
Time readFrom:'14' |
|
233 |
Time readFrom:'2 am' |
|
234 |
Time readFrom:'2 pm' |
|
7240
341d1ac49d42
Handle 12:10 am correct when reading times.
Stefan Vogel <sv@exept.de>
parents:
7118
diff
changeset
|
235 |
Time readFrom:'12:05 pm' |
341d1ac49d42
Handle 12:10 am correct when reading times.
Stefan Vogel <sv@exept.de>
parents:
7118
diff
changeset
|
236 |
Time readFrom:'12:06 am' |
6165 | 237 |
|
238 |
Time readFrom:'18:22:00' |
|
239 |
Time readFrom:'14:00:11' |
|
240 |
Time readFrom:'7:00:11' |
|
241 |
Time readFrom:'24:00:00' |
|
242 |
Time readFrom:'0:00:00' |
|
243 |
Time readFrom:'12:00:00' |
|
244 |
Time readFrom:'0:00:00' |
|
245 |
Time readFrom:'6:22:00 pm' |
|
246 |
Time readFrom:'2:00:11 pm' |
|
247 |
Time readFrom:'7:00:11 am' |
|
248 |
Time readFrom:'12:00:00 am' |
|
249 |
Time readFrom:'0:00:00 am' |
|
250 |
Time readFrom:'24:00:00 am' |
|
251 |
Time readFrom:'12:00:00 pm' |
|
6624
886265eff14c
#readFrom:onError: - avoid block creation
Stefan Vogel <sv@exept.de>
parents:
6467
diff
changeset
|
252 |
Time readFrom:'0:00:00 pm' onError:'invalid' |
6165 | 253 |
Time readFrom:'24:00:00 pm' |
254 |
" |
|
255 |
||
256 |
"Modified: 8.10.1996 / 19:32:11 / cg" |
|
17815 | 257 |
! |
258 |
||
259 |
utcNow |
|
260 |
"return an instance of myself representing this moment." |
|
261 |
||
262 |
^ self basicNew fromUtcOSTime:(OperatingSystem getOSTime) |
|
263 |
||
264 |
" |
|
265 |
Timestamp now |
|
266 |
Timestamp utcNow |
|
267 |
||
268 |
Time now |
|
269 |
Time utcNow |
|
270 |
" |
|
271 |
||
272 |
"Modified: 1.7.1996 / 15:20:10 / cg" |
|
1 | 273 |
! ! |
274 |
||
6165 | 275 |
!Time class methodsFor:'format strings'! |
4411
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
276 |
|
17815 | 277 |
defaultFormatString |
278 |
LanguageTerritory == #us ifTrue:[ |
|
279 |
^ self formatString12us |
|
280 |
] ifFalse:[ |
|
281 |
^ self formatString24 |
|
282 |
] |
|
283 |
||
284 |
"Created: / 16-01-2011 / 11:23:36 / cg" |
|
285 |
! |
|
286 |
||
4411
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
287 |
formatString12us |
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
288 |
"return the format string used to format US times (and other areas)" |
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
289 |
|
8231 | 290 |
^ '%u:%m:%s %A' |
4411
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
291 |
! |
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
292 |
|
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
293 |
formatString24 |
8231 | 294 |
"return the format string used to format non US times" |
4411
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
295 |
|
6164 | 296 |
^ '%h:%m:%s' |
297 |
! ! |
|
298 |
||
17865 | 299 |
|
7261 | 300 |
!Time methodsFor:'Compatibility-Squeak'! |
6164 | 301 |
|
17859 | 302 |
asMilliseconds |
303 |
^ self asMilliSeconds |
|
304 |
||
305 |
"Created: / 05-09-2011 / 14:56:36 / cg" |
|
306 |
! |
|
307 |
||
6164 | 308 |
intervalString |
309 |
"Treat the time as a difference. |
|
310 |
Give it in hours and minutes with two digits of accuracy." |
|
311 |
||
6467 | 312 |
|hours minutes seconds hh mm ss s| |
6164 | 313 |
|
314 |
hours := self hours. |
|
315 |
minutes := self minutes. |
|
316 |
seconds := self seconds. |
|
6467 | 317 |
|
318 |
hours = 0 |
|
319 |
ifTrue: [hh := ''] |
|
320 |
ifFalse: [ |
|
321 |
hh := hours printString, ' hour'. |
|
322 |
hours > 1 ifTrue:[ |
|
323 |
hh := hh , 's' |
|
324 |
] |
|
325 |
]. |
|
326 |
minutes = 0 |
|
327 |
ifTrue: [mm := ''] |
|
328 |
ifFalse: [ |
|
329 |
mm := minutes printString, ' minute'. |
|
330 |
minutes > 1 ifTrue:[ |
|
331 |
mm := mm , 's' |
|
332 |
] |
|
333 |
]. |
|
334 |
seconds = 0 |
|
335 |
ifTrue: [ss := ''] |
|
336 |
ifFalse: [ |
|
337 |
ss := seconds printString, ' second'. |
|
338 |
seconds > 1 ifTrue:[ |
|
339 |
ss := ss , 's' |
|
340 |
] |
|
341 |
]. |
|
342 |
||
343 |
"/ do not show seconds, if hours are there |
|
6164 | 344 |
hh size > 0 ifTrue: [ss := '']. |
6467 | 345 |
|
346 |
s := hh. |
|
6873 | 347 |
mm notEmpty ifTrue:[ |
348 |
s notEmpty ifTrue:[ |
|
349 |
s := s , ' ' |
|
350 |
]. |
|
351 |
s := s , mm. |
|
6467 | 352 |
]. |
6873 | 353 |
ss notEmpty ifTrue:[ |
354 |
s notEmpty ifTrue:[ |
|
355 |
s := s , ' ' |
|
356 |
]. |
|
357 |
s := s , ss |
|
6467 | 358 |
]. |
6873 | 359 |
^ s |
6164 | 360 |
|
361 |
" |
|
6873 | 362 |
(Time fromSeconds:3600) intervalString |
363 |
(Time fromSeconds:3605) intervalString |
|
364 |
(Time fromSeconds:3700) intervalString |
|
365 |
(Time fromSeconds:5) intervalString |
|
366 |
(Time fromSeconds:65) intervalString |
|
6164 | 367 |
" |
368 |
! |
|
369 |
||
7024 | 370 |
print24:prnt24FormatBoolean on:aStream |
371 |
"print me either US or 24hr format on a stream" |
|
372 |
||
373 |
^ self print24:prnt24FormatBoolean showSeconds:true on:aStream |
|
374 |
||
375 |
" |
|
376 |
Time now print24:true on:Transcript. Transcript cr. |
|
377 |
Time now print24:false on:Transcript. Transcript cr. |
|
378 |
" |
|
379 |
! |
|
380 |
||
6164 | 381 |
print24:prnt24Format showSeconds:doSeconds on:aStream |
382 |
"print me either US or 24hr format, optionally with |
|
383 |
seconds on a stream" |
|
384 |
||
385 |
|format| |
|
386 |
||
387 |
prnt24Format ifTrue:[ |
|
388 |
doSeconds ifTrue:[ |
|
389 |
format := '%h:%m:%s' |
|
390 |
] ifFalse:[ |
|
391 |
format := '%h:%m' |
|
392 |
]. |
|
393 |
] ifFalse:[ |
|
394 |
"/ US format |
|
395 |
doSeconds ifTrue:[ |
|
396 |
format := '%u:%m:%s %a' |
|
397 |
] ifFalse:[ |
|
398 |
format := '%u:%m %a' |
|
399 |
]. |
|
400 |
]. |
|
401 |
^ self |
|
402 |
printOn:aStream |
|
403 |
format:format. |
|
404 |
||
405 |
" |
|
406 |
Time now print24:true showSeconds:true on:Transcript. Transcript cr. |
|
407 |
Time now print24:false showSeconds:true on:Transcript. Transcript cr. |
|
408 |
Time now print24:true showSeconds:false on:Transcript. Transcript cr. |
|
409 |
Time now print24:false showSeconds:false on:Transcript. Transcript cr. |
|
410 |
" |
|
4411
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
411 |
! ! |
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
412 |
|
6164 | 413 |
!Time methodsFor:'accessing'! |
414 |
||
415 |
hours |
|
416 |
"return the number of hours since midnight (i.e. 0..23)" |
|
417 |
||
418 |
^ self getSeconds // 3600 |
|
419 |
||
420 |
" |
|
421 |
Time now hours |
|
422 |
" |
|
423 |
! |
|
275 | 424 |
|
4845 | 425 |
milliseconds |
6164 | 426 |
"time does not keep milliseconds |
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
427 |
- for compatibility with Timestamp" |
6164 | 428 |
|
429 |
^ 0 |
|
430 |
! |
|
431 |
||
432 |
minutes |
|
433 |
"return the number of minutes within the hour (i.e. 0..59)" |
|
434 |
||
435 |
^ (self getSeconds \\ 3600) // 60 |
|
436 |
||
437 |
" |
|
438 |
Time now minutes |
|
439 |
" |
|
440 |
! |
|
441 |
||
442 |
seconds |
|
443 |
"return the number of seconds within the minute (i.e. 0..59)" |
|
444 |
||
7733 | 445 |
^ self getSeconds \\ 60 |
6164 | 446 |
|
447 |
" |
|
448 |
Time now seconds |
|
449 |
" |
|
450 |
! |
|
451 |
||
9441 | 452 |
timeZoneDeltaInMinutes |
453 |
"answer the number of minutes between local time and utc time. |
|
454 |
Delta is positive if local time is ahead of utc, negative if behind utc. |
|
455 |
||
456 |
Time is local time, so answer the delta of today" |
|
457 |
||
458 |
^ Timestamp now timeZoneDeltaInMinutes |
|
459 |
||
460 |
||
461 |
" |
|
462 |
Time now timeZoneDeltaInMinutes |
|
463 |
" |
|
1 | 464 |
! ! |
465 |
||
6164 | 466 |
!Time methodsFor:'comparing'! |
467 |
||
468 |
< aTime |
|
469 |
"return true if the receiver is before the argument" |
|
470 |
||
10744
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
471 |
aTime class == self class ifTrue:[ |
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
472 |
^ timeEncoding < aTime timeEncoding |
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
473 |
]. |
6165 | 474 |
^ self getSeconds < aTime getSeconds |
6164 | 475 |
! |
476 |
||
477 |
= aTime |
|
478 |
"return true if the argument, aTime represents the same timeOfDay" |
|
479 |
||
480 |
aTime class == self class ifTrue:[ |
|
10666
609bedc85066
changed #'=' (for largeinteger timeEncodings in TimeDuration)
Claus Gittinger <cg@exept.de>
parents:
9441
diff
changeset
|
481 |
^ timeEncoding = aTime timeEncoding |
6164 | 482 |
]. |
483 |
(aTime species == self species) ifFalse:[^ false]. |
|
10666
609bedc85066
changed #'=' (for largeinteger timeEncodings in TimeDuration)
Claus Gittinger <cg@exept.de>
parents:
9441
diff
changeset
|
484 |
^ self getSeconds = aTime getSeconds |
609bedc85066
changed #'=' (for largeinteger timeEncodings in TimeDuration)
Claus Gittinger <cg@exept.de>
parents:
9441
diff
changeset
|
485 |
|
609bedc85066
changed #'=' (for largeinteger timeEncodings in TimeDuration)
Claus Gittinger <cg@exept.de>
parents:
9441
diff
changeset
|
486 |
"Modified: / 18-07-2007 / 14:16:34 / cg" |
6164 | 487 |
! |
488 |
||
489 |
> aTime |
|
490 |
"return true if the receiver is before the argument" |
|
491 |
||
10744
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
492 |
aTime class == self class ifTrue:[ |
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
493 |
^ timeEncoding > aTime timeEncoding |
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
494 |
]. |
6165 | 495 |
^ self getSeconds > aTime getSeconds |
6164 | 496 |
! |
497 |
||
498 |
hash |
|
499 |
"return an integer useful for hashing on times" |
|
500 |
||
10744
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
501 |
^ timeEncoding |
6164 | 502 |
! ! |
503 |
||
504 |
!Time methodsFor:'converting'! |
|
505 |
||
17815 | 506 |
asLocalTimestamp |
507 |
"return an Timestamp object from the receiver. |
|
508 |
So I am interpreted as a Time in the local timezone. |
|
509 |
The date components are taken from today." |
|
510 |
||
511 |
|todayTimestamp| |
|
512 |
||
513 |
todayTimestamp := Timestamp now. |
|
514 |
todayTimestamp year:todayTimestamp year month:todayTimestamp month day:todayTimestamp day |
|
515 |
hour:(self hours) minute:(self minutes) second:(self seconds) |
|
516 |
millisecond:(self milliseconds). |
|
517 |
||
518 |
^ todayTimestamp. |
|
519 |
||
520 |
" |
|
521 |
Time now asLocalTimestamp |
|
522 |
" |
|
17751 | 523 |
! |
524 |
||
17859 | 525 |
asMilliSeconds |
526 |
"return the number of milliseconds elapsed since midnight" |
|
527 |
||
528 |
^ self getMilliseconds |
|
529 |
||
530 |
" |
|
531 |
Time now asMilliSeconds |
|
532 |
(TimeDuration days:1) asMilliSeconds |
|
533 |
(TimeDuration hours:1) asMilliSeconds |
|
534 |
" |
|
535 |
||
536 |
"Created: / 05-09-2011 / 10:40:15 / cg" |
|
537 |
! |
|
538 |
||
6164 | 539 |
asSeconds |
540 |
"return the number of seconds elapsed since midnight" |
|
541 |
||
542 |
^ self getSeconds |
|
543 |
||
544 |
" |
|
545 |
Time now asSeconds |
|
10933 | 546 |
(TimeDuration days:1) asSeconds |
6164 | 547 |
" |
548 |
! |
|
95 | 549 |
|
699 | 550 |
asTime |
551 |
"return a Time object from the receiver - thats the receiver." |
|
552 |
||
6164 | 553 |
^ self |
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
554 |
! |
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
555 |
|
8796 | 556 |
asTimeDuration |
557 |
"return an TimeDuration object from the receiver, taking the time since midnight." |
|
558 |
||
559 |
^ TimeDuration |
|
560 |
hours:(self hours) minutes:(self minutes) seconds:(self seconds) |
|
561 |
milliseconds:(self milliseconds) |
|
562 |
" |
|
563 |
Time now asTimeDuration |
|
564 |
" |
|
565 |
! |
|
566 |
||
8994
cbf9e75584fe
asTimeStamp -> asTimestamp for AbstractTime protocol compatibility
Stefan Vogel <sv@exept.de>
parents:
8796
diff
changeset
|
567 |
asTimestamp |
cbf9e75584fe
asTimeStamp -> asTimestamp for AbstractTime protocol compatibility
Stefan Vogel <sv@exept.de>
parents:
8796
diff
changeset
|
568 |
"return an Timestamp object from the receiver. |
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
569 |
The date components are taken from today." |
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
570 |
|
17815 | 571 |
^ self asLocalTimestamp |
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
572 |
|
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
573 |
" |
8994
cbf9e75584fe
asTimeStamp -> asTimestamp for AbstractTime protocol compatibility
Stefan Vogel <sv@exept.de>
parents:
8796
diff
changeset
|
574 |
Time now asTimestamp |
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
575 |
" |
17815 | 576 |
! |
577 |
||
578 |
asUtcTimestamp |
|
579 |
"return an UtcTimestamp object from the receiver. |
|
580 |
So I am interpreted as a Time in the UTC zone. |
|
581 |
The date components are taken from today." |
|
582 |
||
583 |
|todayTimestamp| |
|
584 |
||
585 |
todayTimestamp := UtcTimestamp now. |
|
586 |
todayTimestamp year:todayTimestamp year month:todayTimestamp month day:todayTimestamp day |
|
587 |
hour:(self hours) minute:(self minutes) second:(self seconds) |
|
588 |
millisecond:(self milliseconds). |
|
589 |
||
590 |
^ todayTimestamp. |
|
591 |
||
592 |
" |
|
593 |
Time now asUtcTimestamp |
|
594 |
" |
|
699 | 595 |
! ! |
596 |
||
6164 | 597 |
!Time methodsFor:'printing & storing'! |
598 |
||
599 |
print12HourFormatOn:aStream |
|
11151 | 600 |
"append a human readable printed representation of the receiver to aStream. |
6164 | 601 |
Format is hh:mm:ss am/pm (i.e. 12-hour american format)." |
602 |
||
603 |
^ self |
|
604 |
printOn:aStream |
|
605 |
format:(self class formatString12us) |
|
606 |
||
607 |
"/ |h m s ampm| |
|
608 |
"/ |
|
609 |
"/ h := self hours. |
|
610 |
"/ |
|
611 |
"/ "/ 0 -> 12 am |
|
612 |
"/ "/ 12 -> 12 pm |
|
613 |
"/ |
|
614 |
"/ h // 12 == 0 ifTrue:[ |
|
615 |
"/ ampm := ' am'. |
|
616 |
"/ ] ifFalse:[ |
|
617 |
"/ ampm := ' pm'. |
|
618 |
"/ ]. |
|
619 |
"/ |
|
620 |
"/ h == 0 ifFalse:[ |
|
621 |
"/ h := h - 1 \\ 12 + 1. |
|
622 |
"/ ]. |
|
623 |
"/ |
|
624 |
"/ h printOn:aStream. |
|
625 |
"/ aStream nextPut:$:. |
|
626 |
"/ m := self minutes. |
|
627 |
"/ (m < 10) ifTrue:[aStream nextPut:$0]. |
|
628 |
"/ m printOn:aStream. |
|
629 |
"/ aStream nextPut:$:. |
|
630 |
"/ s := self seconds. |
|
631 |
"/ (s < 10) ifTrue:[aStream nextPut:$0]. |
|
632 |
"/ s printOn:aStream. |
|
633 |
"/ aStream nextPutAll:ampm |
|
634 |
||
635 |
" |
|
636 |
Time now print12HourFormatOn:Transcript. Transcript cr |
|
637 |
(Time now subtractHours:12) print12HourFormatOn:Transcript. Transcript cr |
|
638 |
(Time hour:24 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
639 |
(Time hour:12 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
640 |
(Time hour:0 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
641 |
0 to:24 do:[:h | |
|
642 |
(Time hour:h minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
643 |
] |
|
644 |
" |
|
645 |
! |
|
646 |
||
647 |
print24HourFormatOn:aStream |
|
11151 | 648 |
"append a human readable printed representation of the receiver to aStream. |
6164 | 649 |
Format is hh:mm:ss (i.e. 24-hour european format)." |
650 |
||
651 |
^ self |
|
652 |
printOn:aStream |
|
653 |
format:(self class formatString24). |
|
654 |
||
655 |
"/ |h m s| |
|
656 |
"/ |
|
657 |
"/ h := self hours. |
|
658 |
"/ (h < 10) ifTrue:[aStream nextPut:$0]. |
|
659 |
"/ h printOn:aStream. |
|
660 |
"/ aStream nextPut:$:. |
|
661 |
"/ m := self minutes. |
|
662 |
"/ (m < 10) ifTrue:[aStream nextPut:$0]. |
|
663 |
"/ m printOn:aStream. |
|
664 |
"/ aStream nextPut:$:. |
|
665 |
"/ s := self seconds. |
|
666 |
"/ (s < 10) ifTrue:[aStream nextPut:$0]. |
|
667 |
"/ s printOn:aStream |
|
668 |
||
669 |
" |
|
670 |
Time now print24HourFormatOn:Transcript. Transcript cr |
|
671 |
" |
|
672 |
! |
|
673 |
||
674 |
printOn:aStream |
|
11151 | 675 |
"append a human readable printed representation of the receiver to aStream. |
6164 | 676 |
The format is suitable for a human - not meant to be read back. |
677 |
||
678 |
Format is hh:mm:ss either in 12-hour or 24-hour format. |
|
679 |
depending on the setting of LanguageTerritory. |
|
680 |
I dont know what ST-80 does here (12-hour format ?)" |
|
681 |
||
17815 | 682 |
^ self |
683 |
printOn:aStream |
|
684 |
format:(self class defaultFormatString) |
|
6164 | 685 |
|
686 |
" |
|
687 |
Time now printOn:Transcript. Transcript cr |
|
688 |
" |
|
689 |
||
17815 | 690 |
"Modified: / 16-01-2011 / 11:27:24 / cg" |
6164 | 691 |
! |
692 |
||
693 |
printString12HourFormat |
|
694 |
"return a printed representation in 12 hour format" |
|
695 |
||
696 |
^ self printStringFormat:(self class formatString12us) |
|
697 |
||
698 |
" |
|
699 |
Time now printString12HourFormat |
|
700 |
" |
|
701 |
! |
|
702 |
||
703 |
printString24HourFormat |
|
704 |
"return a printed representation in 24 hour format" |
|
705 |
||
706 |
^ self printStringFormat:(self class formatString24) |
|
707 |
||
708 |
" |
|
709 |
Time now printString24HourFormat |
|
710 |
" |
|
711 |
||
712 |
! |
|
713 |
||
714 |
shortPrintString |
|
715 |
"dummy - for now" |
|
716 |
||
717 |
^ self printString |
|
718 |
||
719 |
"Created: 20.6.1997 / 17:17:01 / cg" |
|
720 |
! ! |
|
721 |
||
722 |
!Time methodsFor:'private'! |
|
723 |
||
724 |
fromOSTime:osTime |
|
17815 | 725 |
"set my time in the local timezone, given an osTime" |
6164 | 726 |
|
7513 | 727 |
|i| |
6164 | 728 |
|
7513 | 729 |
i := OperatingSystem computeTimeAndDateFrom:osTime. |
7740 | 730 |
self setHours:(i hours) minutes:(i minutes) seconds:(i seconds) |
6164 | 731 |
|
732 |
"Modified: 1.7.1996 / 15:21:06 / cg" |
|
733 |
! |
|
275 | 734 |
|
17815 | 735 |
fromUtcOSTime:osTime |
736 |
"set my time in the local timezone, given an osTime" |
|
737 |
||
738 |
|i| |
|
739 |
||
740 |
i := OperatingSystem computeUTCTimeAndDateFrom:osTime. |
|
741 |
self setHours:(i hours) minutes:(i minutes) seconds:(i seconds) |
|
742 |
||
743 |
"Modified: 1.7.1996 / 15:21:06 / cg" |
|
744 |
! |
|
745 |
||
3477 | 746 |
getMilliseconds |
6046 | 747 |
"return the number of milliseconds since midnight" |
748 |
||
6165 | 749 |
^ self getSeconds * 1000 |
3477 | 750 |
! |
751 |
||
275 | 752 |
getSeconds |
6046 | 753 |
"return the number of seconds since midnight" |
754 |
||
6165 | 755 |
^ timeEncoding |
275 | 756 |
! |
757 |
||
6165 | 758 |
setHours:h minutes:m seconds:s |
699 | 759 |
"set my time given individual values" |
760 |
||
6165 | 761 |
self setSeconds:(((h\\24) * 60 * 60 ) + (m * 60) + s). |
3477 | 762 |
! |
763 |
||
764 |
setMilliseconds:millis |
|
765 |
"set my time given milliseconds since midnight" |
|
766 |
||
6165 | 767 |
self setSeconds:(millis // 1000) |
699 | 768 |
! |
769 |
||
6904 | 770 |
setSeconds:secs |
771 |
"set my time given seconds since midnight. |
|
9441 | 772 |
Notice the modulo operations here - there cannot be a time beyond 24hours |
6904 | 773 |
(use TimeDuration, if you need that)." |
774 |
||
775 |
secs < 0 ifTrue:[ |
|
776 |
timeEncoding := (24 * 3600) - (secs negated \\ (24 * 3600)) |
|
777 |
] ifFalse:[ |
|
778 |
timeEncoding := secs |
|
779 |
]. |
|
780 |
timeEncoding > (24 * 3600) ifTrue:[ |
|
781 |
timeEncoding := timeEncoding \\ (24 * 3600). |
|
782 |
] |
|
783 |
||
784 |
" |
|
7011 | 785 |
Time basicNew setSeconds:0 |
786 |
Time fromSeconds:3601 |
|
6904 | 787 |
Time now seconds |
788 |
Time now timeEncoding |
|
789 |
(Time now addDays:5) seconds |
|
790 |
(Time now addDays:5) timeEncoding |
|
791 |
" |
|
792 |
! |
|
793 |
||
6165 | 794 |
timeEncoding |
795 |
"the internal encoding is stricktly private, |
|
796 |
and should not be used outside." |
|
797 |
||
798 |
^ timeEncoding |
|
799 |
! |
|
800 |
||
801 |
timeEncoding:encoding |
|
802 |
"the internal encoding is stricktly private, |
|
803 |
and should not be used outside." |
|
804 |
||
805 |
timeEncoding := encoding |
|
699 | 806 |
! ! |
807 |
||
17865 | 808 |
|
6165 | 809 |
!Time class methodsFor:'documentation'! |
241 | 810 |
|
699 | 811 |
version |
17892 | 812 |
^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.92 2011/09/05 13:03:28 cg Exp $' |
17751 | 813 |
! |
814 |
||
815 |
version_CVS |
|
17865 | 816 |
^ '§Header: /cvs/stx/stx/libbasic/Time.st,v 1.92 2011/09/05 13:03:28 cg Exp §' |
817 |
! |
|
818 |
||
819 |
version_SVN |
|
17911 | 820 |
^ '$Id: Time.st 10761 2012-01-19 11:46:00Z vranyj1 $' |
17846 | 821 |
! ! |
17892 | 822 |
|
17910 | 823 |
|
17911 | 824 |