16356
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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 |
"
|
16357
|
12 |
"{ Package: 'stx:libbasic' }"
|
16356
|
13 |
|
17388
|
14 |
"{ NameSpace: Smalltalk }"
|
|
15 |
|
16356
|
16 |
Time subclass:#TimeDuration
|
|
17 |
instanceVariableNames:''
|
19849
|
18 |
classVariableNames:'DefaultFormatForPrinting TimeDurationZero'
|
16356
|
19 |
poolDictionaries:''
|
|
20 |
category:'Magnitude-Time'
|
|
21 |
!
|
|
22 |
|
|
23 |
!TimeDuration class methodsFor:'documentation'!
|
|
24 |
|
|
25 |
copyright
|
|
26 |
"
|
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
|
28 |
All Rights Reserved
|
|
29 |
|
|
30 |
This software is furnished under a license and may be used
|
|
31 |
only in accordance with the terms of that license and with the
|
|
32 |
inclusion of the above copyright notice. This software may not
|
|
33 |
be provided or otherwise made available to, or used by, any
|
|
34 |
other person. No title to or ownership of the software is
|
|
35 |
hereby transferred.
|
|
36 |
"
|
|
37 |
!
|
|
38 |
|
|
39 |
documentation
|
|
40 |
"
|
|
41 |
Represents a timestap difference.
|
|
42 |
|
|
43 |
DefaultFormatForPrinting if non-nil, allows for the variable printFormat to be overwritten
|
|
44 |
"
|
|
45 |
! !
|
|
46 |
|
|
47 |
!TimeDuration class methodsFor:'instance creation'!
|
|
48 |
|
|
49 |
days:d
|
|
50 |
"return a new TimeDuration representing a duration of d days."
|
|
51 |
|
|
52 |
d == 0 ifTrue:[^ TimeDurationZero].
|
|
53 |
^ self hours:(d*24) minutes:0
|
|
54 |
|
|
55 |
"
|
|
56 |
TimeDuration days:1
|
|
57 |
"
|
|
58 |
!
|
|
59 |
|
|
60 |
days:d hours:h minutes:m seconds:s
|
|
61 |
"return a new TimeDuration representing a duration of d days, h hours, m minutes and s seconds.
|
|
62 |
See also Time now / Date today / Timestamp now."
|
|
63 |
|
|
64 |
^ self basicNew
|
|
65 |
setHours:(d*24)+h minutes:m seconds:s milliseconds:0
|
|
66 |
|
|
67 |
"
|
|
68 |
TimeDuration days:1 hours:2 minutes:33 seconds:0
|
|
69 |
TimeDuration days:4 hours:100 minutes:33 seconds:0
|
|
70 |
"
|
|
71 |
!
|
|
72 |
|
|
73 |
fromHours:hoursInterval
|
|
74 |
"return a new TimeDuration representing a duration of n hours."
|
|
75 |
|
|
76 |
^ self new setSeconds:(hoursInterval * (60*60))
|
|
77 |
|
|
78 |
"
|
|
79 |
TimeDuration fromHours:8
|
|
80 |
"
|
|
81 |
!
|
|
82 |
|
|
83 |
fromMilliseconds:n
|
|
84 |
"return a new TimeDuration representing a duration of n milliseconds."
|
|
85 |
"redefined to disable wrapping at 24hours."
|
|
86 |
|
|
87 |
^ self new setMilliseconds:n
|
|
88 |
|
|
89 |
"
|
|
90 |
TimeDuration fromMilliseconds:500
|
|
91 |
500 milliseconds
|
|
92 |
"
|
|
93 |
|
|
94 |
"Created: / 18-07-2007 / 13:56:25 / cg"
|
|
95 |
!
|
|
96 |
|
|
97 |
fromMinutes:minutesInterval
|
|
98 |
"return a new TimeDuration representing a duration of n minutes."
|
|
99 |
|
|
100 |
^ self new setSeconds:(minutesInterval * 60)
|
|
101 |
|
|
102 |
"
|
|
103 |
TimeDuration fromMinutes:120
|
|
104 |
"
|
|
105 |
!
|
|
106 |
|
|
107 |
fromSeconds:secondsInterval
|
|
108 |
"return a new TimeDuration representing a duration of n seconds."
|
|
109 |
"redefined to disable wrapping at 24hours."
|
|
110 |
|
|
111 |
^ self new setSeconds:secondsInterval
|
|
112 |
|
|
113 |
"
|
|
114 |
TimeDuration fromSeconds:3600
|
|
115 |
"
|
|
116 |
!
|
|
117 |
|
|
118 |
hours:h
|
|
119 |
"return a new TimeDuration representing a duration of h hours.
|
|
120 |
See also Time now / Date today / Timestamp now."
|
|
121 |
|
|
122 |
h == 0 ifTrue:[^ TimeDurationZero].
|
|
123 |
^ self basicNew setHours:h minutes:0 seconds:0 milliseconds:0
|
|
124 |
|
|
125 |
"
|
|
126 |
TimeDuration hours:2
|
|
127 |
TimeDuration hours:100
|
|
128 |
"
|
|
129 |
|
|
130 |
"Created: / 14-07-2007 / 18:15:51 / cg"
|
|
131 |
!
|
|
132 |
|
|
133 |
hours:h minutes:m
|
|
134 |
"return a new TimeDuration representing a duration of h hours and m minutes.
|
|
135 |
See also Time now / Date today / Timestamp now."
|
|
136 |
|
|
137 |
^ self basicNew setHours:h minutes:m seconds:0 milliseconds:0
|
|
138 |
|
|
139 |
"
|
|
140 |
TimeDuration hours:2 minutes:33
|
|
141 |
TimeDuration hours:100 minutes:33
|
|
142 |
"
|
|
143 |
!
|
|
144 |
|
|
145 |
hours:h minutes:m seconds:s millis:millis
|
|
146 |
<resource: #obsolete>
|
|
147 |
"return a new TimeDuration representing a duration of h hours, m minutes, s seconds and millis milliseconds.
|
|
148 |
See also Time now / Date today / Timestamp now."
|
|
149 |
|
|
150 |
self obsoleteMethodWarning:'use hours:minutes:seconds:milliseconds:'.
|
|
151 |
^ self hours:h minutes:m seconds:s milliseconds:millis
|
|
152 |
|
|
153 |
"
|
|
154 |
TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123
|
|
155 |
TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123
|
|
156 |
"
|
|
157 |
!
|
|
158 |
|
|
159 |
hours:h minutes:m seconds:s milliseconds:millis
|
|
160 |
"return a new TimeDuration representing a duration of h hours, m minutes, s seconds and millis milliseconds.
|
|
161 |
See also Time now / Date today / Timestamp now."
|
|
162 |
|
|
163 |
^ self basicNew
|
|
164 |
setHours:h minutes:m seconds:s milliseconds:millis
|
|
165 |
|
|
166 |
"
|
|
167 |
TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123
|
|
168 |
TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123
|
|
169 |
"
|
|
170 |
!
|
|
171 |
|
16872
|
172 |
microseconds:microseconds
|
|
173 |
"return a new TimeDuration representing a duration of microseconds microseconds.
|
|
174 |
Currently we do not support this (we round to millis), but maybe later..."
|
|
175 |
|
|
176 |
microseconds == 0 ifTrue:[^ TimeDurationZero].
|
|
177 |
^ self basicNew setMilliseconds:((microseconds / 1000) rounded).
|
|
178 |
|
|
179 |
"
|
|
180 |
TimeDuration microseconds:2499
|
|
181 |
TimeDuration microseconds:2500
|
18564
|
182 |
TimeDuration microseconds:12345678900
|
16872
|
183 |
"
|
|
184 |
!
|
|
185 |
|
16356
|
186 |
milliseconds:m
|
|
187 |
"return a new TimeDuration representing a duration of m millis.
|
|
188 |
See also Time now / Date today / Timestamp now."
|
|
189 |
|
|
190 |
m == 0 ifTrue:[^ TimeDurationZero].
|
16872
|
191 |
^ self basicNew setMilliseconds:m
|
16356
|
192 |
|
|
193 |
"
|
|
194 |
TimeDuration milliseconds:2
|
|
195 |
"
|
|
196 |
!
|
|
197 |
|
|
198 |
minutes:m
|
|
199 |
"return a new TimeDuration representing a duration of m minutes.
|
|
200 |
See also Time now / Date today / Timestamp now."
|
|
201 |
|
|
202 |
m == 0 ifTrue:[^ TimeDurationZero].
|
|
203 |
^ self basicNew setHours:0 minutes:m seconds:0 milliseconds:0
|
|
204 |
|
|
205 |
"
|
|
206 |
TimeDuration minutes:2
|
|
207 |
"
|
|
208 |
|
|
209 |
"Created: / 06-08-2007 / 15:32:42 / cg"
|
|
210 |
!
|
|
211 |
|
20704
|
212 |
readFrom:aStringOrStream defaultUnit:defaultUnitOrNilArg onError:exceptionBlock
|
16356
|
213 |
"return a new TimeDuration, reading a printed representation from aStream.
|
|
214 |
The format is either:
|
|
215 |
[n 'yr'] [n 'mon'] [n 'w'] [n 'd'] [n 'h'] [n 'm'] [n 's'] [n 'ms']
|
|
216 |
where
|
|
217 |
yr -> year
|
|
218 |
mon -> month
|
|
219 |
w -> week
|
|
220 |
d -> day
|
|
221 |
h -> hour
|
|
222 |
m -> minutes
|
|
223 |
s -> seconds
|
|
224 |
ms -> milliseconds
|
|
225 |
or:
|
|
226 |
h:m:s.ms
|
|
227 |
|
|
228 |
The yr and mon specifiers stand for 365d and 30d respectively.
|
|
229 |
If defaultUnitOrNil is non-nil, a plain number is treated as that;
|
|
230 |
otherwise, a plain number raises an error.
|
20704
|
231 |
Individual components may be negative, as in '1h -10m', which gives 50m
|
|
232 |
or the whole duration may be negative, as in '-(1h 10m)'
|
16356
|
233 |
"
|
|
234 |
|
|
235 |
^ [
|
20704
|
236 |
|seconds millis str val fraction uIdx unit unitChar1 negative defaultUnitOrNil|
|
16356
|
237 |
|
20704
|
238 |
defaultUnitOrNil := defaultUnitOrNilArg.
|
16356
|
239 |
str := aStringOrStream readStream.
|
|
240 |
seconds := 0.
|
|
241 |
millis := 0.
|
20704
|
242 |
|
|
243 |
negative := false.
|
|
244 |
str peek == $- ifTrue:[
|
|
245 |
str next.
|
|
246 |
str peek == $( ifTrue:[
|
|
247 |
|t|
|
|
248 |
str next.
|
|
249 |
t := self readFrom:str defaultUnit:defaultUnitOrNil onError:[^ exceptionBlock value].
|
|
250 |
str skipSeparators.
|
|
251 |
str peek == $) ifTrue:[
|
|
252 |
str next.
|
|
253 |
^ t negated.
|
|
254 |
].
|
|
255 |
^ exceptionBlock value.
|
|
256 |
].
|
|
257 |
negative := true.
|
|
258 |
].
|
|
259 |
|
16356
|
260 |
[
|
|
261 |
|nextCh|
|
|
262 |
|
|
263 |
val := Integer readFrom:str onError:nil.
|
|
264 |
val isNil ifTrue:[^ exceptionBlock value].
|
20704
|
265 |
negative ifTrue:[ val := val negated. negative := false. ].
|
16356
|
266 |
str peek == $: ifTrue:[
|
|
267 |
"/ hour:minutes format
|
|
268 |
str next.
|
|
269 |
seconds := val*3600.
|
|
270 |
val := Integer readFrom:str onError:nil.
|
|
271 |
val isNil ifTrue:[^ exceptionBlock value].
|
|
272 |
seconds := seconds + (val*60).
|
|
273 |
str peek == $: ifTrue:[
|
|
274 |
"/ hour:minutes:seconds format
|
|
275 |
str next.
|
|
276 |
val := Integer readFrom:str onError:nil.
|
|
277 |
val isNil ifTrue:[^ exceptionBlock value].
|
|
278 |
seconds := seconds + val.
|
|
279 |
str peek == $. ifTrue:[
|
|
280 |
"/ hour:minutes:seconds.millis format
|
|
281 |
str next.
|
|
282 |
val := Integer readFrom:str onError:nil.
|
|
283 |
val isNil ifTrue:[^ exceptionBlock value].
|
|
284 |
millis := val.
|
|
285 |
]
|
|
286 |
].
|
|
287 |
^ self fromMilliseconds:(seconds*1000+millis) rounded asInteger.
|
|
288 |
].
|
|
289 |
str peek == $. ifTrue:[
|
|
290 |
str next.
|
|
291 |
fraction := Number readMantissaFrom:str radix:10.
|
|
292 |
val := val + fraction.
|
|
293 |
].
|
|
294 |
str skipSeparators.
|
|
295 |
str atEnd ifTrue:[
|
|
296 |
defaultUnitOrNil isNil ifTrue:[
|
|
297 |
^ exceptionBlock value
|
|
298 |
].
|
|
299 |
"/ no unit given - assume defaultUnit
|
|
300 |
unitChar1 := defaultUnitOrNil.
|
20704
|
301 |
"/ can be only used for one number
|
|
302 |
defaultUnitOrNil := nil.
|
16356
|
303 |
] ifFalse:[
|
|
304 |
unitChar1 := str next.
|
|
305 |
].
|
|
306 |
uIdx := #($y $w $d $h $m $s) indexOf:unitChar1.
|
|
307 |
uIdx == 0 ifTrue:[^ exceptionBlock value].
|
|
308 |
|
|
309 |
str atEnd ifFalse:[
|
|
310 |
nextCh := str peek
|
|
311 |
].
|
|
312 |
|
|
313 |
(unitChar1 == $m and:[nextCh == $s]) ifTrue:[
|
|
314 |
millis := millis + val.
|
|
315 |
] ifFalse:[
|
|
316 |
(unitChar1 == $m and:[nextCh == $o]) ifTrue:[
|
|
317 |
unit := 2592000 "24*60*60*30" "mon"
|
|
318 |
] ifFalse:[
|
|
319 |
unit := #(
|
|
320 |
31536000 "24*60*60*365" "yr"
|
|
321 |
604800 "24*60*60*7" "w"
|
|
322 |
86400 "24*60*60" "d"
|
|
323 |
3600 "h"
|
|
324 |
60 "m"
|
|
325 |
1 ) at:uIdx.
|
|
326 |
].
|
|
327 |
seconds := seconds + (unit * val).
|
|
328 |
].
|
20704
|
329 |
[str atEnd not and:[str peek isSeparator not and:[str peek ~~ $)]]] whileTrue:[ str next].
|
16356
|
330 |
str skipSeparators.
|
20704
|
331 |
"/ done when at the end or a $) is to be read
|
|
332 |
str atEnd or:[aStringOrStream isString not and:[str peek == $)]]
|
16356
|
333 |
] whileFalse.
|
|
334 |
self fromMilliseconds:(seconds*1000+millis) rounded asInteger.
|
20704
|
335 |
] on:Error do:[:ex |
|
16356
|
336 |
|t|
|
|
337 |
|
|
338 |
"/ retry, using inherited readFrom (Object-storeString)
|
|
339 |
t := Object readFrom:aStringOrStream onError:[^ exceptionBlock value].
|
|
340 |
(t isKindOf:TimeDuration) ifFalse:[
|
|
341 |
^ exceptionBlock value
|
|
342 |
].
|
|
343 |
t
|
|
344 |
]
|
|
345 |
|
|
346 |
"
|
20704
|
347 |
TimeDuration readFrom:'2' defaultUnit:$h onError:nil -> 2h
|
|
348 |
TimeDuration readFrom:'100' defaultUnit:$m onError:nil -> 1h 40m
|
|
349 |
TimeDuration readFrom:'100' defaultUnit:$s onError:nil -> 1m 40s
|
|
350 |
TimeDuration readFrom:'0200' defaultUnit:$h onError:nil -> 1w 1d 8h
|
16356
|
351 |
|
|
352 |
TimeDuration readFrom:'1h'
|
|
353 |
TimeDuration readFrom:'1h 35m'
|
|
354 |
TimeDuration readFrom:'25h'
|
|
355 |
TimeDuration readFrom:'3d'
|
|
356 |
TimeDuration readFrom:'1w'
|
|
357 |
TimeDuration readFrom:'120s'
|
20704
|
358 |
TimeDuration readFrom:'1500ms'
|
16356
|
359 |
TimeDuration readFrom:'3ms'
|
|
360 |
TimeDuration readFrom:'1yr 5d'
|
|
361 |
TimeDuration readFrom:'1mon'
|
|
362 |
TimeDuration readFrom:'05:10'
|
|
363 |
TimeDuration readFrom:'05:10:5'
|
|
364 |
TimeDuration readFrom:'05:10:5.150'
|
|
365 |
|
20704
|
366 |
TimeDuration readFrom:'-1h'
|
|
367 |
TimeDuration readFrom:'-1h 10m'
|
|
368 |
TimeDuration readFrom:'1h -10m'
|
|
369 |
TimeDuration readFrom:'-(1h 10m)'
|
|
370 |
|
16356
|
371 |
TimeDuration readFrom:(TimeDuration new storeString)
|
|
372 |
"
|
|
373 |
|
|
374 |
"Modified: / 08-10-2007 / 16:41:48 / cg"
|
|
375 |
!
|
|
376 |
|
|
377 |
readFrom:aStringOrStream onError:exceptionBlock
|
|
378 |
"return a new TimeDuration, reading a printed representation from aStream.
|
|
379 |
The format is [n 'yr'] [n 'mon'] [n 'w'] [n 'd'] [n 'h'] [n 'm'] [n 's'] [n 'ms']
|
|
380 |
where
|
|
381 |
yr -> year
|
|
382 |
mon -> month
|
|
383 |
w -> week
|
|
384 |
d -> day
|
|
385 |
h -> hour
|
|
386 |
m -> minutes
|
|
387 |
s -> seconds
|
|
388 |
ms -> milliseconds
|
|
389 |
The yr and mon specifiers stand for 365d and 30d respectively.
|
|
390 |
If no unit is given (i.e. a plain number string is given), assume seconds.
|
|
391 |
"
|
|
392 |
|
|
393 |
^ self readFrom:aStringOrStream defaultUnit:$s onError:exceptionBlock
|
|
394 |
|
|
395 |
"
|
|
396 |
TimeDuration readFrom:'100' onError:nil
|
|
397 |
TimeDuration readFrom:'100' defaultUnit:$m onError:nil
|
|
398 |
|
|
399 |
TimeDuration readFrom:'1h'
|
|
400 |
TimeDuration readFrom:'1h 35m'
|
|
401 |
TimeDuration readFrom:'25h'
|
|
402 |
TimeDuration readFrom:'3d'
|
|
403 |
TimeDuration readFrom:'1w'
|
|
404 |
TimeDuration readFrom:'120s'
|
|
405 |
TimeDuration readFrom:'1500ms
|
|
406 |
TimeDuration readFrom:'3ms'
|
|
407 |
TimeDuration readFrom:'1yr 5d'
|
|
408 |
TimeDuration readFrom:'1mon'
|
|
409 |
"
|
|
410 |
|
|
411 |
"Modified: / 08-10-2007 / 16:41:48 / cg"
|
|
412 |
!
|
|
413 |
|
|
414 |
seconds:s
|
|
415 |
"return a new TimeDuration representing a duration of s seconds.
|
|
416 |
See also Time now / Date today / Timestamp now."
|
|
417 |
|
|
418 |
s == 0 ifTrue:[^ TimeDurationZero].
|
|
419 |
|
16872
|
420 |
^ self basicNew setMilliseconds:(s * 1000)
|
16356
|
421 |
|
|
422 |
"
|
|
423 |
TimeDuration seconds:2
|
|
424 |
"
|
|
425 |
|
|
426 |
"Created: / 06-08-2007 / 15:32:21 / cg"
|
|
427 |
!
|
|
428 |
|
|
429 |
weeks:w
|
|
430 |
"return a new TimeDuration representing a duration of w weeks."
|
|
431 |
|
|
432 |
^ self days:(w * 7)
|
|
433 |
|
|
434 |
"
|
|
435 |
TimeDuration weeks:1
|
|
436 |
"
|
|
437 |
|
|
438 |
"Created: / 05-09-2011 / 11:18:27 / cg"
|
|
439 |
! !
|
|
440 |
|
|
441 |
!TimeDuration class methodsFor:'class initialization'!
|
|
442 |
|
|
443 |
initialize
|
|
444 |
TimeDurationZero isNil ifTrue:[
|
|
445 |
TimeDurationZero := self basicNew setHours:0 minutes:0 seconds:0 milliseconds:0.
|
|
446 |
]
|
|
447 |
|
|
448 |
"Modified: / 10-05-2011 / 10:31:35 / cg"
|
|
449 |
! !
|
|
450 |
|
|
451 |
!TimeDuration class methodsFor:'constants'!
|
|
452 |
|
|
453 |
zero
|
|
454 |
"return the neutral element for addition (0s)"
|
|
455 |
|
|
456 |
^ TimeDurationZero
|
|
457 |
|
|
458 |
"Modified: 18.7.1996 / 12:26:12 / cg"
|
|
459 |
! !
|
|
460 |
|
|
461 |
!TimeDuration class methodsFor:'format strings'!
|
|
462 |
|
|
463 |
defaultFormatForPrinting
|
|
464 |
^ DefaultFormatForPrinting
|
|
465 |
!
|
|
466 |
|
|
467 |
defaultFormatForPrinting:aString
|
|
468 |
DefaultFormatForPrinting := aString
|
|
469 |
!
|
|
470 |
|
|
471 |
formatString12us
|
|
472 |
"return the format string used to format US times (and other areas)"
|
|
473 |
|
|
474 |
^ '%h:%m:%s.%i'
|
|
475 |
!
|
|
476 |
|
|
477 |
formatString24
|
|
478 |
"return the format string used to format european times (and other areas)"
|
|
479 |
|
|
480 |
^ '%h:%m:%s.%i'
|
|
481 |
! !
|
|
482 |
|
|
483 |
!TimeDuration class methodsFor:'timing evaluation'!
|
|
484 |
|
|
485 |
toRun:aBlock
|
|
486 |
"return the TimeDuration it takes to execute aBlock.
|
|
487 |
A modern variant of Time millisecondsToRun: (which prints itself nicely)"
|
|
488 |
|
16872
|
489 |
^ self microseconds:(Time microsecondsToRun:aBlock).
|
16356
|
490 |
|
|
491 |
"
|
|
492 |
TimeDuration toRun:[ 20000 factorial ]
|
16872
|
493 |
TimeDuration toRun:[ 2000 factorial ]
|
|
494 |
TimeDuration toRun:[ 900 factorial ]
|
16356
|
495 |
"
|
|
496 |
! !
|
|
497 |
|
|
498 |
!TimeDuration methodsFor:'accessing'!
|
|
499 |
|
|
500 |
days
|
|
501 |
"get the number of days"
|
|
502 |
|
20704
|
503 |
^ self
|
|
504 |
possiblyNegatedValueFromTimeEncodingInto:[:t |
|
|
505 |
t // 1000 // 3600 // 24
|
|
506 |
].
|
|
507 |
|
|
508 |
"
|
|
509 |
(Duration days: 9 hours: 1 minutes: 2 seconds: 3) days
|
|
510 |
(Duration days: -9 hours: -1 minutes: -2 seconds: -3) days
|
|
511 |
"
|
16356
|
512 |
!
|
|
513 |
|
|
514 |
hours
|
|
515 |
"get the number of hours.
|
|
516 |
(notice: that is NOT the total number of hours,
|
|
517 |
but the fractional part only. Use this only for printing"
|
|
518 |
|
20704
|
519 |
^ self
|
|
520 |
possiblyNegatedValueFromTimeEncodingInto:[:t |
|
|
521 |
(t // 1000 // 3600 \\ 24)
|
|
522 |
]
|
|
523 |
"
|
|
524 |
(Duration days: 9 hours: 1 minutes: 2 seconds: 3) hours
|
|
525 |
(Duration days: -9 hours: -1 minutes: -2 seconds: -3) hours
|
|
526 |
"
|
16356
|
527 |
!
|
|
528 |
|
|
529 |
milliseconds
|
|
530 |
"get the milliseconds part
|
|
531 |
(notice: that is NOT the total number of millis,
|
19849
|
532 |
but the fractional part only. Use this only for printing.
|
|
533 |
asMilliseconds or getMilliseconds is probably what you want"
|
16356
|
534 |
|
20704
|
535 |
^ self
|
|
536 |
possiblyNegatedValueFromTimeEncodingInto:[:t |
|
|
537 |
t \\ 1000
|
|
538 |
].
|
|
539 |
"
|
|
540 |
(Duration milliseconds:10) milliseconds
|
|
541 |
(Duration milliseconds:-10) milliseconds
|
|
542 |
"
|
16356
|
543 |
|
|
544 |
"Modified: / 05-05-2010 / 14:22:04 / cg"
|
|
545 |
!
|
|
546 |
|
|
547 |
minutes
|
|
548 |
"get the number of minutes.
|
|
549 |
(notice: that is NOT the total number of minutes,
|
|
550 |
but the fractional part only. Use this only for printing"
|
|
551 |
|
20704
|
552 |
^ self
|
|
553 |
possiblyNegatedValueFromTimeEncodingInto:[:t |
|
|
554 |
t // 1000 // 60 \\ 60
|
|
555 |
]
|
|
556 |
|
|
557 |
"
|
|
558 |
(Duration days: 9 hours: 1 minutes: 2 seconds: 3) minutes
|
|
559 |
(Duration days: -9 hours: -1 minutes: -2 seconds: -3) minutes
|
|
560 |
"
|
16356
|
561 |
!
|
|
562 |
|
|
563 |
seconds
|
|
564 |
"get the number of seconds.
|
|
565 |
(notice: that is NOT the total number of seconds,
|
19849
|
566 |
but the fractional part only. Use this only for printing.
|
|
567 |
asSeconds or getSeconds is probably what you want"
|
16356
|
568 |
|
20704
|
569 |
^ self
|
|
570 |
possiblyNegatedValueFromTimeEncodingInto:[:t |
|
|
571 |
t // 1000 \\ 60
|
|
572 |
]
|
|
573 |
|
|
574 |
"
|
|
575 |
(Duration days: 9 hours: 1 minutes: 2 seconds: 3) seconds
|
|
576 |
(Duration days: -9 hours: -1 minutes: -2 seconds: -3) seconds
|
|
577 |
"
|
16356
|
578 |
! !
|
|
579 |
|
|
580 |
!TimeDuration methodsFor:'arithmetic'!
|
|
581 |
|
|
582 |
* aNumber
|
|
583 |
"return a new scaled timeDuration"
|
|
584 |
|
19022
|
585 |
aNumber isNumber ifTrue:[
|
|
586 |
^ self species basicNew
|
|
587 |
setMilliseconds:(self getMilliseconds * aNumber) asInteger
|
|
588 |
].
|
|
589 |
^ aNumber productFromTimeDuration:self
|
|
590 |
|
16356
|
591 |
"
|
|
592 |
(TimeDuration fromString:'10s') * 5
|
20704
|
593 |
(TimeDuration fromString:'10s') * 10
|
|
594 |
(TimeDuration fromString:'10s') * 100
|
|
595 |
(TimeDuration fromString:'10s') * 1000
|
|
596 |
(TimeDuration fromString:'-10s') * 1000
|
16356
|
597 |
"
|
|
598 |
!
|
|
599 |
|
|
600 |
+ aTimeDurationOrNumberOfSeconds
|
|
601 |
"return a new timeDuration"
|
|
602 |
|
|
603 |
aTimeDurationOrNumberOfSeconds isNumber ifTrue:[
|
|
604 |
^ self species basicNew
|
|
605 |
setMilliseconds:(self getMilliseconds + (aTimeDurationOrNumberOfSeconds * 1000) asInteger)
|
|
606 |
].
|
|
607 |
^ self species basicNew
|
|
608 |
setMilliseconds:(self getMilliseconds + aTimeDurationOrNumberOfSeconds getMilliseconds)
|
|
609 |
|
20704
|
610 |
"
|
|
611 |
(TimeDuration fromString:'1m') + (TimeDuration fromString:'10s')
|
|
612 |
1 minutes + 10 seconds
|
|
613 |
"
|
16356
|
614 |
!
|
|
615 |
|
|
616 |
/ aTimeDurationOrNumberOfSeconds
|
|
617 |
"if the argument is a number, return a new timeDuration.
|
|
618 |
Otherwise, return the quotient as a number."
|
|
619 |
|
|
620 |
aTimeDurationOrNumberOfSeconds isNumber ifTrue:[
|
|
621 |
^ self species basicNew
|
|
622 |
setMilliseconds:(self getMilliseconds / aTimeDurationOrNumberOfSeconds) asInteger
|
|
623 |
].
|
19023
|
624 |
aTimeDurationOrNumberOfSeconds isTimeDuration ifTrue:[
|
|
625 |
^ (self getMilliseconds / aTimeDurationOrNumberOfSeconds getMilliseconds)
|
|
626 |
].
|
|
627 |
^ aTimeDurationOrNumberOfSeconds quotientFromTimeDuration:self
|
|
628 |
|
16356
|
629 |
"
|
|
630 |
(TimeDuration fromString:'10s') / (TimeDuration fromString:'5s')
|
|
631 |
(TimeDuration fromString:'10s') / 5
|
|
632 |
"
|
|
633 |
!
|
|
634 |
|
16403
|
635 |
abs
|
|
636 |
timeEncoding >= 0 ifTrue:[
|
|
637 |
^ self.
|
|
638 |
].
|
|
639 |
^ self class new timeEncoding:(timeEncoding negated)
|
|
640 |
|
|
641 |
"
|
|
642 |
(TimeDuration fromSeconds:20000) abs
|
|
643 |
(TimeDuration fromSeconds:-20000) abs
|
|
644 |
"
|
|
645 |
!
|
|
646 |
|
16356
|
647 |
negated
|
|
648 |
^ self class new timeEncoding:(timeEncoding negated)
|
|
649 |
!
|
|
650 |
|
20704
|
651 |
productFromFloat:aFloat
|
|
652 |
"return a new timeDuration"
|
|
653 |
|
|
654 |
^ self productFromNumber:aFloat
|
|
655 |
|
|
656 |
"
|
|
657 |
5.1 * (TimeDuration fromString:'10s')
|
|
658 |
"
|
|
659 |
!
|
|
660 |
|
16356
|
661 |
productFromFraction:aNumber
|
|
662 |
"return a new timeDuration"
|
|
663 |
|
|
664 |
^ self productFromNumber:aNumber
|
|
665 |
|
|
666 |
"
|
|
667 |
(TimeDuration fromString:'10s') * 5
|
|
668 |
"
|
|
669 |
!
|
|
670 |
|
20704
|
671 |
productFromInteger:anInteger
|
|
672 |
"return a new timeDuration"
|
|
673 |
|
|
674 |
^ self productFromNumber:anInteger
|
|
675 |
|
|
676 |
"
|
|
677 |
5 * (TimeDuration fromString:'10s')
|
|
678 |
"
|
|
679 |
!
|
|
680 |
|
16356
|
681 |
productFromNumber:aNumber
|
|
682 |
"return a new timeDuration"
|
|
683 |
|
|
684 |
^ self species basicNew
|
|
685 |
setMilliseconds:(self getMilliseconds * aNumber) asInteger
|
|
686 |
|
|
687 |
"
|
20704
|
688 |
5.1 * (TimeDuration fromString:'10s')
|
16356
|
689 |
"
|
|
690 |
! !
|
|
691 |
|
|
692 |
!TimeDuration methodsFor:'converting'!
|
|
693 |
|
16478
|
694 |
asFixedPoint
|
|
695 |
"answer the duration is seconds"
|
|
696 |
|
|
697 |
^ FixedPoint numerator:timeEncoding denominator:1000 scale:4
|
|
698 |
|
|
699 |
"
|
|
700 |
(10 milliseconds) asFixedPoint
|
|
701 |
"
|
|
702 |
!
|
|
703 |
|
|
704 |
asFloat
|
|
705 |
"answer the duration is seconds"
|
|
706 |
|
|
707 |
^ timeEncoding / 1000.0
|
|
708 |
|
|
709 |
"
|
|
710 |
(10 milliseconds) asFloat
|
|
711 |
"
|
|
712 |
!
|
|
713 |
|
|
714 |
asFraction
|
|
715 |
"answer the duration is seconds"
|
|
716 |
|
|
717 |
^ timeEncoding / 1000
|
|
718 |
|
|
719 |
"
|
|
720 |
(10 milliseconds) asFraction
|
|
721 |
"
|
|
722 |
!
|
|
723 |
|
|
724 |
asInteger
|
|
725 |
"answer the duration is seconds"
|
|
726 |
|
|
727 |
^ self getSeconds
|
|
728 |
!
|
|
729 |
|
|
730 |
asLongFloat
|
|
731 |
"answer the duration is seconds"
|
|
732 |
|
16479
|
733 |
^ timeEncoding / 1000 asLongFloat
|
16478
|
734 |
|
|
735 |
"
|
|
736 |
(10 milliseconds) asLongFloat
|
|
737 |
"
|
|
738 |
!
|
|
739 |
|
|
740 |
asNumber
|
17059
|
741 |
"answer the duration is seconds.
|
|
742 |
Better use the explicit getSeconds"
|
16478
|
743 |
|
|
744 |
^ self getSeconds
|
|
745 |
!
|
|
746 |
|
16356
|
747 |
asTime
|
|
748 |
"return a Time object from the receiver."
|
|
749 |
|
|
750 |
^ Time hours:(self hours) minutes:(self minutes) seconds:(self seconds)
|
|
751 |
!
|
|
752 |
|
|
753 |
asTimeDuration
|
20430
|
754 |
"return a TimeDuration object from the receiver - that's the receiver."
|
16356
|
755 |
|
|
756 |
^ self
|
|
757 |
! !
|
|
758 |
|
|
759 |
!TimeDuration methodsFor:'double dispatching'!
|
|
760 |
|
|
761 |
differenceFromTimestamp:aTimestamp
|
|
762 |
"return the timestamp this timeDuration before aTimestamp"
|
|
763 |
|
|
764 |
^ aTimestamp subtractMilliseconds:(self getMilliseconds)
|
|
765 |
!
|
|
766 |
|
|
767 |
sumFromTimestamp:aTimestamp
|
|
768 |
"return the timestamp this timeDuration after aTimestamp"
|
|
769 |
|
|
770 |
^ aTimestamp addMilliseconds:(self getMilliseconds)
|
|
771 |
! !
|
|
772 |
|
|
773 |
!TimeDuration methodsFor:'printing'!
|
|
774 |
|
|
775 |
addPrintBindingsTo:aDictionary language:languageOrNil
|
|
776 |
"private print support: add bindings for printing to aDictionary.
|
|
777 |
languageOrNil can only be #en or nil for the current language.
|
|
778 |
|
|
779 |
Additional formats available here (for timeDuration) are:
|
|
780 |
%(Hd) hours in day (i.e. 0..23)
|
|
781 |
%(hd) hours in day padded to 2 chars (i.e. 00..23)
|
|
782 |
|
19930
|
783 |
%(yrR) years rounded (i.e. for 730 days, we get 2 asFixedPoint:1 )
|
|
784 |
%(monR) month rounded (i.e. for 45 days, we get 1.5 asFixedPoint:1 )
|
|
785 |
%(w) weeks
|
|
786 |
%(wR) weeks rounded (i.e. for 45 days, we get 6.xxx asFixedPoint:1 )
|
|
787 |
%(dR) days rounded (i.e. for 36 hours, we get 1.5 asFixedPoint:1 )
|
|
788 |
%(dw) days in week
|
|
789 |
%(hR) hours rounded (i.e. for 3h 30m, we get 3.5 asFixedPoint:1 )
|
|
790 |
%(mR) minutes rounded (i.e. for 2m 30s, we get 2.5 asFixedPoint:1 )
|
|
791 |
%(sR) seconds rounded to 1 postDecimal (i.e. for 2s 100ms, we get 2.1 asFixedPoint:1 )
|
16356
|
792 |
"
|
|
793 |
|
19930
|
794 |
|hoursInDay s yearsRounded monthsRounded weeksRounded daysRounded hoursRounded minutesRounded secondsRounded|
|
16356
|
795 |
|
16830
|
796 |
self addBasicPrintBindingsTo:aDictionary language:languageOrNil.
|
16356
|
797 |
aDictionary at:$d put:self days.
|
|
798 |
|
|
799 |
hoursInDay := self hours \\ 24.
|
|
800 |
aDictionary at:#Hd put:(s := hoursInDay printString).
|
|
801 |
aDictionary at:#hd put:(s leftPaddedTo:2 with:$0).
|
|
802 |
|
19930
|
803 |
aDictionary at:#dw put:(self days \\ 7).
|
|
804 |
|
16356
|
805 |
yearsRounded := (self hours / 24 / 365).
|
|
806 |
yearsRounded isInteger ifFalse:[
|
|
807 |
yearsRounded := yearsRounded asFixedPoint:1.
|
|
808 |
].
|
|
809 |
aDictionary at:#yrR put:yearsRounded.
|
|
810 |
|
19930
|
811 |
monthsRounded := self days / 30.
|
16356
|
812 |
monthsRounded isInteger ifFalse:[
|
|
813 |
monthsRounded := monthsRounded asFixedPoint:1.
|
|
814 |
monthsRounded roundedToScale = monthsRounded asInteger ifTrue:[
|
|
815 |
monthsRounded := monthsRounded truncated
|
|
816 |
].
|
|
817 |
].
|
|
818 |
aDictionary at:#monR put:monthsRounded.
|
|
819 |
|
19930
|
820 |
aDictionary at:#w put:(self days // 7).
|
|
821 |
weeksRounded := self days / 7.
|
|
822 |
weeksRounded isInteger ifFalse:[
|
|
823 |
weeksRounded := weeksRounded asFixedPoint:1.
|
|
824 |
weeksRounded roundedToScale = weeksRounded asInteger ifTrue:[
|
|
825 |
weeksRounded := weeksRounded truncated
|
|
826 |
].
|
|
827 |
].
|
|
828 |
aDictionary at:#wR put:weeksRounded.
|
|
829 |
|
16356
|
830 |
daysRounded := (self hours / 24).
|
|
831 |
daysRounded isInteger ifFalse:[
|
|
832 |
daysRounded := daysRounded asFixedPoint:1.
|
|
833 |
daysRounded roundedToScale = daysRounded asInteger ifTrue:[
|
|
834 |
daysRounded := daysRounded truncated
|
|
835 |
].
|
|
836 |
].
|
|
837 |
aDictionary at:#dR put:daysRounded.
|
|
838 |
|
|
839 |
hoursRounded := self hours + (self minutes / 60).
|
|
840 |
hoursRounded isInteger ifFalse:[
|
|
841 |
hoursRounded := hoursRounded asFixedPoint:1.
|
|
842 |
hoursRounded roundedToScale = hoursRounded asInteger ifTrue:[
|
|
843 |
hoursRounded := hoursRounded truncated
|
|
844 |
].
|
|
845 |
].
|
|
846 |
aDictionary at:#hR put:hoursRounded.
|
|
847 |
|
|
848 |
minutesRounded := self minutes + (self seconds / 60).
|
|
849 |
minutesRounded isInteger ifFalse:[
|
|
850 |
minutesRounded := minutesRounded asFixedPoint:1.
|
|
851 |
minutesRounded roundedToScale = minutesRounded asInteger ifTrue:[
|
|
852 |
minutesRounded := minutesRounded truncated
|
|
853 |
].
|
|
854 |
].
|
|
855 |
aDictionary at:#mR put:minutesRounded.
|
|
856 |
|
|
857 |
secondsRounded := self seconds + (self milliseconds / 1000).
|
|
858 |
secondsRounded isInteger ifFalse:[
|
|
859 |
secondsRounded := secondsRounded asFixedPoint:1.
|
|
860 |
secondsRounded roundedToScale = secondsRounded asInteger ifTrue:[
|
|
861 |
secondsRounded := secondsRounded truncated
|
|
862 |
].
|
|
863 |
].
|
|
864 |
aDictionary at:#sR put:secondsRounded.
|
|
865 |
|
|
866 |
"Modified: / 22-02-2011 / 15:55:38 / cg"
|
|
867 |
!
|
|
868 |
|
|
869 |
formatForApproximatePrinting
|
|
870 |
"Return a format which is suitable for a human - not meant to be read back.
|
|
871 |
In contrast to the regular format, this one only gives a rounded approximation
|
|
872 |
of the time duration as useful in information-dialogs or other user-hint-GUI elements.
|
|
873 |
For example, in a timeDuration of more than 2hours, the user might not be interested in
|
|
874 |
individual seconds or even milliseconds.
|
|
875 |
The way this hidding/rounding is done is pure magic heuristics."
|
|
876 |
|
|
877 |
|hours mins secs ms|
|
|
878 |
|
|
879 |
hours := self hours.
|
|
880 |
|
|
881 |
hours >= (24*356*2) ifTrue:[
|
|
882 |
^ '%(yrR)yr'.
|
|
883 |
].
|
|
884 |
hours >= (24*40) ifTrue:[
|
|
885 |
^ '%(monR)mon'.
|
|
886 |
].
|
|
887 |
hours >= 48 ifTrue:[
|
|
888 |
^ '%(dR)d'.
|
|
889 |
].
|
|
890 |
hours >= 24 ifTrue:[
|
|
891 |
^ '%dd %(Hd)h'.
|
|
892 |
].
|
|
893 |
hours >= 2 ifTrue:[
|
|
894 |
^ '%(hR)h'.
|
|
895 |
].
|
|
896 |
hours >= 1 ifTrue:[
|
|
897 |
^ '%Hh %Mm'.
|
|
898 |
].
|
|
899 |
|
|
900 |
"/ no hours
|
|
901 |
mins := self minutes.
|
|
902 |
mins >= 5 ifTrue:[
|
|
903 |
^ '%(mR)m'.
|
|
904 |
].
|
|
905 |
mins >= 1 ifTrue:[
|
|
906 |
^ '%Mm %Ss'.
|
|
907 |
].
|
|
908 |
|
|
909 |
"/ no mins
|
|
910 |
secs := self seconds.
|
|
911 |
secs >= 1 ifTrue:[
|
|
912 |
secs >= 30 ifTrue:[
|
|
913 |
^ '%(S)s'.
|
|
914 |
].
|
|
915 |
^ '%(sR)s'.
|
|
916 |
].
|
|
917 |
|
|
918 |
"/ no secs
|
|
919 |
ms := self milliseconds.
|
|
920 |
ms > 500 ifTrue:[
|
|
921 |
^ '%(sR)s'
|
|
922 |
].
|
|
923 |
|
|
924 |
^ self formatForPrinting
|
|
925 |
|
|
926 |
"
|
|
927 |
(TimeDuration hours:0 minutes:0 seconds:0 millis:12) printStringForApproximation
|
|
928 |
|
|
929 |
(TimeDuration hours:0 minutes:0 seconds:10 millis:123) printStringForApproximation
|
|
930 |
(TimeDuration hours:0 minutes:1 seconds:10 millis:123) printStringForApproximation
|
|
931 |
(TimeDuration hours:0 minutes:2 seconds:10 millis:123) printStringForApproximation
|
|
932 |
(TimeDuration hours:0 minutes:33 seconds:0 millis:123) printStringForApproximation
|
|
933 |
(TimeDuration hours:2 minutes:0 seconds:0 millis:123) printStringForApproximation
|
|
934 |
(TimeDuration hours:2 minutes:33 seconds:0 millis:123) printStringForApproximation
|
|
935 |
(TimeDuration hours:100 minutes:33 seconds:0 millis:123) printStringForApproximation
|
|
936 |
(TimeDuration hours:10000 minutes:33 seconds:0 millis:123) printStringForApproximation
|
|
937 |
(TimeDuration hours:1000000 minutes:33 seconds:0 millis:123) printStringForApproximation
|
|
938 |
|
|
939 |
(TimeDuration hours:2 minutes:33 seconds:0 millis:0) printStringForApproximation
|
|
940 |
(TimeDuration hours:2 minutes:0 seconds:0 millis:0) printStringForApproximation
|
|
941 |
(TimeDuration hours:24 minutes:0 seconds:0 millis:0) printStringForApproximation
|
|
942 |
"
|
|
943 |
!
|
|
944 |
|
|
945 |
formatForPrinting
|
|
946 |
"Return the format for printing"
|
|
947 |
|
|
948 |
DefaultFormatForPrinting notNil ifTrue:[
|
|
949 |
^ DefaultFormatForPrinting
|
|
950 |
].
|
|
951 |
^ self formatForPrinting:false
|
|
952 |
|
|
953 |
"
|
|
954 |
(TimeDuration readFrom:'10h 3s') formatForPrinting
|
|
955 |
(TimeDuration readFrom:'3s') formatForPrinting
|
|
956 |
(TimeDuration readFrom:'1d 2ms') formatForPrinting
|
19930
|
957 |
(TimeDuration readFrom:'1 week') formatForPrinting
|
16356
|
958 |
"
|
|
959 |
!
|
|
960 |
|
|
961 |
formatForShortPrinting
|
|
962 |
"Return the short format for printing (without ms)"
|
|
963 |
|
|
964 |
^ self formatForPrinting:true
|
|
965 |
!
|
|
966 |
|
|
967 |
printAsApproximationOn:aStream
|
|
968 |
"append a human readable printed representation of the receiver to aStream.
|
|
969 |
The format is meant for a human and does not give all information;
|
|
970 |
especially, useless detail is hidden.
|
|
971 |
This means, that seconds are rounded or hidden, if the dT is more than a few minutes;
|
|
972 |
minutes rounded into the hour or hidden, if its more than a few hours etc.
|
|
973 |
The way this is done is pure magic heuristics - let me know, if you have a better algorithm."
|
|
974 |
|
|
975 |
^ self
|
|
976 |
printOn:aStream
|
|
977 |
format:(self formatForApproximatePrinting).
|
|
978 |
|
|
979 |
"
|
|
980 |
(TimeDuration hours:0 minutes:0 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
981 |
(TimeDuration hours:0 minutes:0 seconds:10 milliseconds:123) printAsApproximationOn:Transcript
|
|
982 |
(TimeDuration hours:0 minutes:33 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
983 |
(TimeDuration hours:2 minutes:0 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
984 |
(TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
985 |
(TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
986 |
(TimeDuration hours:10000 minutes:33 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
987 |
(TimeDuration hours:1000000 minutes:33 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
988 |
|
|
989 |
(TimeDuration hours:2 minutes:33 seconds:0) printAsApproximationOn:Transcript
|
|
990 |
(TimeDuration hours:2 minutes:0 seconds:0) printAsApproximationOn:Transcript
|
|
991 |
(TimeDuration hours:24 minutes:0 seconds:0) printAsApproximationOn:Transcript
|
|
992 |
"
|
|
993 |
|
|
994 |
"Modified: / 18-07-2007 / 14:06:17 / cg"
|
|
995 |
!
|
|
996 |
|
|
997 |
printOn:aStream
|
|
998 |
"append a human readable printed representation of the receiver to aStream.
|
|
999 |
The format is suitable for a human - not meant to be read back."
|
|
1000 |
|
|
1001 |
timeEncoding negative ifTrue:[
|
20704
|
1002 |
aStream nextPutAll:'-('.
|
16356
|
1003 |
(self class basicNew timeEncoding:timeEncoding negated) printOn:aStream.
|
20704
|
1004 |
aStream nextPutAll:')'.
|
16356
|
1005 |
^ self
|
|
1006 |
].
|
|
1007 |
^ self
|
|
1008 |
printOn:aStream
|
|
1009 |
format:(self formatForPrinting).
|
|
1010 |
|
|
1011 |
"
|
|
1012 |
TimeDuration hours:0 minutes:0 seconds:0 millis:12
|
|
1013 |
|
|
1014 |
TimeDuration hours:0 minutes:0 seconds:0 millis:123
|
|
1015 |
TimeDuration hours:0 minutes:0 seconds:10 millis:123
|
|
1016 |
TimeDuration hours:0 minutes:33 seconds:0 millis:123
|
|
1017 |
TimeDuration hours:2 minutes:0 seconds:0 millis:123
|
|
1018 |
TimeDuration hours:2 minutes:33 seconds:0 millis:123
|
|
1019 |
TimeDuration hours:100 minutes:33 seconds:0 millis:123
|
|
1020 |
TimeDuration hours:10000 minutes:33 seconds:0 millis:123
|
|
1021 |
TimeDuration hours:1000000 minutes:33 seconds:0 millis:123
|
|
1022 |
|
|
1023 |
TimeDuration hours:2 minutes:33 seconds:0 millis:0
|
|
1024 |
TimeDuration hours:2 minutes:0 seconds:0 millis:0
|
|
1025 |
TimeDuration hours:24 minutes:0 seconds:0 millis:0
|
|
1026 |
|
|
1027 |
(TimeDuration hours:0 minutes:0 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1028 |
(TimeDuration hours:0 minutes:0 seconds:10 millis:123) printStringFormat:'%h:%m:%s'
|
|
1029 |
(TimeDuration hours:0 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1030 |
(TimeDuration hours:2 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1031 |
(TimeDuration hours:100 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1032 |
(TimeDuration hours:10000 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1033 |
(TimeDuration hours:1000000 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1034 |
"
|
|
1035 |
|
|
1036 |
"Modified: / 18-07-2007 / 14:06:17 / cg"
|
|
1037 |
!
|
|
1038 |
|
|
1039 |
printShortOn:aStream
|
|
1040 |
"append a human readable printed representation
|
|
1041 |
of the receiver to aStream (without milliseconds)"
|
|
1042 |
|
|
1043 |
^ self
|
|
1044 |
printOn:aStream
|
|
1045 |
format:(self formatForShortPrinting).
|
|
1046 |
|
|
1047 |
"
|
|
1048 |
TimeDuration hours:0 minutes:0 seconds:0 millis:12
|
|
1049 |
|
|
1050 |
TimeDuration hours:0 minutes:0 seconds:0 millis:123
|
|
1051 |
TimeDuration hours:0 minutes:0 seconds:10 millis:123
|
|
1052 |
TimeDuration hours:0 minutes:33 seconds:0 millis:123
|
|
1053 |
TimeDuration hours:2 minutes:0 seconds:0 millis:123
|
|
1054 |
TimeDuration hours:2 minutes:33 seconds:0 millis:123
|
|
1055 |
TimeDuration hours:100 minutes:33 seconds:0 millis:123
|
|
1056 |
TimeDuration hours:10000 minutes:33 seconds:0 millis:123
|
|
1057 |
TimeDuration hours:1000000 minutes:33 seconds:0 millis:123
|
|
1058 |
|
|
1059 |
TimeDuration hours:2 minutes:33 seconds:0 millis:0
|
|
1060 |
TimeDuration hours:2 minutes:0 seconds:0 millis:0
|
|
1061 |
TimeDuration hours:24 minutes:0 seconds:0 millis:0
|
|
1062 |
|
|
1063 |
(TimeDuration hours:0 minutes:0 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1064 |
(TimeDuration hours:0 minutes:0 seconds:10 millis:123) printStringFormat:'%h:%m:%s'
|
|
1065 |
(TimeDuration hours:0 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1066 |
(TimeDuration hours:2 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1067 |
(TimeDuration hours:100 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1068 |
(TimeDuration hours:10000 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1069 |
(TimeDuration hours:1000000 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1070 |
"
|
|
1071 |
|
|
1072 |
"Modified: / 18-07-2007 / 14:06:17 / cg"
|
|
1073 |
!
|
|
1074 |
|
|
1075 |
printStringForApproximation
|
|
1076 |
"return a human readable printed representation of the receiver to aStream.
|
|
1077 |
The format is meant for a human and does not give all information;
|
|
1078 |
especially, useless detail is hidden.
|
|
1079 |
This means, that seconds are rounded or hidden, if the dT is more than a few minutes;
|
|
1080 |
minutes rounded into the hour or hidden, if its more than a few hours etc.
|
|
1081 |
The way this is done is pure magic heuristics - let me know, if you have a better algorithm."
|
|
1082 |
|
20837
|
1083 |
^ self printStringFormat:(self formatForApproximatePrinting).
|
16356
|
1084 |
|
|
1085 |
"
|
|
1086 |
(TimeDuration hours:0 minutes:0 seconds:0 milliseconds:123) printStringForApproximation
|
|
1087 |
(TimeDuration hours:0 minutes:0 seconds:10 milliseconds:123) printStringForApproximation
|
|
1088 |
(TimeDuration hours:0 minutes:33 seconds:0 milliseconds:123) printStringForApproximation
|
|
1089 |
(TimeDuration hours:2 minutes:0 seconds:0 milliseconds:123) printStringForApproximation
|
|
1090 |
(TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123) printStringForApproximation
|
|
1091 |
(TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123) printStringForApproximation
|
|
1092 |
(TimeDuration hours:10000 minutes:33 seconds:0 milliseconds:123) printStringForApproximation
|
|
1093 |
(TimeDuration hours:1000000 minutes:33 seconds:0 milliseconds:123) printStringForApproximation
|
|
1094 |
|
|
1095 |
(TimeDuration hours:2 minutes:33 seconds:0) printStringForApproximation
|
|
1096 |
(TimeDuration hours:2 minutes:0 seconds:0) printStringForApproximation
|
|
1097 |
(TimeDuration hours:24 minutes:0 seconds:0) printStringForApproximation
|
|
1098 |
"
|
|
1099 |
! !
|
|
1100 |
|
|
1101 |
!TimeDuration methodsFor:'private'!
|
|
1102 |
|
|
1103 |
formatForPrinting:shortFlag
|
|
1104 |
"Return a format which is suitable for a human - not meant to be read back.
|
|
1105 |
If shortFlag is true, some millisecond-info is ommitted for longer times."
|
|
1106 |
|
19930
|
1107 |
|fmt days weeks hours mins secs overAllSeconds millis|
|
16356
|
1108 |
|
|
1109 |
days := self days.
|
19930
|
1110 |
weeks := days // 7.
|
16356
|
1111 |
hours := self hours.
|
|
1112 |
mins := self minutes.
|
|
1113 |
secs := self seconds.
|
|
1114 |
millis := self milliseconds.
|
|
1115 |
|
19930
|
1116 |
weeks > 0 ifTrue:[
|
|
1117 |
fmt := '%(w)w %(dw)d %(Hd)h %Mm'.
|
|
1118 |
secs = 0 ifTrue:[
|
|
1119 |
fmt := '%(w)w %(dw)d %(Hd)h %Mm'.
|
|
1120 |
mins = 0 ifTrue:[
|
|
1121 |
fmt := '%(w)w %(dw)d %(Hd)h'.
|
|
1122 |
(hours \\ 24) = 0 ifTrue:[
|
|
1123 |
fmt := '%(w)w %(dw)d'.
|
|
1124 |
(days \\ 7) = 0 ifTrue:[
|
|
1125 |
fmt := '%(w)w'.
|
|
1126 |
].
|
|
1127 |
].
|
|
1128 |
].
|
|
1129 |
].
|
|
1130 |
] ifFalse:[days > 0 ifTrue:[
|
16356
|
1131 |
fmt := '%dd %(Hd)h %Mm'.
|
|
1132 |
secs = 0 ifTrue:[
|
|
1133 |
fmt := '%dd %(Hd)h %Mm'.
|
|
1134 |
mins = 0 ifTrue:[
|
|
1135 |
fmt := '%dd %(Hd)h'.
|
|
1136 |
(hours \\ 24) = 0 ifTrue:[
|
|
1137 |
fmt := '%dd'.
|
|
1138 |
].
|
|
1139 |
].
|
|
1140 |
].
|
|
1141 |
] ifFalse:[
|
|
1142 |
hours > 0 ifTrue:[
|
|
1143 |
fmt := '%Hh %Mm'.
|
|
1144 |
secs = 0 ifTrue:[
|
|
1145 |
fmt := '%Hh %Mm'.
|
|
1146 |
mins = 0 ifTrue:[
|
|
1147 |
fmt := '%Hh'.
|
|
1148 |
].
|
|
1149 |
].
|
|
1150 |
] ifFalse:[
|
|
1151 |
mins > 0 ifTrue:[
|
|
1152 |
fmt := '%Mm'.
|
|
1153 |
secs = 0 ifTrue:[
|
|
1154 |
fmt := '%Mm'
|
|
1155 |
].
|
|
1156 |
] ifFalse:[
|
|
1157 |
fmt := ''
|
|
1158 |
].
|
|
1159 |
].
|
19930
|
1160 |
]].
|
16356
|
1161 |
((secs ~= 0) or:[millis ~= 0])ifTrue:[
|
|
1162 |
fmt size ~~ 0 ifTrue:[
|
|
1163 |
fmt := fmt , ' '
|
|
1164 |
].
|
|
1165 |
(millis = 0) ifTrue:[
|
|
1166 |
fmt := fmt , '%Ss'
|
|
1167 |
] ifFalse:[
|
|
1168 |
secs = 0 ifTrue:[
|
|
1169 |
fmt := fmt , '%Ims'
|
|
1170 |
] ifFalse:[
|
|
1171 |
shortFlag ifFalse:[
|
|
1172 |
"/ show millis
|
|
1173 |
fmt := fmt , '%S.%is'
|
|
1174 |
] ifTrue:[
|
|
1175 |
"/ only show millis if the number of seconds is small
|
|
1176 |
overAllSeconds := self asSeconds.
|
|
1177 |
overAllSeconds > 2 ifTrue:[
|
|
1178 |
overAllSeconds > 10 ifTrue:[
|
|
1179 |
overAllSeconds > 300 ifTrue:[
|
|
1180 |
fmt := fmt , '%Ss'
|
|
1181 |
] ifFalse:[
|
|
1182 |
fmt := fmt , '%S.%(milli1)s'
|
|
1183 |
]
|
|
1184 |
] ifFalse:[
|
|
1185 |
fmt := fmt , '%S.%(milli2)s'
|
|
1186 |
]
|
|
1187 |
] ifFalse:[
|
|
1188 |
fmt := fmt , '%S.%is'
|
|
1189 |
]
|
|
1190 |
]
|
|
1191 |
]
|
|
1192 |
].
|
|
1193 |
] ifFalse:[
|
|
1194 |
fmt isEmpty ifTrue:[
|
|
1195 |
fmt := '%Ss'
|
|
1196 |
].
|
|
1197 |
].
|
|
1198 |
|
|
1199 |
^ fmt.
|
|
1200 |
|
|
1201 |
"
|
18564
|
1202 |
(TimeDuration hours:0 minutes:0 seconds:0 milliseconds:12) formatForPrinting
|
16356
|
1203 |
|
18564
|
1204 |
(TimeDuration hours:0 minutes:0 seconds:0 milliseconds:123) formatForPrinting
|
|
1205 |
(TimeDuration hours:0 minutes:0 seconds:10 milliseconds:123) formatForPrinting
|
|
1206 |
(TimeDuration hours:0 minutes:33 seconds:0 milliseconds:123) formatForPrinting
|
|
1207 |
(TimeDuration hours:2 minutes:0 seconds:0 milliseconds:123) formatForPrinting
|
|
1208 |
(TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123) formatForPrinting
|
|
1209 |
(TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123) formatForPrinting
|
|
1210 |
(TimeDuration hours:10000 minutes:33 seconds:0 milliseconds:123) formatForPrinting
|
|
1211 |
(TimeDuration hours:1000000 minutes:33 seconds:0 milliseconds:123) formatForPrinting
|
16356
|
1212 |
|
18564
|
1213 |
(TimeDuration hours:0 minutes:38 seconds:22 milliseconds:123) formatForPrinting:true
|
16356
|
1214 |
|
18564
|
1215 |
(TimeDuration hours:2 minutes:33 seconds:0 milliseconds:0) formatForPrinting
|
|
1216 |
(TimeDuration hours:2 minutes:0 seconds:0 milliseconds:0) formatForPrinting
|
|
1217 |
(TimeDuration hours:24 minutes:0 seconds:0 milliseconds:0) formatForPrinting
|
16356
|
1218 |
|
18564
|
1219 |
(TimeDuration hours:0 minutes:0 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
|
|
1220 |
(TimeDuration hours:0 minutes:0 seconds:10 milliseconds:123) printStringFormat:'%h:%m:%s'
|
|
1221 |
(TimeDuration hours:0 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
|
|
1222 |
(TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
|
|
1223 |
(TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
|
|
1224 |
(TimeDuration hours:10000 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
|
|
1225 |
(TimeDuration hours:1000000 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
|
16356
|
1226 |
"
|
|
1227 |
|
|
1228 |
"Modified: / 18-07-2007 / 14:06:17 / cg"
|
|
1229 |
!
|
|
1230 |
|
|
1231 |
getMilliseconds
|
|
1232 |
"return the number of milliseconds"
|
|
1233 |
|
|
1234 |
^ timeEncoding
|
|
1235 |
|
|
1236 |
"Modified: / 18-07-2007 / 13:44:33 / cg"
|
|
1237 |
!
|
|
1238 |
|
|
1239 |
getSeconds
|
|
1240 |
"return the number of seconds"
|
|
1241 |
|
|
1242 |
^ timeEncoding // 1000
|
|
1243 |
|
|
1244 |
"Modified: / 18-07-2007 / 13:44:37 / cg"
|
|
1245 |
!
|
|
1246 |
|
20704
|
1247 |
possiblyNegatedValueFromTimeEncodingInto:aBlock
|
|
1248 |
timeEncoding < 0 ifTrue:[
|
|
1249 |
^ (aBlock value:(timeEncoding negated)) negated
|
|
1250 |
].
|
|
1251 |
^ aBlock value:timeEncoding
|
|
1252 |
!
|
|
1253 |
|
16356
|
1254 |
setHours:h minutes:m seconds:s millis:millis
|
|
1255 |
<resource: #obsolete>
|
|
1256 |
"set my time given individual values"
|
|
1257 |
|
|
1258 |
self obsoleteMethodWarning:'use setHours:minutes:seconds:milliseconds:'.
|
|
1259 |
self setHours:h minutes:m seconds:s milliseconds:millis.
|
|
1260 |
!
|
|
1261 |
|
|
1262 |
setHours:h minutes:m seconds:s milliseconds:millis
|
|
1263 |
"set my time given individual values"
|
|
1264 |
|
|
1265 |
self setMilliseconds:((h * 60 * 60 ) + (m * 60) + s) * 1000 + millis.
|
|
1266 |
!
|
|
1267 |
|
|
1268 |
setMilliseconds:millis
|
|
1269 |
"set my duration given milliseconds.
|
16462
|
1270 |
Notice that (in contrast to Time), there is no modulo operation here.
|
16356
|
1271 |
Duration can be longer than a day"
|
|
1272 |
|
|
1273 |
"/ self assert:(millis isInteger).
|
16462
|
1274 |
timeEncoding := millis rounded
|
16356
|
1275 |
|
|
1276 |
"Modified: / 18-07-2007 / 13:44:16 / cg"
|
|
1277 |
!
|
|
1278 |
|
|
1279 |
setSeconds:secs
|
|
1280 |
"set my timeduration given seconds.
|
|
1281 |
Notice that (in contrast to Time), there is no modulu operation here.
|
|
1282 |
Duration can be longer than a day"
|
|
1283 |
|
|
1284 |
self setMilliseconds:(secs * 1000).
|
|
1285 |
|
|
1286 |
"Modified: / 18-07-2007 / 13:44:24 / cg"
|
|
1287 |
! !
|
|
1288 |
|
|
1289 |
!TimeDuration methodsFor:'testing'!
|
|
1290 |
|
|
1291 |
isTimeDuration
|
|
1292 |
^ true
|
|
1293 |
!
|
|
1294 |
|
|
1295 |
isZero
|
|
1296 |
^ self = self class zero
|
|
1297 |
!
|
|
1298 |
|
|
1299 |
negative
|
|
1300 |
^ timeEncoding < 0
|
|
1301 |
! !
|
|
1302 |
|
|
1303 |
!TimeDuration class methodsFor:'documentation'!
|
|
1304 |
|
|
1305 |
version
|
18564
|
1306 |
^ '$Header$'
|
16356
|
1307 |
!
|
|
1308 |
|
|
1309 |
version_CVS
|
18564
|
1310 |
^ '$Header$'
|
16356
|
1311 |
! !
|
|
1312 |
|
|
1313 |
|
|
1314 |
TimeDuration initialize!
|