author | Claus Gittinger <cg@exept.de> |
Mon, 05 Sep 2011 15:03:28 +0200 | |
changeset 13632 | 09ee3dad124c |
parent 13630 | 5a595564da03 |
child 16933 | dcf3e0e47852 |
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 |
|
13235 | 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 |
||
13603
d9baa85cd3ba
comment/format in: #fromString:
Claus Gittinger <cg@exept.de>
parents:
13235
diff
changeset
|
74 |
fromString: aString |
d9baa85cd3ba
comment/format in: #fromString:
Claus Gittinger <cg@exept.de>
parents:
13235
diff
changeset
|
75 |
^ self readFrom: (ReadStream on: aString). |
d9baa85cd3ba
comment/format in: #fromString:
Claus Gittinger <cg@exept.de>
parents:
13235
diff
changeset
|
76 |
|
d9baa85cd3ba
comment/format in: #fromString:
Claus Gittinger <cg@exept.de>
parents:
13235
diff
changeset
|
77 |
"Modified (format): / 20-08-2011 / 16:46:39 / cg" |
d9baa85cd3ba
comment/format in: #fromString:
Claus Gittinger <cg@exept.de>
parents:
13235
diff
changeset
|
78 |
! |
d9baa85cd3ba
comment/format in: #fromString:
Claus Gittinger <cg@exept.de>
parents:
13235
diff
changeset
|
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 |
||
13235 | 140 |
^ self fromSeconds: 0 |
7293 | 141 |
|
142 |
" |
|
143 |
Time midnight |
|
144 |
" |
|
145 |
! |
|
146 |
||
147 |
noon |
|
148 |
"Answer a new instance at noon." |
|
149 |
||
13235 | 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:[ |
|
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
|
211 |
"/ 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
|
212 |
"/ 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
|
213 |
"/ 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
|
214 |
"/ 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
|
215 |
"/ 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
|
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" |
|
13235 | 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 |
|
13213
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
277 |
defaultFormatString |
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
278 |
LanguageTerritory == #us ifTrue:[ |
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
279 |
^ self formatString12us |
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
280 |
] ifFalse:[ |
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
281 |
^ self formatString24 |
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
282 |
] |
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
283 |
|
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
284 |
"Created: / 16-01-2011 / 11:23:36 / cg" |
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
285 |
! |
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
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 |
||
7261 | 299 |
!Time methodsFor:'Compatibility-Squeak'! |
6164 | 300 |
|
13632 | 301 |
asMilliseconds |
302 |
^ self asMilliSeconds |
|
303 |
||
304 |
"Created: / 05-09-2011 / 14:56:36 / cg" |
|
305 |
! |
|
306 |
||
6164 | 307 |
intervalString |
308 |
"Treat the time as a difference. |
|
309 |
Give it in hours and minutes with two digits of accuracy." |
|
310 |
||
6467 | 311 |
|hours minutes seconds hh mm ss s| |
6164 | 312 |
|
313 |
hours := self hours. |
|
314 |
minutes := self minutes. |
|
315 |
seconds := self seconds. |
|
6467 | 316 |
|
317 |
hours = 0 |
|
318 |
ifTrue: [hh := ''] |
|
319 |
ifFalse: [ |
|
320 |
hh := hours printString, ' hour'. |
|
321 |
hours > 1 ifTrue:[ |
|
322 |
hh := hh , 's' |
|
323 |
] |
|
324 |
]. |
|
325 |
minutes = 0 |
|
326 |
ifTrue: [mm := ''] |
|
327 |
ifFalse: [ |
|
328 |
mm := minutes printString, ' minute'. |
|
329 |
minutes > 1 ifTrue:[ |
|
330 |
mm := mm , 's' |
|
331 |
] |
|
332 |
]. |
|
333 |
seconds = 0 |
|
334 |
ifTrue: [ss := ''] |
|
335 |
ifFalse: [ |
|
336 |
ss := seconds printString, ' second'. |
|
337 |
seconds > 1 ifTrue:[ |
|
338 |
ss := ss , 's' |
|
339 |
] |
|
340 |
]. |
|
341 |
||
342 |
"/ do not show seconds, if hours are there |
|
6164 | 343 |
hh size > 0 ifTrue: [ss := '']. |
6467 | 344 |
|
345 |
s := hh. |
|
6873 | 346 |
mm notEmpty ifTrue:[ |
347 |
s notEmpty ifTrue:[ |
|
348 |
s := s , ' ' |
|
349 |
]. |
|
350 |
s := s , mm. |
|
6467 | 351 |
]. |
6873 | 352 |
ss notEmpty ifTrue:[ |
353 |
s notEmpty ifTrue:[ |
|
354 |
s := s , ' ' |
|
355 |
]. |
|
356 |
s := s , ss |
|
6467 | 357 |
]. |
6873 | 358 |
^ s |
6164 | 359 |
|
360 |
" |
|
6873 | 361 |
(Time fromSeconds:3600) intervalString |
362 |
(Time fromSeconds:3605) intervalString |
|
363 |
(Time fromSeconds:3700) intervalString |
|
364 |
(Time fromSeconds:5) intervalString |
|
365 |
(Time fromSeconds:65) intervalString |
|
6164 | 366 |
" |
367 |
! |
|
368 |
||
7024 | 369 |
print24:prnt24FormatBoolean on:aStream |
370 |
"print me either US or 24hr format on a stream" |
|
371 |
||
372 |
^ self print24:prnt24FormatBoolean showSeconds:true on:aStream |
|
373 |
||
374 |
" |
|
375 |
Time now print24:true on:Transcript. Transcript cr. |
|
376 |
Time now print24:false on:Transcript. Transcript cr. |
|
377 |
" |
|
378 |
! |
|
379 |
||
6164 | 380 |
print24:prnt24Format showSeconds:doSeconds on:aStream |
381 |
"print me either US or 24hr format, optionally with |
|
382 |
seconds on a stream" |
|
383 |
||
384 |
|format| |
|
385 |
||
386 |
prnt24Format ifTrue:[ |
|
387 |
doSeconds ifTrue:[ |
|
388 |
format := '%h:%m:%s' |
|
389 |
] ifFalse:[ |
|
390 |
format := '%h:%m' |
|
391 |
]. |
|
392 |
] ifFalse:[ |
|
393 |
"/ US format |
|
394 |
doSeconds ifTrue:[ |
|
395 |
format := '%u:%m:%s %a' |
|
396 |
] ifFalse:[ |
|
397 |
format := '%u:%m %a' |
|
398 |
]. |
|
399 |
]. |
|
400 |
^ self |
|
401 |
printOn:aStream |
|
402 |
format:format. |
|
403 |
||
404 |
" |
|
405 |
Time now print24:true showSeconds:true on:Transcript. Transcript cr. |
|
406 |
Time now print24:false showSeconds:true on:Transcript. Transcript cr. |
|
407 |
Time now print24:true showSeconds:false on:Transcript. Transcript cr. |
|
408 |
Time now print24:false showSeconds:false on:Transcript. Transcript cr. |
|
409 |
" |
|
4411
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
410 |
! ! |
684a2b944463
much improved formatting (see #printOn:format:)
Claus Gittinger <cg@exept.de>
parents:
4392
diff
changeset
|
411 |
|
6164 | 412 |
!Time methodsFor:'accessing'! |
413 |
||
414 |
hours |
|
415 |
"return the number of hours since midnight (i.e. 0..23)" |
|
416 |
||
417 |
^ self getSeconds // 3600 |
|
418 |
||
419 |
" |
|
420 |
Time now hours |
|
421 |
" |
|
422 |
! |
|
275 | 423 |
|
4845 | 424 |
milliseconds |
6164 | 425 |
"time does not keep milliseconds |
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
426 |
- for compatibility with Timestamp" |
6164 | 427 |
|
428 |
^ 0 |
|
429 |
! |
|
430 |
||
431 |
minutes |
|
432 |
"return the number of minutes within the hour (i.e. 0..59)" |
|
433 |
||
434 |
^ (self getSeconds \\ 3600) // 60 |
|
435 |
||
436 |
" |
|
437 |
Time now minutes |
|
438 |
" |
|
439 |
! |
|
440 |
||
441 |
seconds |
|
442 |
"return the number of seconds within the minute (i.e. 0..59)" |
|
443 |
||
7733 | 444 |
^ self getSeconds \\ 60 |
6164 | 445 |
|
446 |
" |
|
447 |
Time now seconds |
|
448 |
" |
|
449 |
! |
|
450 |
||
9441 | 451 |
timeZoneDeltaInMinutes |
452 |
"answer the number of minutes between local time and utc time. |
|
453 |
Delta is positive if local time is ahead of utc, negative if behind utc. |
|
454 |
||
455 |
Time is local time, so answer the delta of today" |
|
456 |
||
457 |
^ Timestamp now timeZoneDeltaInMinutes |
|
458 |
||
459 |
||
460 |
" |
|
461 |
Time now timeZoneDeltaInMinutes |
|
462 |
" |
|
1 | 463 |
! ! |
464 |
||
6164 | 465 |
!Time methodsFor:'comparing'! |
466 |
||
467 |
< aTime |
|
468 |
"return true if the receiver is before the argument" |
|
469 |
||
10744
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
470 |
aTime class == self class ifTrue:[ |
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
471 |
^ timeEncoding < aTime timeEncoding |
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
472 |
]. |
6165 | 473 |
^ self getSeconds < aTime getSeconds |
6164 | 474 |
! |
475 |
||
476 |
= aTime |
|
477 |
"return true if the argument, aTime represents the same timeOfDay" |
|
478 |
||
479 |
aTime class == self class ifTrue:[ |
|
10666
609bedc85066
changed #'=' (for largeinteger timeEncodings in TimeDuration)
Claus Gittinger <cg@exept.de>
parents:
9441
diff
changeset
|
480 |
^ timeEncoding = aTime timeEncoding |
6164 | 481 |
]. |
482 |
(aTime species == self species) ifFalse:[^ false]. |
|
10666
609bedc85066
changed #'=' (for largeinteger timeEncodings in TimeDuration)
Claus Gittinger <cg@exept.de>
parents:
9441
diff
changeset
|
483 |
^ self getSeconds = aTime getSeconds |
609bedc85066
changed #'=' (for largeinteger timeEncodings in TimeDuration)
Claus Gittinger <cg@exept.de>
parents:
9441
diff
changeset
|
484 |
|
609bedc85066
changed #'=' (for largeinteger timeEncodings in TimeDuration)
Claus Gittinger <cg@exept.de>
parents:
9441
diff
changeset
|
485 |
"Modified: / 18-07-2007 / 14:16:34 / cg" |
6164 | 486 |
! |
487 |
||
488 |
> aTime |
|
489 |
"return true if the receiver is before the argument" |
|
490 |
||
10744
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
491 |
aTime class == self class ifTrue:[ |
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
492 |
^ timeEncoding > aTime timeEncoding |
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
493 |
]. |
6165 | 494 |
^ self getSeconds > aTime getSeconds |
6164 | 495 |
! |
496 |
||
497 |
hash |
|
498 |
"return an integer useful for hashing on times" |
|
499 |
||
10744
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
500 |
^ timeEncoding |
6164 | 501 |
! ! |
502 |
||
503 |
!Time methodsFor:'converting'! |
|
504 |
||
13235 | 505 |
asLocalTimestamp |
506 |
"return an Timestamp object from the receiver. |
|
507 |
So I am interpreted as a Time in the local timezone. |
|
508 |
The date components are taken from today." |
|
509 |
||
510 |
|todayTimestamp| |
|
511 |
||
512 |
todayTimestamp := Timestamp now. |
|
513 |
todayTimestamp year:todayTimestamp year month:todayTimestamp month day:todayTimestamp day |
|
514 |
hour:(self hours) minute:(self minutes) second:(self seconds) |
|
515 |
millisecond:(self milliseconds). |
|
516 |
||
517 |
^ todayTimestamp. |
|
518 |
||
519 |
" |
|
520 |
Time now asLocalTimestamp |
|
521 |
" |
|
522 |
! |
|
523 |
||
13630 | 524 |
asMilliSeconds |
525 |
"return the number of milliseconds elapsed since midnight" |
|
526 |
||
527 |
^ self getMilliseconds |
|
528 |
||
529 |
" |
|
530 |
Time now asMilliSeconds |
|
531 |
(TimeDuration days:1) asMilliSeconds |
|
532 |
(TimeDuration hours:1) asMilliSeconds |
|
533 |
" |
|
534 |
||
535 |
"Created: / 05-09-2011 / 10:40:15 / cg" |
|
536 |
! |
|
537 |
||
6164 | 538 |
asSeconds |
539 |
"return the number of seconds elapsed since midnight" |
|
540 |
||
541 |
^ self getSeconds |
|
542 |
||
543 |
" |
|
544 |
Time now asSeconds |
|
10933 | 545 |
(TimeDuration days:1) asSeconds |
6164 | 546 |
" |
547 |
! |
|
95 | 548 |
|
699 | 549 |
asTime |
550 |
"return a Time object from the receiver - thats the receiver." |
|
551 |
||
6164 | 552 |
^ self |
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
553 |
! |
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
554 |
|
8796 | 555 |
asTimeDuration |
556 |
"return an TimeDuration object from the receiver, taking the time since midnight." |
|
557 |
||
558 |
^ TimeDuration |
|
559 |
hours:(self hours) minutes:(self minutes) seconds:(self seconds) |
|
560 |
milliseconds:(self milliseconds) |
|
561 |
" |
|
562 |
Time now asTimeDuration |
|
563 |
" |
|
564 |
! |
|
565 |
||
8994
cbf9e75584fe
asTimeStamp -> asTimestamp for AbstractTime protocol compatibility
Stefan Vogel <sv@exept.de>
parents:
8796
diff
changeset
|
566 |
asTimestamp |
cbf9e75584fe
asTimeStamp -> asTimestamp for AbstractTime protocol compatibility
Stefan Vogel <sv@exept.de>
parents:
8796
diff
changeset
|
567 |
"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
|
568 |
The date components are taken from today." |
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
569 |
|
13235 | 570 |
^ self asLocalTimestamp |
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
571 |
|
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
572 |
" |
8994
cbf9e75584fe
asTimeStamp -> asTimestamp for AbstractTime protocol compatibility
Stefan Vogel <sv@exept.de>
parents:
8796
diff
changeset
|
573 |
Time now asTimestamp |
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
574 |
" |
13235 | 575 |
! |
576 |
||
577 |
asUtcTimestamp |
|
578 |
"return an UtcTimestamp object from the receiver. |
|
579 |
So I am interpreted as a Time in the UTC zone. |
|
580 |
The date components are taken from today." |
|
581 |
||
582 |
|todayTimestamp| |
|
583 |
||
584 |
todayTimestamp := UtcTimestamp now. |
|
585 |
todayTimestamp year:todayTimestamp year month:todayTimestamp month day:todayTimestamp day |
|
586 |
hour:(self hours) minute:(self minutes) second:(self seconds) |
|
587 |
millisecond:(self milliseconds). |
|
588 |
||
589 |
^ todayTimestamp. |
|
590 |
||
591 |
" |
|
592 |
Time now asUtcTimestamp |
|
593 |
" |
|
699 | 594 |
! ! |
595 |
||
6164 | 596 |
!Time methodsFor:'printing & storing'! |
597 |
||
598 |
print12HourFormatOn:aStream |
|
11151 | 599 |
"append a human readable printed representation of the receiver to aStream. |
6164 | 600 |
Format is hh:mm:ss am/pm (i.e. 12-hour american format)." |
601 |
||
602 |
^ self |
|
603 |
printOn:aStream |
|
604 |
format:(self class formatString12us) |
|
605 |
||
606 |
"/ |h m s ampm| |
|
607 |
"/ |
|
608 |
"/ h := self hours. |
|
609 |
"/ |
|
610 |
"/ "/ 0 -> 12 am |
|
611 |
"/ "/ 12 -> 12 pm |
|
612 |
"/ |
|
613 |
"/ h // 12 == 0 ifTrue:[ |
|
614 |
"/ ampm := ' am'. |
|
615 |
"/ ] ifFalse:[ |
|
616 |
"/ ampm := ' pm'. |
|
617 |
"/ ]. |
|
618 |
"/ |
|
619 |
"/ h == 0 ifFalse:[ |
|
620 |
"/ h := h - 1 \\ 12 + 1. |
|
621 |
"/ ]. |
|
622 |
"/ |
|
623 |
"/ h printOn:aStream. |
|
624 |
"/ aStream nextPut:$:. |
|
625 |
"/ m := self minutes. |
|
626 |
"/ (m < 10) ifTrue:[aStream nextPut:$0]. |
|
627 |
"/ m printOn:aStream. |
|
628 |
"/ aStream nextPut:$:. |
|
629 |
"/ s := self seconds. |
|
630 |
"/ (s < 10) ifTrue:[aStream nextPut:$0]. |
|
631 |
"/ s printOn:aStream. |
|
632 |
"/ aStream nextPutAll:ampm |
|
633 |
||
634 |
" |
|
635 |
Time now print12HourFormatOn:Transcript. Transcript cr |
|
636 |
(Time now subtractHours:12) print12HourFormatOn:Transcript. Transcript cr |
|
637 |
(Time hour:24 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
638 |
(Time hour:12 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
639 |
(Time hour:0 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
640 |
0 to:24 do:[:h | |
|
641 |
(Time hour:h minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
642 |
] |
|
643 |
" |
|
644 |
! |
|
645 |
||
646 |
print24HourFormatOn:aStream |
|
11151 | 647 |
"append a human readable printed representation of the receiver to aStream. |
6164 | 648 |
Format is hh:mm:ss (i.e. 24-hour european format)." |
649 |
||
650 |
^ self |
|
651 |
printOn:aStream |
|
652 |
format:(self class formatString24). |
|
653 |
||
654 |
"/ |h m s| |
|
655 |
"/ |
|
656 |
"/ h := self hours. |
|
657 |
"/ (h < 10) ifTrue:[aStream nextPut:$0]. |
|
658 |
"/ h printOn:aStream. |
|
659 |
"/ aStream nextPut:$:. |
|
660 |
"/ m := self minutes. |
|
661 |
"/ (m < 10) ifTrue:[aStream nextPut:$0]. |
|
662 |
"/ m printOn:aStream. |
|
663 |
"/ aStream nextPut:$:. |
|
664 |
"/ s := self seconds. |
|
665 |
"/ (s < 10) ifTrue:[aStream nextPut:$0]. |
|
666 |
"/ s printOn:aStream |
|
667 |
||
668 |
" |
|
669 |
Time now print24HourFormatOn:Transcript. Transcript cr |
|
670 |
" |
|
671 |
! |
|
672 |
||
673 |
printOn:aStream |
|
11151 | 674 |
"append a human readable printed representation of the receiver to aStream. |
6164 | 675 |
The format is suitable for a human - not meant to be read back. |
676 |
||
677 |
Format is hh:mm:ss either in 12-hour or 24-hour format. |
|
678 |
depending on the setting of LanguageTerritory. |
|
679 |
I dont know what ST-80 does here (12-hour format ?)" |
|
680 |
||
13213
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
681 |
^ self |
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
682 |
printOn:aStream |
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
683 |
format:(self class defaultFormatString) |
6164 | 684 |
|
685 |
" |
|
686 |
Time now printOn:Transcript. Transcript cr |
|
687 |
" |
|
688 |
||
13213
d37c29bad3da
added: #defaultFormatString
Claus Gittinger <cg@exept.de>
parents:
12829
diff
changeset
|
689 |
"Modified: / 16-01-2011 / 11:27:24 / cg" |
6164 | 690 |
! |
691 |
||
692 |
printString12HourFormat |
|
693 |
"return a printed representation in 12 hour format" |
|
694 |
||
695 |
^ self printStringFormat:(self class formatString12us) |
|
696 |
||
697 |
" |
|
698 |
Time now printString12HourFormat |
|
699 |
" |
|
700 |
! |
|
701 |
||
702 |
printString24HourFormat |
|
703 |
"return a printed representation in 24 hour format" |
|
704 |
||
705 |
^ self printStringFormat:(self class formatString24) |
|
706 |
||
707 |
" |
|
708 |
Time now printString24HourFormat |
|
709 |
" |
|
710 |
||
711 |
! |
|
712 |
||
713 |
shortPrintString |
|
714 |
"dummy - for now" |
|
715 |
||
716 |
^ self printString |
|
717 |
||
718 |
"Created: 20.6.1997 / 17:17:01 / cg" |
|
719 |
! ! |
|
720 |
||
721 |
!Time methodsFor:'private'! |
|
722 |
||
723 |
fromOSTime:osTime |
|
13235 | 724 |
"set my time in the local timezone, given an osTime" |
6164 | 725 |
|
7513 | 726 |
|i| |
6164 | 727 |
|
7513 | 728 |
i := OperatingSystem computeTimeAndDateFrom:osTime. |
7740 | 729 |
self setHours:(i hours) minutes:(i minutes) seconds:(i seconds) |
6164 | 730 |
|
731 |
"Modified: 1.7.1996 / 15:21:06 / cg" |
|
732 |
! |
|
275 | 733 |
|
13235 | 734 |
fromUtcOSTime:osTime |
735 |
"set my time in the local timezone, given an osTime" |
|
736 |
||
737 |
|i| |
|
738 |
||
739 |
i := OperatingSystem computeUTCTimeAndDateFrom:osTime. |
|
740 |
self setHours:(i hours) minutes:(i minutes) seconds:(i seconds) |
|
741 |
||
742 |
"Modified: 1.7.1996 / 15:21:06 / cg" |
|
743 |
! |
|
744 |
||
3477 | 745 |
getMilliseconds |
6046 | 746 |
"return the number of milliseconds since midnight" |
747 |
||
6165 | 748 |
^ self getSeconds * 1000 |
3477 | 749 |
! |
750 |
||
275 | 751 |
getSeconds |
6046 | 752 |
"return the number of seconds since midnight" |
753 |
||
6165 | 754 |
^ timeEncoding |
275 | 755 |
! |
756 |
||
6165 | 757 |
setHours:h minutes:m seconds:s |
699 | 758 |
"set my time given individual values" |
759 |
||
6165 | 760 |
self setSeconds:(((h\\24) * 60 * 60 ) + (m * 60) + s). |
3477 | 761 |
! |
762 |
||
763 |
setMilliseconds:millis |
|
764 |
"set my time given milliseconds since midnight" |
|
765 |
||
6165 | 766 |
self setSeconds:(millis // 1000) |
699 | 767 |
! |
768 |
||
6904 | 769 |
setSeconds:secs |
770 |
"set my time given seconds since midnight. |
|
9441 | 771 |
Notice the modulo operations here - there cannot be a time beyond 24hours |
6904 | 772 |
(use TimeDuration, if you need that)." |
773 |
||
774 |
secs < 0 ifTrue:[ |
|
775 |
timeEncoding := (24 * 3600) - (secs negated \\ (24 * 3600)) |
|
776 |
] ifFalse:[ |
|
777 |
timeEncoding := secs |
|
778 |
]. |
|
779 |
timeEncoding > (24 * 3600) ifTrue:[ |
|
780 |
timeEncoding := timeEncoding \\ (24 * 3600). |
|
781 |
] |
|
782 |
||
783 |
" |
|
7011 | 784 |
Time basicNew setSeconds:0 |
785 |
Time fromSeconds:3601 |
|
6904 | 786 |
Time now seconds |
787 |
Time now timeEncoding |
|
788 |
(Time now addDays:5) seconds |
|
789 |
(Time now addDays:5) timeEncoding |
|
790 |
" |
|
791 |
! |
|
792 |
||
6165 | 793 |
timeEncoding |
794 |
"the internal encoding is stricktly private, |
|
795 |
and should not be used outside." |
|
796 |
||
797 |
^ timeEncoding |
|
798 |
! |
|
799 |
||
800 |
timeEncoding:encoding |
|
801 |
"the internal encoding is stricktly private, |
|
802 |
and should not be used outside." |
|
803 |
||
804 |
timeEncoding := encoding |
|
699 | 805 |
! ! |
806 |
||
6165 | 807 |
!Time class methodsFor:'documentation'! |
241 | 808 |
|
699 | 809 |
version |
13632 | 810 |
^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.92 2011-09-05 13:03:28 cg Exp $' |
12728 | 811 |
! |
812 |
||
813 |
version_CVS |
|
13632 | 814 |
^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.92 2011-09-05 13:03:28 cg Exp $' |
241 | 815 |
! ! |