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