author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 26 Apr 2010 19:26:38 +0100 | |
branch | jv |
changeset 17761 | b0e5971141bc |
parent 17757 | 73caeb68bf1f |
child 17767 | a4a32df3aa5e |
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:[ |
|
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
202 |
"/ cg: dont be too picky here - we do not care, what comes after the |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
203 |
"/ time string. (Needed to be able to read rfc822 strings where a timezone |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
204 |
"/ follows (-/+0700 GMT, for example) |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
205 |
"/ peekC notNil ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
206 |
"/ peekC isSeparator ifFalse:[^ exceptionBlock value]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
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 |
hours |
|
374 |
"return the number of hours since midnight (i.e. 0..23)" |
|
375 |
||
376 |
^ self getSeconds // 3600 |
|
377 |
||
378 |
" |
|
379 |
Time now hours |
|
380 |
" |
|
381 |
! |
|
275 | 382 |
|
4845 | 383 |
milliseconds |
6164 | 384 |
"time does not keep milliseconds |
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
385 |
- for compatibility with Timestamp" |
6164 | 386 |
|
387 |
^ 0 |
|
388 |
! |
|
389 |
||
390 |
minutes |
|
391 |
"return the number of minutes within the hour (i.e. 0..59)" |
|
392 |
||
393 |
^ (self getSeconds \\ 3600) // 60 |
|
394 |
||
395 |
" |
|
396 |
Time now minutes |
|
397 |
" |
|
398 |
! |
|
399 |
||
400 |
seconds |
|
401 |
"return the number of seconds within the minute (i.e. 0..59)" |
|
402 |
||
7733 | 403 |
^ self getSeconds \\ 60 |
6164 | 404 |
|
405 |
" |
|
406 |
Time now seconds |
|
407 |
" |
|
408 |
! |
|
409 |
||
9441 | 410 |
timeZoneDeltaInMinutes |
411 |
"answer the number of minutes between local time and utc time. |
|
412 |
Delta is positive if local time is ahead of utc, negative if behind utc. |
|
413 |
||
414 |
Time is local time, so answer the delta of today" |
|
415 |
||
416 |
^ Timestamp now timeZoneDeltaInMinutes |
|
417 |
||
418 |
||
419 |
" |
|
420 |
Time now timeZoneDeltaInMinutes |
|
421 |
" |
|
1 | 422 |
! ! |
423 |
||
6164 | 424 |
!Time methodsFor:'comparing'! |
425 |
||
426 |
< aTime |
|
427 |
"return true if the receiver is before the argument" |
|
428 |
||
10744
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
429 |
aTime class == self class ifTrue:[ |
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
430 |
^ timeEncoding < aTime timeEncoding |
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
431 |
]. |
6165 | 432 |
^ self getSeconds < aTime getSeconds |
6164 | 433 |
! |
434 |
||
435 |
= aTime |
|
436 |
"return true if the argument, aTime represents the same timeOfDay" |
|
437 |
||
438 |
aTime class == self class ifTrue:[ |
|
10666
609bedc85066
changed #'=' (for largeinteger timeEncodings in TimeDuration)
Claus Gittinger <cg@exept.de>
parents:
9441
diff
changeset
|
439 |
^ timeEncoding = aTime timeEncoding |
6164 | 440 |
]. |
441 |
(aTime species == self species) ifFalse:[^ false]. |
|
10666
609bedc85066
changed #'=' (for largeinteger timeEncodings in TimeDuration)
Claus Gittinger <cg@exept.de>
parents:
9441
diff
changeset
|
442 |
^ self getSeconds = aTime getSeconds |
609bedc85066
changed #'=' (for largeinteger timeEncodings in TimeDuration)
Claus Gittinger <cg@exept.de>
parents:
9441
diff
changeset
|
443 |
|
609bedc85066
changed #'=' (for largeinteger timeEncodings in TimeDuration)
Claus Gittinger <cg@exept.de>
parents:
9441
diff
changeset
|
444 |
"Modified: / 18-07-2007 / 14:16:34 / cg" |
6164 | 445 |
! |
446 |
||
447 |
> aTime |
|
448 |
"return true if the receiver is before the argument" |
|
449 |
||
10744
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
450 |
aTime class == self class ifTrue:[ |
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
451 |
^ timeEncoding > aTime timeEncoding |
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
452 |
]. |
6165 | 453 |
^ self getSeconds > aTime getSeconds |
6164 | 454 |
! |
455 |
||
456 |
hash |
|
457 |
"return an integer useful for hashing on times" |
|
458 |
||
10744
afd1835c12d2
comparison did only care for seconds, when used with timeDurations
sr
parents:
10666
diff
changeset
|
459 |
^ timeEncoding |
6164 | 460 |
! ! |
461 |
||
462 |
!Time methodsFor:'converting'! |
|
463 |
||
17751 | 464 |
asNative |
465 |
#swAdded. |
|
466 |
^self |
|
467 |
! |
|
468 |
||
6164 | 469 |
asSeconds |
470 |
"return the number of seconds elapsed since midnight" |
|
471 |
||
472 |
^ self getSeconds |
|
473 |
||
474 |
" |
|
475 |
Time now asSeconds |
|
10933 | 476 |
(TimeDuration days:1) asSeconds |
6164 | 477 |
" |
478 |
! |
|
95 | 479 |
|
699 | 480 |
asTime |
481 |
"return a Time object from the receiver - thats the receiver." |
|
482 |
||
6164 | 483 |
^ self |
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
484 |
! |
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
485 |
|
8796 | 486 |
asTimeDuration |
487 |
"return an TimeDuration object from the receiver, taking the time since midnight." |
|
488 |
||
489 |
^ TimeDuration |
|
490 |
hours:(self hours) minutes:(self minutes) seconds:(self seconds) |
|
491 |
milliseconds:(self milliseconds) |
|
492 |
" |
|
493 |
Time now asTimeDuration |
|
494 |
" |
|
495 |
! |
|
496 |
||
8994
cbf9e75584fe
asTimeStamp -> asTimestamp for AbstractTime protocol compatibility
Stefan Vogel <sv@exept.de>
parents:
8796
diff
changeset
|
497 |
asTimestamp |
cbf9e75584fe
asTimeStamp -> asTimestamp for AbstractTime protocol compatibility
Stefan Vogel <sv@exept.de>
parents:
8796
diff
changeset
|
498 |
"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
|
499 |
The date components are taken from today." |
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
500 |
|
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
501 |
|today| |
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
502 |
|
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
503 |
today := Date today. |
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
504 |
^ 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
|
505 |
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
|
506 |
millisecond:(self milliseconds) |
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
507 |
" |
8994
cbf9e75584fe
asTimeStamp -> asTimestamp for AbstractTime protocol compatibility
Stefan Vogel <sv@exept.de>
parents:
8796
diff
changeset
|
508 |
Time now asTimestamp |
8249
bdcae1232e6d
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
8231
diff
changeset
|
509 |
" |
699 | 510 |
! ! |
511 |
||
6164 | 512 |
!Time methodsFor:'printing & storing'! |
513 |
||
514 |
print12HourFormatOn:aStream |
|
11151 | 515 |
"append a human readable printed representation of the receiver to aStream. |
6164 | 516 |
Format is hh:mm:ss am/pm (i.e. 12-hour american format)." |
517 |
||
518 |
^ self |
|
519 |
printOn:aStream |
|
520 |
format:(self class formatString12us) |
|
521 |
||
522 |
"/ |h m s ampm| |
|
523 |
"/ |
|
524 |
"/ h := self hours. |
|
525 |
"/ |
|
526 |
"/ "/ 0 -> 12 am |
|
527 |
"/ "/ 12 -> 12 pm |
|
528 |
"/ |
|
529 |
"/ h // 12 == 0 ifTrue:[ |
|
530 |
"/ ampm := ' am'. |
|
531 |
"/ ] ifFalse:[ |
|
532 |
"/ ampm := ' pm'. |
|
533 |
"/ ]. |
|
534 |
"/ |
|
535 |
"/ h == 0 ifFalse:[ |
|
536 |
"/ h := h - 1 \\ 12 + 1. |
|
537 |
"/ ]. |
|
538 |
"/ |
|
539 |
"/ h printOn:aStream. |
|
540 |
"/ aStream nextPut:$:. |
|
541 |
"/ m := self minutes. |
|
542 |
"/ (m < 10) ifTrue:[aStream nextPut:$0]. |
|
543 |
"/ m printOn:aStream. |
|
544 |
"/ aStream nextPut:$:. |
|
545 |
"/ s := self seconds. |
|
546 |
"/ (s < 10) ifTrue:[aStream nextPut:$0]. |
|
547 |
"/ s printOn:aStream. |
|
548 |
"/ aStream nextPutAll:ampm |
|
549 |
||
550 |
" |
|
551 |
Time now print12HourFormatOn:Transcript. Transcript cr |
|
552 |
(Time now subtractHours:12) print12HourFormatOn:Transcript. Transcript cr |
|
553 |
(Time hour:24 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
554 |
(Time hour:12 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
555 |
(Time hour:0 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
556 |
0 to:24 do:[:h | |
|
557 |
(Time hour:h minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr |
|
558 |
] |
|
559 |
" |
|
560 |
! |
|
561 |
||
562 |
print24HourFormatOn:aStream |
|
11151 | 563 |
"append a human readable printed representation of the receiver to aStream. |
6164 | 564 |
Format is hh:mm:ss (i.e. 24-hour european format)." |
565 |
||
566 |
^ self |
|
567 |
printOn:aStream |
|
568 |
format:(self class formatString24). |
|
569 |
||
570 |
"/ |h m s| |
|
571 |
"/ |
|
572 |
"/ h := self hours. |
|
573 |
"/ (h < 10) ifTrue:[aStream nextPut:$0]. |
|
574 |
"/ h printOn:aStream. |
|
575 |
"/ aStream nextPut:$:. |
|
576 |
"/ m := self minutes. |
|
577 |
"/ (m < 10) ifTrue:[aStream nextPut:$0]. |
|
578 |
"/ m printOn:aStream. |
|
579 |
"/ aStream nextPut:$:. |
|
580 |
"/ s := self seconds. |
|
581 |
"/ (s < 10) ifTrue:[aStream nextPut:$0]. |
|
582 |
"/ s printOn:aStream |
|
583 |
||
584 |
" |
|
585 |
Time now print24HourFormatOn:Transcript. Transcript cr |
|
586 |
" |
|
587 |
! |
|
588 |
||
589 |
printOn:aStream |
|
11151 | 590 |
"append a human readable printed representation of the receiver to aStream. |
6164 | 591 |
The format is suitable for a human - not meant to be read back. |
592 |
||
593 |
Format is hh:mm:ss either in 12-hour or 24-hour format. |
|
594 |
depending on the setting of LanguageTerritory. |
|
595 |
I dont know what ST-80 does here (12-hour format ?)" |
|
596 |
||
597 |
LanguageTerritory == #us ifTrue:[ |
|
598 |
self print12HourFormatOn:aStream |
|
599 |
] ifFalse:[ |
|
600 |
self print24HourFormatOn:aStream |
|
601 |
] |
|
602 |
||
603 |
" |
|
604 |
Time now printOn:Transcript. Transcript cr |
|
605 |
" |
|
606 |
||
607 |
"Modified: 22.2.1996 / 16:58:30 / cg" |
|
608 |
! |
|
609 |
||
610 |
printString12HourFormat |
|
611 |
"return a printed representation in 12 hour format" |
|
612 |
||
613 |
^ self printStringFormat:(self class formatString12us) |
|
614 |
||
615 |
" |
|
616 |
Time now printString12HourFormat |
|
617 |
" |
|
618 |
! |
|
619 |
||
620 |
printString24HourFormat |
|
621 |
"return a printed representation in 24 hour format" |
|
622 |
||
623 |
^ self printStringFormat:(self class formatString24) |
|
624 |
||
625 |
" |
|
626 |
Time now printString24HourFormat |
|
627 |
" |
|
628 |
||
629 |
! |
|
630 |
||
631 |
shortPrintString |
|
632 |
"dummy - for now" |
|
633 |
||
634 |
^ self printString |
|
635 |
||
636 |
"Created: 20.6.1997 / 17:17:01 / cg" |
|
637 |
! ! |
|
638 |
||
639 |
!Time methodsFor:'private'! |
|
640 |
||
641 |
fromOSTime:osTime |
|
642 |
"set my time, given an osTime" |
|
643 |
||
7513 | 644 |
|i| |
6164 | 645 |
|
7513 | 646 |
i := OperatingSystem computeTimeAndDateFrom:osTime. |
7740 | 647 |
self setHours:(i hours) minutes:(i minutes) seconds:(i seconds) |
6164 | 648 |
|
649 |
"Modified: 1.7.1996 / 15:21:06 / cg" |
|
650 |
! |
|
275 | 651 |
|
3477 | 652 |
getMilliseconds |
6046 | 653 |
"return the number of milliseconds since midnight" |
654 |
||
6165 | 655 |
^ self getSeconds * 1000 |
3477 | 656 |
! |
657 |
||
275 | 658 |
getSeconds |
6046 | 659 |
"return the number of seconds since midnight" |
660 |
||
6165 | 661 |
^ timeEncoding |
275 | 662 |
! |
663 |
||
6165 | 664 |
setHours:h minutes:m seconds:s |
699 | 665 |
"set my time given individual values" |
666 |
||
6165 | 667 |
self setSeconds:(((h\\24) * 60 * 60 ) + (m * 60) + s). |
3477 | 668 |
! |
669 |
||
670 |
setMilliseconds:millis |
|
671 |
"set my time given milliseconds since midnight" |
|
672 |
||
6165 | 673 |
self setSeconds:(millis // 1000) |
699 | 674 |
! |
675 |
||
6904 | 676 |
setSeconds:secs |
677 |
"set my time given seconds since midnight. |
|
9441 | 678 |
Notice the modulo operations here - there cannot be a time beyond 24hours |
6904 | 679 |
(use TimeDuration, if you need that)." |
680 |
||
681 |
secs < 0 ifTrue:[ |
|
682 |
timeEncoding := (24 * 3600) - (secs negated \\ (24 * 3600)) |
|
683 |
] ifFalse:[ |
|
684 |
timeEncoding := secs |
|
685 |
]. |
|
686 |
timeEncoding > (24 * 3600) ifTrue:[ |
|
687 |
timeEncoding := timeEncoding \\ (24 * 3600). |
|
688 |
] |
|
689 |
||
690 |
" |
|
7011 | 691 |
Time basicNew setSeconds:0 |
692 |
Time fromSeconds:3601 |
|
6904 | 693 |
Time now seconds |
694 |
Time now timeEncoding |
|
695 |
(Time now addDays:5) seconds |
|
696 |
(Time now addDays:5) timeEncoding |
|
697 |
" |
|
698 |
! |
|
699 |
||
6165 | 700 |
timeEncoding |
701 |
"the internal encoding is stricktly private, |
|
702 |
and should not be used outside." |
|
703 |
||
704 |
^ timeEncoding |
|
705 |
! |
|
706 |
||
707 |
timeEncoding:encoding |
|
708 |
"the internal encoding is stricktly private, |
|
709 |
and should not be used outside." |
|
710 |
||
711 |
timeEncoding := encoding |
|
699 | 712 |
! ! |
713 |
||
6165 | 714 |
!Time class methodsFor:'documentation'! |
241 | 715 |
|
699 | 716 |
version |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17757
diff
changeset
|
717 |
^ '$Id: Time.st 10517 2010-04-26 18:26:38Z vranyj1 $' |
17751 | 718 |
! |
719 |
||
720 |
version_CVS |
|
17757 | 721 |
^ '§Header: /cvs/stx/stx/libbasic/Time.st,v 1.87 2010/03/31 09:39:45 stefan Exp §' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17757
diff
changeset
|
722 |
! |
17751 | 723 |
|
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17757
diff
changeset
|
724 |
version_SVN |
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17757
diff
changeset
|
725 |
^ '$Id: Time.st 10517 2010-04-26 18:26:38Z vranyj1 $' |
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17757
diff
changeset
|
726 |
! ! |