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