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