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