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