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