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