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