22817
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
16356
|
3 |
"
|
|
4 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
16357
|
14 |
"{ Package: 'stx:libbasic' }"
|
16356
|
15 |
|
17388
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
16356
|
18 |
Time subclass:#TimeDuration
|
22843
|
19 |
instanceVariableNames:'additionalPicoseconds'
|
19849
|
20 |
classVariableNames:'DefaultFormatForPrinting TimeDurationZero'
|
16356
|
21 |
poolDictionaries:''
|
|
22 |
category:'Magnitude-Time'
|
|
23 |
!
|
|
24 |
|
|
25 |
!TimeDuration class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
documentation
|
|
42 |
"
|
22868
|
43 |
Represents a time/timestamp difference.
|
|
44 |
|
|
45 |
The resolution is 1 ps.
|
|
46 |
However, such small timedurations are usually only created by physical computations
|
|
47 |
(see goodies/physic).
|
|
48 |
The typical OS-time resolution is in the milli- or microsecond range.
|
|
49 |
External logging hardware may generate timestamps in the micro- or nanosecond range.
|
|
50 |
Picosecond resolution should be good enough for almost any application (at least for the near future).
|
16356
|
51 |
|
22870
|
52 |
Most timedurations only require/have millisecond resolution,
|
|
53 |
so the pico instvar is nil/0 and does not require an aditional largeInteger operation.
|
|
54 |
|
16356
|
55 |
DefaultFormatForPrinting if non-nil, allows for the variable printFormat to be overwritten
|
|
56 |
"
|
|
57 |
! !
|
|
58 |
|
|
59 |
!TimeDuration class methodsFor:'instance creation'!
|
|
60 |
|
|
61 |
days:d
|
|
62 |
"return a new TimeDuration representing a duration of d days."
|
|
63 |
|
|
64 |
d == 0 ifTrue:[^ TimeDurationZero].
|
|
65 |
^ self hours:(d*24) minutes:0
|
|
66 |
|
|
67 |
"
|
|
68 |
TimeDuration days:1
|
|
69 |
"
|
|
70 |
!
|
|
71 |
|
|
72 |
days:d hours:h minutes:m seconds:s
|
|
73 |
"return a new TimeDuration representing a duration of d days, h hours, m minutes and s seconds.
|
|
74 |
See also Time now / Date today / Timestamp now."
|
|
75 |
|
|
76 |
^ self basicNew
|
|
77 |
setHours:(d*24)+h minutes:m seconds:s milliseconds:0
|
|
78 |
|
|
79 |
"
|
|
80 |
TimeDuration days:1 hours:2 minutes:33 seconds:0
|
|
81 |
TimeDuration days:4 hours:100 minutes:33 seconds:0
|
|
82 |
"
|
|
83 |
!
|
|
84 |
|
|
85 |
fromHours:hoursInterval
|
|
86 |
"return a new TimeDuration representing a duration of n hours."
|
|
87 |
|
|
88 |
^ self new setSeconds:(hoursInterval * (60*60))
|
|
89 |
|
|
90 |
"
|
|
91 |
TimeDuration fromHours:8
|
|
92 |
"
|
|
93 |
!
|
|
94 |
|
22843
|
95 |
fromMicroseconds:n
|
|
96 |
"return a new TimeDuration representing a duration of n microseconds."
|
|
97 |
|
|
98 |
^ self new setMicroseconds:n
|
|
99 |
|
|
100 |
"
|
|
101 |
TimeDuration fromMicroseconds:500
|
|
102 |
500 microseconds
|
|
103 |
"
|
|
104 |
|
|
105 |
"Created: / 18-07-2007 / 13:56:25 / cg"
|
|
106 |
!
|
|
107 |
|
16356
|
108 |
fromMilliseconds:n
|
|
109 |
"return a new TimeDuration representing a duration of n milliseconds."
|
|
110 |
"redefined to disable wrapping at 24hours."
|
|
111 |
|
|
112 |
^ self new setMilliseconds:n
|
|
113 |
|
|
114 |
"
|
|
115 |
TimeDuration fromMilliseconds:500
|
|
116 |
500 milliseconds
|
|
117 |
"
|
|
118 |
|
|
119 |
"Created: / 18-07-2007 / 13:56:25 / cg"
|
|
120 |
!
|
|
121 |
|
|
122 |
fromMinutes:minutesInterval
|
|
123 |
"return a new TimeDuration representing a duration of n minutes."
|
|
124 |
|
|
125 |
^ self new setSeconds:(minutesInterval * 60)
|
|
126 |
|
|
127 |
"
|
|
128 |
TimeDuration fromMinutes:120
|
|
129 |
"
|
|
130 |
!
|
|
131 |
|
22843
|
132 |
fromNanoseconds:n
|
|
133 |
"return a new TimeDuration representing a duration of n nanoseconds."
|
|
134 |
|
|
135 |
^ self new setNanoseconds:n
|
|
136 |
|
|
137 |
"
|
|
138 |
TimeDuration fromNanoseconds:500
|
|
139 |
500 nanoseconds
|
|
140 |
"
|
|
141 |
!
|
|
142 |
|
|
143 |
fromPicoseconds:n
|
|
144 |
"return a new TimeDuration representing a duration of n picoseconds."
|
|
145 |
|
|
146 |
^ self new setPicoseconds:n
|
|
147 |
|
|
148 |
"
|
|
149 |
TimeDuration fromPicoseconds:500
|
|
150 |
500 picoseconds
|
|
151 |
"
|
|
152 |
!
|
|
153 |
|
16356
|
154 |
fromSeconds:secondsInterval
|
|
155 |
"return a new TimeDuration representing a duration of n seconds."
|
|
156 |
"redefined to disable wrapping at 24hours."
|
|
157 |
|
|
158 |
^ self new setSeconds:secondsInterval
|
|
159 |
|
|
160 |
"
|
|
161 |
TimeDuration fromSeconds:3600
|
|
162 |
"
|
|
163 |
!
|
|
164 |
|
|
165 |
hours:h
|
|
166 |
"return a new TimeDuration representing a duration of h hours.
|
|
167 |
See also Time now / Date today / Timestamp now."
|
|
168 |
|
|
169 |
h == 0 ifTrue:[^ TimeDurationZero].
|
|
170 |
^ self basicNew setHours:h minutes:0 seconds:0 milliseconds:0
|
|
171 |
|
|
172 |
"
|
|
173 |
TimeDuration hours:2
|
|
174 |
TimeDuration hours:100
|
|
175 |
"
|
|
176 |
|
|
177 |
"Created: / 14-07-2007 / 18:15:51 / cg"
|
|
178 |
!
|
|
179 |
|
|
180 |
hours:h minutes:m seconds:s millis:millis
|
|
181 |
<resource: #obsolete>
|
|
182 |
"return a new TimeDuration representing a duration of h hours, m minutes, s seconds and millis milliseconds.
|
|
183 |
See also Time now / Date today / Timestamp now."
|
|
184 |
|
|
185 |
self obsoleteMethodWarning:'use hours:minutes:seconds:milliseconds:'.
|
|
186 |
^ self hours:h minutes:m seconds:s milliseconds:millis
|
|
187 |
|
|
188 |
"
|
|
189 |
TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123
|
|
190 |
TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123
|
|
191 |
"
|
|
192 |
!
|
|
193 |
|
16872
|
194 |
microseconds:microseconds
|
|
195 |
"return a new TimeDuration representing a duration of microseconds microseconds.
|
22990
|
196 |
Now we support microseconds (even picoseconds) but we still round to milliseconds for backward
|
|
197 |
compatibility with the historic interface."
|
16872
|
198 |
|
|
199 |
microseconds == 0 ifTrue:[^ TimeDurationZero].
|
|
200 |
^ self basicNew setMilliseconds:((microseconds / 1000) rounded).
|
|
201 |
|
|
202 |
"
|
|
203 |
TimeDuration microseconds:2499
|
|
204 |
TimeDuration microseconds:2500
|
18564
|
205 |
TimeDuration microseconds:12345678900
|
16872
|
206 |
"
|
22990
|
207 |
|
|
208 |
"Modified (comment): / 22-05-2018 / 16:54:17 / Stefan Vogel"
|
16872
|
209 |
!
|
|
210 |
|
16356
|
211 |
milliseconds:m
|
|
212 |
"return a new TimeDuration representing a duration of m millis.
|
|
213 |
See also Time now / Date today / Timestamp now."
|
|
214 |
|
|
215 |
m == 0 ifTrue:[^ TimeDurationZero].
|
16872
|
216 |
^ self basicNew setMilliseconds:m
|
16356
|
217 |
|
|
218 |
"
|
|
219 |
TimeDuration milliseconds:2
|
|
220 |
"
|
|
221 |
!
|
|
222 |
|
|
223 |
minutes:m
|
|
224 |
"return a new TimeDuration representing a duration of m minutes.
|
|
225 |
See also Time now / Date today / Timestamp now."
|
|
226 |
|
|
227 |
m == 0 ifTrue:[^ TimeDurationZero].
|
|
228 |
^ self basicNew setHours:0 minutes:m seconds:0 milliseconds:0
|
|
229 |
|
|
230 |
"
|
|
231 |
TimeDuration minutes:2
|
|
232 |
"
|
|
233 |
|
|
234 |
"Created: / 06-08-2007 / 15:32:42 / cg"
|
|
235 |
!
|
|
236 |
|
20704
|
237 |
readFrom:aStringOrStream defaultUnit:defaultUnitOrNilArg onError:exceptionBlock
|
16356
|
238 |
"return a new TimeDuration, reading a printed representation from aStream.
|
|
239 |
The format is either:
|
22843
|
240 |
[n 'yr'] [n 'mon'] [n 'w'] [n 'd'] [n 'h'] [n 'm'] [n 's']
|
|
241 |
([n 'ms'] | [n 'us'] | [n 'ns'] | [n 'ps'])
|
16356
|
242 |
where
|
|
243 |
yr -> year
|
|
244 |
mon -> month
|
|
245 |
w -> week
|
|
246 |
d -> day
|
|
247 |
h -> hour
|
|
248 |
m -> minutes
|
|
249 |
s -> seconds
|
22843
|
250 |
ms -> milliseconds (only one of ms,us,ns or ps can follow)
|
|
251 |
us -> microseconds
|
|
252 |
ns -> nanoseconds
|
|
253 |
ps -> picoseconds
|
16356
|
254 |
or:
|
22843
|
255 |
h:m:s.<ms2>
|
|
256 |
h:m:s.<fract>
|
16356
|
257 |
|
|
258 |
The yr and mon specifiers stand for 365d and 30d respectively.
|
|
259 |
If defaultUnitOrNil is non-nil, a plain number is treated as that;
|
|
260 |
otherwise, a plain number raises an error.
|
20704
|
261 |
Individual components may be negative, as in '1h -10m', which gives 50m
|
|
262 |
or the whole duration may be negative, as in '-(1h 10m)'
|
16356
|
263 |
"
|
|
264 |
|
|
265 |
^ [
|
22843
|
266 |
|seconds millis picos restMillis
|
22857
|
267 |
t1 str val fraction mantissa uIdx unit unitChar1 negative defaultUnitOrNil|
|
16356
|
268 |
|
20704
|
269 |
defaultUnitOrNil := defaultUnitOrNilArg.
|
16356
|
270 |
str := aStringOrStream readStream.
|
|
271 |
seconds := 0.
|
|
272 |
millis := 0.
|
22843
|
273 |
picos := 0.
|
20704
|
274 |
|
|
275 |
negative := false.
|
|
276 |
str peek == $- ifTrue:[
|
|
277 |
str next.
|
|
278 |
str peek == $( ifTrue:[
|
|
279 |
|t|
|
|
280 |
str next.
|
|
281 |
t := self readFrom:str defaultUnit:defaultUnitOrNil onError:[^ exceptionBlock value].
|
|
282 |
str skipSeparators.
|
|
283 |
str peek == $) ifTrue:[
|
|
284 |
str next.
|
|
285 |
^ t negated.
|
|
286 |
].
|
|
287 |
^ exceptionBlock value.
|
|
288 |
].
|
|
289 |
negative := true.
|
|
290 |
].
|
|
291 |
|
16356
|
292 |
[
|
|
293 |
|nextCh|
|
|
294 |
|
|
295 |
val := Integer readFrom:str onError:nil.
|
|
296 |
val isNil ifTrue:[^ exceptionBlock value].
|
20704
|
297 |
negative ifTrue:[ val := val negated. negative := false. ].
|
16356
|
298 |
str peek == $: ifTrue:[
|
|
299 |
"/ hour:minutes format
|
|
300 |
str next.
|
|
301 |
seconds := val*3600.
|
|
302 |
val := Integer readFrom:str onError:nil.
|
|
303 |
val isNil ifTrue:[^ exceptionBlock value].
|
|
304 |
seconds := seconds + (val*60).
|
|
305 |
str peek == $: ifTrue:[
|
|
306 |
"/ hour:minutes:seconds format
|
|
307 |
str next.
|
|
308 |
val := Integer readFrom:str onError:nil.
|
|
309 |
val isNil ifTrue:[^ exceptionBlock value].
|
|
310 |
seconds := seconds + val.
|
22857
|
311 |
(str peek == $. or:[str peek == $,]) ifTrue:[
|
16356
|
312 |
"/ hour:minutes:seconds.millis format
|
|
313 |
str next.
|
22843
|
314 |
"/ the old code here was wrong in assuming that exactly 3 digits
|
|
315 |
"/ are coming; thus hh:mm:ss.1 was interpreted as 1ms (instead of 100)
|
|
316 |
"/ thus: count the zeros...
|
|
317 |
str peek isDigit ifTrue:[
|
22857
|
318 |
"/ fraction := Number readMantissaFrom:str radix:10.
|
|
319 |
"/ fraction isNil ifTrue:[^ exceptionBlock value].
|
|
320 |
"/ ignore the float value; take the fraction
|
|
321 |
|
|
322 |
mantissa := Number readMantissaAndScaleFrom:str radix:10.
|
|
323 |
fraction := (mantissa at:2) / (10 raisedTo:(mantissa at:3)).
|
22858
|
324 |
(mantissa at:3) > 3 ifTrue:[
|
|
325 |
picos := fraction * (1000 * 1000 * 1000 * 1000).
|
|
326 |
millis := picos // (1000 * 1000 * 1000).
|
|
327 |
picos := picos \\ (1000 * 1000 * 1000).
|
|
328 |
] ifFalse:[
|
|
329 |
millis := fraction * 1000.
|
|
330 |
].
|
22843
|
331 |
] ifFalse:[
|
|
332 |
millis := 0
|
|
333 |
].
|
16356
|
334 |
]
|
|
335 |
].
|
22858
|
336 |
t1 := self fromMilliseconds:(seconds*1000+millis).
|
|
337 |
picos notNil ifTrue:[
|
|
338 |
t1 additionalPicoseconds:picos
|
|
339 |
].
|
|
340 |
^ t1
|
16356
|
341 |
].
|
22843
|
342 |
((str peek == $.) or:[(str peek == $,)]) ifTrue:[
|
16356
|
343 |
str next.
|
22857
|
344 |
"/ fraction := Number readMantissaFrom:str radix:10.
|
|
345 |
"/ ignore the float value; take the fraction
|
|
346 |
mantissa := Number readMantissaAndScaleFrom:str radix:10.
|
|
347 |
fraction := (mantissa at:2) / (10 raisedTo:(mantissa at:3)).
|
16356
|
348 |
val := val + fraction.
|
|
349 |
].
|
|
350 |
str skipSeparators.
|
|
351 |
str atEnd ifTrue:[
|
|
352 |
defaultUnitOrNil isNil ifTrue:[
|
|
353 |
^ exceptionBlock value
|
|
354 |
].
|
|
355 |
"/ no unit given - assume defaultUnit
|
|
356 |
unitChar1 := defaultUnitOrNil.
|
20704
|
357 |
"/ can be only used for one number
|
|
358 |
defaultUnitOrNil := nil.
|
16356
|
359 |
] ifFalse:[
|
|
360 |
unitChar1 := str next.
|
|
361 |
].
|
|
362 |
|
|
363 |
str atEnd ifFalse:[
|
|
364 |
nextCh := str peek
|
|
365 |
].
|
|
366 |
|
22843
|
367 |
"/ milli, micro, nano and pico
|
|
368 |
unitChar1 == $µ ifTrue:[ unitChar1 := $u].
|
23238
|
369 |
uIdx := 'munp' indexOf:unitChar1.
|
22843
|
370 |
((uIdx ~~ 0) and:[nextCh == $s]) ifTrue:[
|
|
371 |
unitChar1 == $m ifTrue:[
|
|
372 |
millis := millis + val.
|
16356
|
373 |
] ifFalse:[
|
|
374 |
unit := #(
|
22843
|
375 |
1000000 "us"
|
|
376 |
1000 "ns"
|
|
377 |
1 "ps"
|
|
378 |
) at:uIdx-1.
|
|
379 |
picos := (unit * val).
|
|
380 |
millis := picos // (1000 * 1000 * 1000).
|
|
381 |
picos := picos \\ (1000 * 1000 * 1000).
|
16356
|
382 |
].
|
22843
|
383 |
] ifFalse:[
|
23238
|
384 |
uIdx := 'ywdhms' indexOf:unitChar1.
|
22843
|
385 |
uIdx == 0 ifTrue:[^ exceptionBlock value].
|
|
386 |
|
|
387 |
(unitChar1 == $m and:[nextCh == $s]) ifTrue:[
|
|
388 |
millis := millis + val.
|
|
389 |
] ifFalse:[
|
|
390 |
(unitChar1 == $m and:[nextCh == $o]) ifTrue:[
|
|
391 |
unit := 2592000 "24*60*60*30" "mon"
|
|
392 |
] ifFalse:[
|
|
393 |
unit := #(
|
|
394 |
31536000 "24*60*60*365" "yr"
|
|
395 |
604800 "24*60*60*7" "w"
|
|
396 |
86400 "24*60*60" "d"
|
|
397 |
3600 "h"
|
|
398 |
60 "m"
|
|
399 |
1 ) at:uIdx.
|
|
400 |
].
|
|
401 |
seconds := seconds + (unit * val).
|
|
402 |
].
|
16356
|
403 |
].
|
20704
|
404 |
[str atEnd not and:[str peek isSeparator not and:[str peek ~~ $)]]] whileTrue:[ str next].
|
16356
|
405 |
str skipSeparators.
|
20704
|
406 |
"/ done when at the end or a $) is to be read
|
|
407 |
str atEnd or:[aStringOrStream isString not and:[str peek == $)]]
|
16356
|
408 |
] whileFalse.
|
22843
|
409 |
millis := (seconds*1000) + millis.
|
|
410 |
restMillis := millis - millis truncated.
|
|
411 |
millis := millis truncated.
|
|
412 |
picos := picos + (restMillis * 1000 * 1000 * 1000) truncated.
|
|
413 |
millis := millis + (picos // (1000*1000*1000)).
|
|
414 |
picos := picos \\ (1000*1000*1000).
|
22856
|
415 |
t1 := self fromMilliseconds:millis asInteger.
|
|
416 |
t1 additionalPicoseconds:picos.
|
|
417 |
t1
|
20704
|
418 |
] on:Error do:[:ex |
|
16356
|
419 |
|t|
|
|
420 |
|
|
421 |
"/ retry, using inherited readFrom (Object-storeString)
|
|
422 |
t := Object readFrom:aStringOrStream onError:[^ exceptionBlock value].
|
23238
|
423 |
(t isTimeDuration) ifFalse:[
|
16356
|
424 |
^ exceptionBlock value
|
|
425 |
].
|
|
426 |
t
|
|
427 |
]
|
|
428 |
|
|
429 |
"
|
20704
|
430 |
TimeDuration readFrom:'2' defaultUnit:$h onError:nil -> 2h
|
|
431 |
TimeDuration readFrom:'100' defaultUnit:$m onError:nil -> 1h 40m
|
|
432 |
TimeDuration readFrom:'100' defaultUnit:$s onError:nil -> 1m 40s
|
|
433 |
TimeDuration readFrom:'0200' defaultUnit:$h onError:nil -> 1w 1d 8h
|
16356
|
434 |
|
|
435 |
TimeDuration readFrom:'1h'
|
|
436 |
TimeDuration readFrom:'1h 35m'
|
|
437 |
TimeDuration readFrom:'25h'
|
|
438 |
TimeDuration readFrom:'3d'
|
|
439 |
TimeDuration readFrom:'1w'
|
|
440 |
TimeDuration readFrom:'120s'
|
20704
|
441 |
TimeDuration readFrom:'1500ms'
|
16356
|
442 |
TimeDuration readFrom:'3ms'
|
|
443 |
TimeDuration readFrom:'1yr 5d'
|
|
444 |
TimeDuration readFrom:'1mon'
|
|
445 |
TimeDuration readFrom:'05:10'
|
|
446 |
TimeDuration readFrom:'05:10:5'
|
|
447 |
TimeDuration readFrom:'05:10:5.150'
|
|
448 |
|
20704
|
449 |
TimeDuration readFrom:'-1h'
|
|
450 |
TimeDuration readFrom:'-1h 10m'
|
|
451 |
TimeDuration readFrom:'1h -10m'
|
|
452 |
TimeDuration readFrom:'-(1h 10m)'
|
|
453 |
|
22843
|
454 |
TimeDuration readFrom:'1ms' -> 1ms
|
|
455 |
TimeDuration readFrom:'5us'
|
|
456 |
TimeDuration readFrom:'5µs'
|
|
457 |
TimeDuration readFrom:'5ns'
|
|
458 |
TimeDuration readFrom:'5ps'
|
|
459 |
TimeDuration readFrom:'5005 ps'
|
|
460 |
TimeDuration readFrom:'1.01 s' -> 1.010s
|
|
461 |
TimeDuration readFrom:'1.001 s' -> 1.001s
|
|
462 |
TimeDuration readFrom:'1.0001 s' -> 1.0001s
|
|
463 |
TimeDuration readFrom:'1s 5ns' -> 1.000000005s
|
|
464 |
(TimeDuration readFrom:'1s 5ns') = (TimeDuration fromNanoseconds:(5+1000000000))
|
|
465 |
|
16356
|
466 |
TimeDuration readFrom:(TimeDuration new storeString)
|
|
467 |
"
|
|
468 |
|
|
469 |
"Modified: / 08-10-2007 / 16:41:48 / cg"
|
23238
|
470 |
"Modified: / 27-07-2018 / 11:51:42 / Stefan Vogel"
|
16356
|
471 |
!
|
|
472 |
|
|
473 |
readFrom:aStringOrStream onError:exceptionBlock
|
|
474 |
"return a new TimeDuration, reading a printed representation from aStream.
|
|
475 |
The format is [n 'yr'] [n 'mon'] [n 'w'] [n 'd'] [n 'h'] [n 'm'] [n 's'] [n 'ms']
|
|
476 |
where
|
|
477 |
yr -> year
|
|
478 |
mon -> month
|
|
479 |
w -> week
|
|
480 |
d -> day
|
|
481 |
h -> hour
|
|
482 |
m -> minutes
|
|
483 |
s -> seconds
|
|
484 |
ms -> milliseconds
|
|
485 |
The yr and mon specifiers stand for 365d and 30d respectively.
|
|
486 |
If no unit is given (i.e. a plain number string is given), assume seconds.
|
|
487 |
"
|
|
488 |
|
|
489 |
^ self readFrom:aStringOrStream defaultUnit:$s onError:exceptionBlock
|
|
490 |
|
|
491 |
"
|
|
492 |
TimeDuration readFrom:'100' onError:nil
|
|
493 |
TimeDuration readFrom:'100' defaultUnit:$m onError:nil
|
|
494 |
|
|
495 |
TimeDuration readFrom:'1h'
|
|
496 |
TimeDuration readFrom:'1h 35m'
|
|
497 |
TimeDuration readFrom:'25h'
|
|
498 |
TimeDuration readFrom:'3d'
|
|
499 |
TimeDuration readFrom:'1w'
|
|
500 |
TimeDuration readFrom:'120s'
|
|
501 |
TimeDuration readFrom:'1500ms
|
|
502 |
TimeDuration readFrom:'3ms'
|
|
503 |
TimeDuration readFrom:'1yr 5d'
|
|
504 |
TimeDuration readFrom:'1mon'
|
|
505 |
"
|
|
506 |
|
|
507 |
"Modified: / 08-10-2007 / 16:41:48 / cg"
|
|
508 |
!
|
|
509 |
|
|
510 |
seconds:s
|
|
511 |
"return a new TimeDuration representing a duration of s seconds.
|
|
512 |
See also Time now / Date today / Timestamp now."
|
|
513 |
|
|
514 |
s == 0 ifTrue:[^ TimeDurationZero].
|
|
515 |
|
16872
|
516 |
^ self basicNew setMilliseconds:(s * 1000)
|
16356
|
517 |
|
|
518 |
"
|
|
519 |
TimeDuration seconds:2
|
|
520 |
"
|
|
521 |
|
|
522 |
"Created: / 06-08-2007 / 15:32:21 / cg"
|
|
523 |
!
|
|
524 |
|
|
525 |
weeks:w
|
|
526 |
"return a new TimeDuration representing a duration of w weeks."
|
|
527 |
|
|
528 |
^ self days:(w * 7)
|
|
529 |
|
|
530 |
"
|
|
531 |
TimeDuration weeks:1
|
|
532 |
"
|
|
533 |
|
|
534 |
"Created: / 05-09-2011 / 11:18:27 / cg"
|
24113
|
535 |
!
|
|
536 |
|
|
537 |
years:y
|
|
538 |
"return a new TimeDuration representing a duration of y years."
|
|
539 |
|
|
540 |
^ self days:(y * 365)
|
|
541 |
|
|
542 |
"
|
|
543 |
TimeDuration years:1
|
|
544 |
"
|
|
545 |
|
|
546 |
"Created: / 08-05-2019 / 12:42:16 / Claus Gittinger"
|
16356
|
547 |
! !
|
|
548 |
|
|
549 |
!TimeDuration class methodsFor:'class initialization'!
|
|
550 |
|
|
551 |
initialize
|
|
552 |
TimeDurationZero isNil ifTrue:[
|
|
553 |
TimeDurationZero := self basicNew setHours:0 minutes:0 seconds:0 milliseconds:0.
|
|
554 |
]
|
|
555 |
|
|
556 |
"Modified: / 10-05-2011 / 10:31:35 / cg"
|
|
557 |
! !
|
|
558 |
|
|
559 |
!TimeDuration class methodsFor:'constants'!
|
|
560 |
|
|
561 |
zero
|
|
562 |
"return the neutral element for addition (0s)"
|
|
563 |
|
|
564 |
^ TimeDurationZero
|
|
565 |
|
|
566 |
"Modified: 18.7.1996 / 12:26:12 / cg"
|
|
567 |
! !
|
|
568 |
|
|
569 |
!TimeDuration class methodsFor:'format strings'!
|
|
570 |
|
|
571 |
defaultFormatForPrinting
|
|
572 |
^ DefaultFormatForPrinting
|
|
573 |
!
|
|
574 |
|
|
575 |
defaultFormatForPrinting:aString
|
|
576 |
DefaultFormatForPrinting := aString
|
|
577 |
!
|
|
578 |
|
|
579 |
formatString12us
|
|
580 |
"return the format string used to format US times (and other areas)"
|
|
581 |
|
|
582 |
^ '%h:%m:%s.%i'
|
|
583 |
!
|
|
584 |
|
|
585 |
formatString24
|
|
586 |
"return the format string used to format european times (and other areas)"
|
|
587 |
|
|
588 |
^ '%h:%m:%s.%i'
|
|
589 |
! !
|
|
590 |
|
|
591 |
!TimeDuration class methodsFor:'timing evaluation'!
|
|
592 |
|
|
593 |
toRun:aBlock
|
|
594 |
"return the TimeDuration it takes to execute aBlock.
|
|
595 |
A modern variant of Time millisecondsToRun: (which prints itself nicely)"
|
|
596 |
|
16872
|
597 |
^ self microseconds:(Time microsecondsToRun:aBlock).
|
16356
|
598 |
|
|
599 |
"
|
|
600 |
TimeDuration toRun:[ 20000 factorial ]
|
16872
|
601 |
TimeDuration toRun:[ 2000 factorial ]
|
|
602 |
TimeDuration toRun:[ 900 factorial ]
|
16356
|
603 |
"
|
|
604 |
! !
|
|
605 |
|
22918
|
606 |
!TimeDuration methodsFor:'Compatibility-Squeak'!
|
|
607 |
|
|
608 |
wait
|
24365
|
609 |
"wait the receiver's timeDuration"
|
|
610 |
|
22918
|
611 |
Delay waitFor:self
|
24365
|
612 |
|
|
613 |
"
|
|
614 |
5 seconds wait
|
|
615 |
"
|
|
616 |
|
|
617 |
"Modified (comment): / 26-06-2019 / 11:35:02 / Claus Gittinger"
|
22918
|
618 |
! !
|
|
619 |
|
16356
|
620 |
!TimeDuration methodsFor:'accessing'!
|
|
621 |
|
|
622 |
days
|
22843
|
623 |
"get the (truncated) total number of days.
|
|
624 |
Use this only for printing.
|
|
625 |
Sigh: this is inconsistent: hours, minutes, seconds etc.
|
|
626 |
return the fraction, not the total"
|
16356
|
627 |
|
20704
|
628 |
^ self
|
|
629 |
possiblyNegatedValueFromTimeEncodingInto:[:t |
|
|
630 |
t // 1000 // 3600 // 24
|
|
631 |
].
|
|
632 |
|
|
633 |
"
|
22843
|
634 |
(Duration fromString:'1mon 1d 4h 3m 5s 10ms') days
|
|
635 |
|
22625
|
636 |
(Duration days:9 hours:1 minutes:2 seconds:3) days
|
|
637 |
(Duration days:-9 hours:-1 minutes:-2 seconds:-3) days
|
20704
|
638 |
"
|
16356
|
639 |
!
|
|
640 |
|
|
641 |
hours
|
22625
|
642 |
"get the (truncated) number of hours.
|
|
643 |
notice: that is NOT the total number of hours,
|
22284
|
644 |
but the fractional part only.
|
|
645 |
Use this only for printing"
|
16356
|
646 |
|
20704
|
647 |
^ self
|
|
648 |
possiblyNegatedValueFromTimeEncodingInto:[:t |
|
|
649 |
(t // 1000 // 3600 \\ 24)
|
|
650 |
]
|
|
651 |
"
|
22843
|
652 |
(Duration fromString:'1d 4h 3m 5s 10ms') hours
|
|
653 |
(Duration fromString:'1d 4h 3m 1s 10ms') getHours
|
|
654 |
|
20704
|
655 |
(Duration days: 9 hours: 1 minutes: 2 seconds: 3) hours
|
|
656 |
(Duration days: -9 hours: -1 minutes: -2 seconds: -3) hours
|
|
657 |
"
|
22284
|
658 |
|
|
659 |
"Modified (comment): / 21-09-2017 / 18:53:32 / cg"
|
16356
|
660 |
!
|
|
661 |
|
|
662 |
milliseconds
|
|
663 |
"get the milliseconds part
|
22625
|
664 |
notice: that is NOT the total number of millis,
|
22843
|
665 |
but the fractional part (within the second) only.
|
22284
|
666 |
Use this only for printing.
|
22860
|
667 |
asMilliseconds is probably what you want"
|
16356
|
668 |
|
20704
|
669 |
^ self
|
|
670 |
possiblyNegatedValueFromTimeEncodingInto:[:t |
|
|
671 |
t \\ 1000
|
|
672 |
].
|
|
673 |
"
|
|
674 |
(Duration milliseconds:10) milliseconds
|
|
675 |
(Duration milliseconds:-10) milliseconds
|
22843
|
676 |
|
|
677 |
(Duration fromString:'1s 10ms') milliseconds
|
|
678 |
(Duration fromString:'1s 10ms') getMilliseconds
|
20704
|
679 |
"
|
16356
|
680 |
|
|
681 |
"Modified: / 05-05-2010 / 14:22:04 / cg"
|
22284
|
682 |
"Modified (comment): / 21-09-2017 / 18:53:26 / cg"
|
16356
|
683 |
!
|
|
684 |
|
|
685 |
minutes
|
|
686 |
"get the number of minutes.
|
22625
|
687 |
notice: that is NOT the total number of minutes,
|
22284
|
688 |
but the fractional part only.
|
|
689 |
Use this only for printing"
|
16356
|
690 |
|
20704
|
691 |
^ self
|
|
692 |
possiblyNegatedValueFromTimeEncodingInto:[:t |
|
|
693 |
t // 1000 // 60 \\ 60
|
|
694 |
]
|
|
695 |
|
|
696 |
"
|
22843
|
697 |
(Duration fromString:'1h 3m 5s 10ms') minutes
|
|
698 |
(Duration fromString:'1h 3m 1s 10ms') getMinutes
|
|
699 |
|
20704
|
700 |
(Duration days: 9 hours: 1 minutes: 2 seconds: 3) minutes
|
|
701 |
(Duration days: -9 hours: -1 minutes: -2 seconds: -3) minutes
|
|
702 |
"
|
22284
|
703 |
|
|
704 |
"Modified (comment): / 21-09-2017 / 18:53:22 / cg"
|
16356
|
705 |
!
|
|
706 |
|
22625
|
707 |
picoseconds
|
|
708 |
"get the optional additional picoseconds (0..999999999)
|
|
709 |
notice: that is NOT the total number of picoseconds,
|
22843
|
710 |
but the fractional part (within the second) only.
|
22625
|
711 |
Use this only for printing."
|
22620
|
712 |
|
22843
|
713 |
^ (self milliseconds * 1000 * 1000 * 1000) + (additionalPicoseconds ? 0)
|
22620
|
714 |
!
|
|
715 |
|
16356
|
716 |
seconds
|
|
717 |
"get the number of seconds.
|
22625
|
718 |
notice: that is NOT the total number of seconds,
|
22284
|
719 |
but the fractional part only.
|
|
720 |
Use this only for printing.
|
22860
|
721 |
asSeconds is probably what you want"
|
16356
|
722 |
|
20704
|
723 |
^ self
|
|
724 |
possiblyNegatedValueFromTimeEncodingInto:[:t |
|
|
725 |
t // 1000 \\ 60
|
|
726 |
]
|
|
727 |
|
|
728 |
"
|
22843
|
729 |
(Duration fromString:'1m 5s 10ms') seconds
|
|
730 |
(Duration fromString:'1m 1s 10ms') getSeconds
|
|
731 |
|
20704
|
732 |
(Duration days: 9 hours: 1 minutes: 2 seconds: 3) seconds
|
|
733 |
(Duration days: -9 hours: -1 minutes: -2 seconds: -3) seconds
|
|
734 |
"
|
22284
|
735 |
|
|
736 |
"Modified (comment): / 21-09-2017 / 18:53:13 / cg"
|
16356
|
737 |
! !
|
|
738 |
|
|
739 |
!TimeDuration methodsFor:'arithmetic'!
|
|
740 |
|
|
741 |
* aNumber
|
|
742 |
"return a new scaled timeDuration"
|
|
743 |
|
19022
|
744 |
aNumber isNumber ifTrue:[
|
|
745 |
^ self species basicNew
|
23238
|
746 |
setMilliseconds:(timeEncoding * aNumber)
|
|
747 |
additionalPicoseconds:(additionalPicoseconds ? 0) * aNumber.
|
19022
|
748 |
].
|
22863
|
749 |
|
22843
|
750 |
"/ notice: although noone seems to implement it (currently),
|
|
751 |
"/ there are additional packages which add support (i.e. goodies/physic),
|
|
752 |
"/ so do not remove the call below.
|
19022
|
753 |
^ aNumber productFromTimeDuration:self
|
|
754 |
|
16356
|
755 |
"
|
22843
|
756 |
5 c* (TimeDuration fromString:'10s')
|
|
757 |
|
16356
|
758 |
(TimeDuration fromString:'10s') * 5
|
20704
|
759 |
(TimeDuration fromString:'10s') * 10
|
|
760 |
(TimeDuration fromString:'10s') * 100
|
|
761 |
(TimeDuration fromString:'10s') * 1000
|
|
762 |
(TimeDuration fromString:'-10s') * 1000
|
22621
|
763 |
(TimeDuration fromString:'10s') * (TimeDuration fromString:'10s')
|
22843
|
764 |
|
|
765 |
(TimeDuration fromString:'10ms') * 5
|
|
766 |
(TimeDuration fromString:'10us') * 5
|
16356
|
767 |
"
|
23238
|
768 |
|
|
769 |
"Modified: / 27-07-2018 / 10:32:02 / Stefan Vogel"
|
16356
|
770 |
!
|
|
771 |
|
23233
|
772 |
+ aTimeDurationOrNumberOfSeconds
|
|
773 |
"return a new timeDuration.
|
|
774 |
The argument may be a timeDuration or
|
|
775 |
a number, which is interpreted as seconds."
|
|
776 |
|
|
777 |
|newMillis newPicos|
|
|
778 |
|
|
779 |
aTimeDurationOrNumberOfSeconds isNumber ifTrue:[
|
23238
|
780 |
newMillis := timeEncoding + (aTimeDurationOrNumberOfSeconds * 1000) asInteger.
|
23233
|
781 |
newPicos := additionalPicoseconds ? 0.
|
|
782 |
^ self species basicNew
|
|
783 |
setMilliseconds:newMillis additionalPicoseconds:newPicos
|
|
784 |
].
|
|
785 |
^ aTimeDurationOrNumberOfSeconds sumFromTimeDuration:self.
|
|
786 |
|
|
787 |
"
|
|
788 |
(TimeDuration fromString:'1m') + (TimeDuration fromString:'10s')
|
|
789 |
1 minutes - 10 seconds
|
|
790 |
"
|
|
791 |
|
|
792 |
"Created: / 25-07-2018 / 20:58:17 / Stefan Vogel"
|
23238
|
793 |
"Modified: / 27-07-2018 / 10:32:21 / Stefan Vogel"
|
23233
|
794 |
!
|
|
795 |
|
22863
|
796 |
- aTimeDurationOrNumberOfSeconds
|
|
797 |
"return a new timeDuration.
|
|
798 |
The argument may be a timeDuration or
|
|
799 |
a number, which is interpreted as seconds."
|
|
800 |
|
|
801 |
|newMillis newPicos|
|
|
802 |
|
|
803 |
aTimeDurationOrNumberOfSeconds isNumber ifTrue:[
|
23238
|
804 |
newMillis := timeEncoding - (aTimeDurationOrNumberOfSeconds * 1000) asInteger.
|
22863
|
805 |
newPicos := additionalPicoseconds ? 0.
|
23233
|
806 |
^ self species basicNew
|
|
807 |
setMilliseconds:newMillis additionalPicoseconds:newPicos
|
22863
|
808 |
].
|
23233
|
809 |
^ aTimeDurationOrNumberOfSeconds differenceFromTimeDuration:self.
|
22863
|
810 |
|
|
811 |
"
|
|
812 |
(TimeDuration fromString:'1m') - (TimeDuration fromString:'10s')
|
|
813 |
1 minutes - 10 seconds
|
|
814 |
"
|
23233
|
815 |
|
23238
|
816 |
"Modified: / 27-07-2018 / 10:32:29 / Stefan Vogel"
|
22863
|
817 |
!
|
|
818 |
|
16356
|
819 |
/ aTimeDurationOrNumberOfSeconds
|
|
820 |
"if the argument is a number, return a new timeDuration.
|
|
821 |
Otherwise, return the quotient as a number."
|
|
822 |
|
|
823 |
aTimeDurationOrNumberOfSeconds isNumber ifTrue:[
|
|
824 |
^ self species basicNew
|
23238
|
825 |
setMilliseconds:(timeEncoding / aTimeDurationOrNumberOfSeconds)
|
|
826 |
additionalPicoseconds:((additionalPicoseconds?0) / aTimeDurationOrNumberOfSeconds).
|
16356
|
827 |
].
|
19023
|
828 |
aTimeDurationOrNumberOfSeconds isTimeDuration ifTrue:[
|
23238
|
829 |
^ self getPicoseconds / aTimeDurationOrNumberOfSeconds getPicoseconds.
|
19023
|
830 |
].
|
22863
|
831 |
|
|
832 |
"/ notice: although noone seems to implement it (currently),
|
|
833 |
"/ there are additional packages which add support (i.e. goodies/physic),
|
|
834 |
"/ so do not remove the call below.
|
19023
|
835 |
^ aTimeDurationOrNumberOfSeconds quotientFromTimeDuration:self
|
|
836 |
|
16356
|
837 |
"
|
|
838 |
(TimeDuration fromString:'10s') / (TimeDuration fromString:'5s')
|
|
839 |
(TimeDuration fromString:'10s') / 5
|
|
840 |
"
|
23238
|
841 |
|
|
842 |
"Modified (format): / 27-07-2018 / 10:38:07 / Stefan Vogel"
|
16356
|
843 |
!
|
|
844 |
|
22917
|
845 |
// aTimeDurationOrNumberOfSeconds
|
|
846 |
"if the argument is a number, return a new timeDuration.
|
|
847 |
Otherwise, return the quotient as a number."
|
|
848 |
|
|
849 |
aTimeDurationOrNumberOfSeconds isNumber ifTrue:[
|
|
850 |
^ self species basicNew
|
|
851 |
setSeconds:(self getSeconds // aTimeDurationOrNumberOfSeconds)
|
|
852 |
].
|
|
853 |
aTimeDurationOrNumberOfSeconds isTimeDuration ifTrue:[
|
|
854 |
^ (self getSeconds // aTimeDurationOrNumberOfSeconds getSeconds)
|
|
855 |
].
|
|
856 |
|
|
857 |
"/ notice: although noone seems to implement it (currently),
|
|
858 |
"/ there are additional packages which add support (i.e. goodies/physic),
|
|
859 |
"/ so do not remove the call below.
|
|
860 |
^ (aTimeDurationOrNumberOfSeconds quotientFromTimeDuration:self) truncated
|
|
861 |
|
|
862 |
"
|
|
863 |
(TimeDuration fromString:'10s') // (TimeDuration fromString:'3')
|
|
864 |
(TimeDuration fromString:'10s') // 3
|
|
865 |
|
|
866 |
(TimeDuration fromString:'10s') / (TimeDuration fromString:'3')
|
|
867 |
(TimeDuration fromString:'10s') / 3
|
|
868 |
"
|
|
869 |
!
|
|
870 |
|
16403
|
871 |
abs
|
22866
|
872 |
^ self class new setMilliseconds:(self asExactMilliseconds abs)
|
16403
|
873 |
|
|
874 |
"
|
22865
|
875 |
(TimeDuration fromSeconds:3600) abs
|
|
876 |
(TimeDuration fromSeconds:-3600) abs
|
|
877 |
|
|
878 |
(TimeDuration fromSeconds:20000) abs
|
|
879 |
(TimeDuration fromSeconds:-20000) abs
|
16403
|
880 |
"
|
|
881 |
!
|
|
882 |
|
16356
|
883 |
negated
|
22866
|
884 |
^ self class new setMilliseconds:(self asExactMilliseconds) negated
|
22865
|
885 |
|
|
886 |
"
|
|
887 |
50 nanoseconds negated asNanoseconds
|
|
888 |
1 seconds negated asSeconds
|
|
889 |
"
|
23929
|
890 |
!
|
|
891 |
|
|
892 |
squared
|
|
893 |
"answer a float representing my value in seconds - squared.
|
|
894 |
Used to compute e.g. variance and standard deviation."
|
|
895 |
|
|
896 |
^ self secondsAsFloat squared
|
|
897 |
|
|
898 |
"
|
|
899 |
50 nanoseconds squared
|
|
900 |
1 seconds squared
|
|
901 |
"
|
|
902 |
|
|
903 |
"Created: / 15-03-2019 / 17:57:50 / Stefan Vogel"
|
16356
|
904 |
! !
|
|
905 |
|
24159
|
906 |
!TimeDuration methodsFor:'comparing'!
|
|
907 |
|
|
908 |
< something
|
|
909 |
|otherTimeEncoding|
|
|
910 |
|
|
911 |
something class == self class ifTrue:[
|
|
912 |
otherTimeEncoding :=something timeEncoding.
|
|
913 |
timeEncoding = otherTimeEncoding ifTrue:[
|
|
914 |
^ (additionalPicoseconds ? 0) < something additionalPicoseconds
|
|
915 |
].
|
|
916 |
^ timeEncoding < otherTimeEncoding
|
|
917 |
].
|
|
918 |
^ super < something
|
|
919 |
|
|
920 |
"Created: / 26-05-2019 / 10:02:59 / Claus Gittinger"
|
|
921 |
!
|
|
922 |
|
|
923 |
= something
|
|
924 |
something class == self class ifTrue:[
|
|
925 |
^ timeEncoding = something timeEncoding
|
|
926 |
and:[(additionalPicoseconds ? 0) = something additionalPicoseconds]
|
|
927 |
].
|
|
928 |
^ super = something
|
|
929 |
|
|
930 |
"Created: / 26-05-2019 / 09:40:44 / Claus Gittinger"
|
|
931 |
!
|
|
932 |
|
|
933 |
hash
|
|
934 |
^ timeEncoding bitXor:(additionalPicoseconds ? 0)
|
|
935 |
|
|
936 |
"Created: / 26-05-2019 / 09:45:19 / Claus Gittinger"
|
|
937 |
! !
|
|
938 |
|
16356
|
939 |
!TimeDuration methodsFor:'converting'!
|
|
940 |
|
22862
|
941 |
asExactHours
|
|
942 |
"answer the duration as hours.
|
|
943 |
In contrast to asTruncatedHours, which returns them truncated,
|
|
944 |
this may return a non-integer value."
|
|
945 |
|
|
946 |
^ self asExactSeconds / (3600)
|
|
947 |
|
|
948 |
"
|
|
949 |
(2 hours + 10 minutes + 30 seconds) asExactHours
|
|
950 |
(2 hours + 10 minutes + 30 seconds) asExactHours asFloat
|
|
951 |
(2 hours + 10 minutes + 30 seconds) asTruncatedHours
|
|
952 |
(2 hours + 10 minutes) asExactHours asFloat
|
|
953 |
(2 hours + 10 minutes) asTruncatedHours
|
|
954 |
10 milliseconds asExactHours
|
|
955 |
10 milliseconds asExactHours asFloat
|
|
956 |
"
|
|
957 |
!
|
|
958 |
|
22860
|
959 |
asExactMicroseconds
|
|
960 |
"return the exact number of mcroseconds.
|
|
961 |
In contrast to asMicroSeconds, which returns them truncated,
|
|
962 |
this may return a non-integer value."
|
|
963 |
|
|
964 |
additionalPicoseconds isNil ifTrue:[
|
|
965 |
^ (timeEncoding * 1000) "/ millis as micros
|
|
966 |
].
|
|
967 |
^ (timeEncoding * 1000) "/ millis as micros
|
|
968 |
+ (additionalPicoseconds / (1000 * 1000)) "/ picos as microseconds.
|
|
969 |
|
|
970 |
"
|
|
971 |
40 milliseconds asExactMicroseconds
|
|
972 |
40 microseconds asExactMicroseconds
|
|
973 |
40 nanoseconds asExactMicroseconds
|
|
974 |
40 picoseconds asExactMicroseconds
|
|
975 |
"
|
|
976 |
|
|
977 |
"Created: / 21-09-2017 / 18:52:26 / cg"
|
|
978 |
!
|
|
979 |
|
|
980 |
asExactMilliseconds
|
|
981 |
"return the exact number of milliseconds.
|
|
982 |
In contrast to asMilliSeconds, which returns them truncated,
|
|
983 |
this may return a non-integer value."
|
|
984 |
|
|
985 |
additionalPicoseconds isNil ifTrue:[
|
23230
|
986 |
^ timeEncoding "/ millis
|
22860
|
987 |
].
|
23230
|
988 |
^ timeEncoding "/ millis
|
|
989 |
+ (additionalPicoseconds / (1000 * 1000 * 1000)) "/ picos as milliseconds.
|
22860
|
990 |
|
|
991 |
"
|
|
992 |
40 milliseconds asExactMilliseconds
|
|
993 |
40 microseconds asExactMilliseconds
|
|
994 |
40 nanoseconds asExactMilliseconds
|
|
995 |
40 picoseconds asExactMilliseconds
|
|
996 |
"
|
|
997 |
|
|
998 |
"Created: / 21-09-2017 / 18:52:26 / cg"
|
23230
|
999 |
"Modified (format): / 24-07-2018 / 16:27:10 / Stefan Vogel"
|
22860
|
1000 |
!
|
|
1001 |
|
22862
|
1002 |
asExactMinutes
|
|
1003 |
"answer the duration as minutes.
|
|
1004 |
In contrast to asTruncatedMinutes, which returns them truncated,
|
|
1005 |
this may return a non-integer value."
|
|
1006 |
|
|
1007 |
^ self asExactSeconds / 60
|
|
1008 |
|
|
1009 |
"
|
|
1010 |
(2 hours + 10 minutes + 30 seconds) asExactMinutes
|
|
1011 |
(2 hours + 10 minutes + 30 seconds) asExactMinutes asFloat
|
|
1012 |
(2 hours + 10 minutes + 30 seconds) asTruncatedMinutes
|
|
1013 |
(2 hours + 10 minutes) asExactMinutes
|
|
1014 |
(2 hours + 10 minutes) asTruncatedMinutes
|
|
1015 |
10 milliseconds asExactMinutes
|
|
1016 |
10 milliseconds asExactMinutes asFloat
|
|
1017 |
"
|
|
1018 |
!
|
|
1019 |
|
22860
|
1020 |
asExactNanoseconds
|
|
1021 |
"return the exact number of nanoseconds.
|
|
1022 |
In contrast to asNanoSeconds, which returns them truncated,
|
|
1023 |
this may return a non-integer value."
|
|
1024 |
|
|
1025 |
additionalPicoseconds isNil ifTrue:[
|
|
1026 |
^ (timeEncoding * 1000 * 1000) "/ millis as nanos
|
|
1027 |
].
|
|
1028 |
^ (timeEncoding * 1000 * 1000) "/ millis as nanos
|
|
1029 |
+ (additionalPicoseconds / (1000)) "/ picos as nanoseconds.
|
|
1030 |
|
|
1031 |
"
|
|
1032 |
40 milliseconds asExactNanoseconds
|
|
1033 |
40 microseconds asExactNanoseconds
|
|
1034 |
40 nanoseconds asExactNanoseconds
|
|
1035 |
40 picoseconds asExactNanoseconds
|
|
1036 |
"
|
|
1037 |
|
|
1038 |
"Created: / 21-09-2017 / 18:52:26 / cg"
|
|
1039 |
!
|
|
1040 |
|
|
1041 |
asExactSeconds
|
|
1042 |
"return the exact number of seconds.
|
|
1043 |
In contrast to asSeconds, which returns them truncated,
|
|
1044 |
this may return a non-integer value."
|
|
1045 |
|
|
1046 |
additionalPicoseconds isNil ifTrue:[
|
|
1047 |
^ (timeEncoding / 1000) "/ millis as seconds
|
|
1048 |
].
|
|
1049 |
^ (timeEncoding / 1000) "/ millis as seconds
|
22861
|
1050 |
+ (additionalPicoseconds / (1000 * 1000 * 1000 * 1000)) "/ picos as seconds.
|
22860
|
1051 |
|
|
1052 |
"
|
22861
|
1053 |
1.5 seconds asExactSeconds
|
|
1054 |
40 seconds asExactSeconds
|
22860
|
1055 |
40 milliseconds asExactSeconds
|
|
1056 |
40 microseconds asExactSeconds
|
|
1057 |
40 nanoseconds asExactSeconds
|
|
1058 |
"
|
|
1059 |
|
|
1060 |
"Created: / 21-09-2017 / 18:52:26 / cg"
|
|
1061 |
!
|
|
1062 |
|
16478
|
1063 |
asFixedPoint
|
22860
|
1064 |
<resource: #obsolete>
|
|
1065 |
"answer the duration in seconds as a fixedPoint number.
|
|
1066 |
This method has a bad name (a historic leftover);
|
|
1067 |
Please change any sender to use secondsAsFixedPoint"
|
16478
|
1068 |
|
22860
|
1069 |
^ self secondsAsFixedPoint
|
16478
|
1070 |
|
|
1071 |
"
|
22625
|
1072 |
(10 milliseconds) asFixedPoint
|
|
1073 |
(10 milliseconds) asFixedPoint asFixedPoint:3
|
|
1074 |
"
|
|
1075 |
|
|
1076 |
"Modified (comment): / 14-09-2017 / 15:15:24 / stefan"
|
|
1077 |
!
|
|
1078 |
|
|
1079 |
asFixedPoint:scale
|
22860
|
1080 |
<resource: #obsolete>
|
|
1081 |
"answer the duration in seconds as a fixedPoint number with given scale.
|
|
1082 |
This method has a bad name (a historic leftover);
|
|
1083 |
Please change any sender to use secondsAsFixedPoint"
|
22857
|
1084 |
|
22860
|
1085 |
^ self secondsAsFixedPoint:scale
|
22625
|
1086 |
|
|
1087 |
"
|
22860
|
1088 |
(1000 milliseconds) secondsAsFixedPoint
|
|
1089 |
(10 milliseconds) secondsAsFixedPoint
|
|
1090 |
(10 microseconds) secondsAsFixedPoint scale:8
|
|
1091 |
(10 nanoseconds) secondsAsFixedPoint scale:8
|
|
1092 |
(1000001 microseconds) secondsAsFixedPoint scale:8
|
16478
|
1093 |
"
|
22255
|
1094 |
|
|
1095 |
"Modified (comment): / 14-09-2017 / 15:15:24 / stefan"
|
16478
|
1096 |
!
|
|
1097 |
|
|
1098 |
asFloat
|
22860
|
1099 |
<resource: #obsolete>
|
|
1100 |
"answer the duration in seconds as a float.
|
|
1101 |
This method has a bad name (a historic leftover);
|
|
1102 |
Please change any sender to use secondsAsFloat"
|
22857
|
1103 |
|
22860
|
1104 |
^ self secondsAsFloat
|
16478
|
1105 |
|
|
1106 |
"
|
22860
|
1107 |
(1000 milliseconds) asFloat
|
22625
|
1108 |
(10 milliseconds) asFloat
|
22857
|
1109 |
(10 microseconds) asFloat
|
|
1110 |
(10 nanoseconds) asFloat
|
|
1111 |
(1000001 microseconds) asFloat
|
16478
|
1112 |
"
|
22255
|
1113 |
|
|
1114 |
"Modified (comment): / 14-09-2017 / 15:15:18 / stefan"
|
16478
|
1115 |
!
|
|
1116 |
|
|
1117 |
asFraction
|
22860
|
1118 |
<resource: #obsolete>
|
|
1119 |
"answer the duration in seconds as a fraction
|
|
1120 |
(might return an integer, if the duration is an exact multiple
|
|
1121 |
of millis).
|
|
1122 |
This method has a bad name (a historic leftover);
|
|
1123 |
Please change any sender to use secondsAsFraction"
|
22857
|
1124 |
|
22860
|
1125 |
^ self secondsAsFraction
|
16478
|
1126 |
|
|
1127 |
"
|
22860
|
1128 |
(1000 milliseconds) asFraction
|
22625
|
1129 |
(10 milliseconds) asFraction
|
22857
|
1130 |
(10 microseconds) asFraction
|
|
1131 |
(10 nanoseconds) asFraction
|
|
1132 |
(1000001 microseconds) asFraction asFloat
|
16478
|
1133 |
"
|
22255
|
1134 |
|
22493
|
1135 |
"Modified (comment): / 19-01-2018 / 17:29:24 / stefan"
|
16478
|
1136 |
!
|
|
1137 |
|
|
1138 |
asInteger
|
22860
|
1139 |
<resource: #obsolete>
|
|
1140 |
"answer the duration as (truncated) integer seconds.
|
|
1141 |
Better use the explicit asTruncatedSeconds"
|
16478
|
1142 |
|
22860
|
1143 |
^ self asTruncatedSeconds
|
21506
|
1144 |
|
|
1145 |
"
|
22625
|
1146 |
10 milliseconds asInteger
|
21506
|
1147 |
"
|
|
1148 |
|
22255
|
1149 |
"Modified (comment): / 14-09-2017 / 15:14:49 / stefan"
|
22285
|
1150 |
"Modified (comment): / 21-09-2017 / 18:57:43 / cg"
|
16478
|
1151 |
!
|
|
1152 |
|
|
1153 |
asLongFloat
|
22860
|
1154 |
<resource: #obsolete>
|
22625
|
1155 |
"answer the duration as longfloat seconds.
|
22860
|
1156 |
This method has a bad name (a historic leftover);
|
|
1157 |
Please change any sender to use secondsAsFloat"
|
22857
|
1158 |
|
22860
|
1159 |
^ self secondsAsLongFloat
|
16478
|
1160 |
|
|
1161 |
"
|
22857
|
1162 |
(10 milliseconds) asLongFloat
|
|
1163 |
(10 microseconds) asLongFloat
|
|
1164 |
(10 nanoseconds) asLongFloat
|
|
1165 |
(1000001 microseconds) asLongFloat
|
16478
|
1166 |
"
|
|
1167 |
!
|
|
1168 |
|
22843
|
1169 |
asMicroseconds
|
22860
|
1170 |
"answer the duration as microseconds (truncated).
|
|
1171 |
Values smaller than 1 us will be returned as 0"
|
22843
|
1172 |
|
22860
|
1173 |
^ self asTruncatedMicroseconds
|
22843
|
1174 |
|
|
1175 |
"
|
22860
|
1176 |
100 nanoseconds asTruncatedMicroseconds
|
|
1177 |
100 nanoseconds asExactMicroseconds
|
|
1178 |
|
22857
|
1179 |
10 milliseconds asMicroseconds
|
|
1180 |
1.5 milliseconds asMicroseconds
|
|
1181 |
10 seconds asMicroseconds
|
22843
|
1182 |
"
|
|
1183 |
!
|
|
1184 |
|
|
1185 |
asMilliseconds
|
22860
|
1186 |
"answer the duration as milliseconds (truncated).
|
|
1187 |
Values smaller than 1 ms will be returned as 0"
|
22843
|
1188 |
|
22860
|
1189 |
^ self asTruncatedMilliseconds
|
22843
|
1190 |
|
|
1191 |
"
|
22860
|
1192 |
10 microseconds asTruncatedMilliseconds
|
|
1193 |
10 microseconds asExactMilliseconds
|
|
1194 |
10 microseconds asMilliseconds
|
|
1195 |
|
22843
|
1196 |
10 milliseconds asMilliseconds
|
|
1197 |
10 seconds asMilliseconds
|
|
1198 |
"
|
|
1199 |
!
|
|
1200 |
|
|
1201 |
asNanoseconds
|
22860
|
1202 |
"answer the duration as nanoseconds (truncated).
|
|
1203 |
Values smaller than 1 ns will be returned as 0"
|
22843
|
1204 |
|
22860
|
1205 |
^ self asTruncatedNanoseconds
|
22843
|
1206 |
|
|
1207 |
"
|
22860
|
1208 |
10 picoseconds asTruncatedNanoseconds
|
|
1209 |
10 picoseconds asExactNanoseconds
|
22843
|
1210 |
"
|
|
1211 |
!
|
|
1212 |
|
16478
|
1213 |
asNumber
|
22860
|
1214 |
<resource: #obsolete>
|
|
1215 |
"answer the duration as seconds.
|
|
1216 |
This method has a bad name (a historic leftover);
|
|
1217 |
Please change any sender to use asTruncatedSeconds or
|
|
1218 |
asExactSeconds, depending on what is wanted."
|
16478
|
1219 |
|
22843
|
1220 |
^ self asExactSeconds
|
22255
|
1221 |
|
|
1222 |
"Modified (comment): / 14-09-2017 / 15:15:00 / stefan"
|
22285
|
1223 |
"Modified: / 21-09-2017 / 18:57:57 / cg"
|
16478
|
1224 |
!
|
|
1225 |
|
22858
|
1226 |
asPicoseconds
|
22860
|
1227 |
"answer the duration as picoseconds (truncated).
|
|
1228 |
Because the smallest representable timeDuration is 1ps,
|
|
1229 |
there is no distinction between truncated and exact picos."
|
22857
|
1230 |
|
22860
|
1231 |
^ (timeEncoding * 1000000000) + ((additionalPicoseconds ? 0))
|
22857
|
1232 |
|
|
1233 |
"
|
|
1234 |
10 milliseconds asPicoeconds
|
|
1235 |
10 seconds asPicoeconds
|
|
1236 |
"
|
|
1237 |
!
|
|
1238 |
|
22843
|
1239 |
asSeconds
|
22860
|
1240 |
"answer the duration as seconds (truncated).
|
|
1241 |
Values smaller than 1 s will be returned as 0.
|
|
1242 |
|
22843
|
1243 |
To get the exact number, use asExactSeconds.
|
22860
|
1244 |
Please change senders to use asTruncatedSeconds
|
|
1245 |
to make this truncation explicit (and obvious when reading code).
|
|
1246 |
For compatibility (both backward and with other smalltalks),
|
|
1247 |
asSeconds returns the TRUNCATED integer value
|
|
1248 |
(many senders assume that an integer is returned)"
|
22843
|
1249 |
|
|
1250 |
^ self asTruncatedSeconds
|
|
1251 |
|
|
1252 |
"
|
|
1253 |
10 milliseconds asSeconds
|
|
1254 |
10 nanoseconds asSeconds
|
|
1255 |
10 milliseconds asExactSeconds
|
|
1256 |
10 nanoseconds asExactSeconds
|
|
1257 |
2 minutes asSeconds
|
|
1258 |
"
|
|
1259 |
!
|
|
1260 |
|
16356
|
1261 |
asTimeDuration
|
20430
|
1262 |
"return a TimeDuration object from the receiver - that's the receiver."
|
16356
|
1263 |
|
|
1264 |
^ self
|
22843
|
1265 |
!
|
|
1266 |
|
22862
|
1267 |
asTruncatedHours
|
|
1268 |
"answer the duration as hours (truncated).
|
|
1269 |
Values smaller than 1 h will be returned as 0.
|
|
1270 |
|
|
1271 |
To get the exact number, use asExactHours."
|
|
1272 |
|
|
1273 |
^ self asTruncatedSeconds // (3600)
|
|
1274 |
|
|
1275 |
"
|
|
1276 |
(2 hours + 10 minutes) asTruncatedHours
|
|
1277 |
(2 hours + 10 minutes + 30 seconds) asTruncatedHours
|
|
1278 |
2 minutes asTruncatedHours
|
|
1279 |
10 milliseconds asTruncatedHours
|
|
1280 |
"
|
|
1281 |
!
|
|
1282 |
|
22843
|
1283 |
asTruncatedMicroseconds
|
22860
|
1284 |
"answer the duration as microseconds (truncated).
|
|
1285 |
Values smaller than 1 us will be returned as 0.
|
|
1286 |
This is the total number of microseconds - not just the fractional part"
|
22843
|
1287 |
|
22860
|
1288 |
^ (timeEncoding * 1000) + ((additionalPicoseconds ? 0) // (1000 * 1000))
|
22843
|
1289 |
|
|
1290 |
"
|
22860
|
1291 |
100 nanoseconds asTruncatedMicroseconds
|
|
1292 |
100 nanoseconds asExactMicroseconds
|
|
1293 |
|
|
1294 |
10 milliseconds asTruncatedMicroseconds
|
|
1295 |
10 seconds asTruncatedMicroseconds
|
22843
|
1296 |
"
|
|
1297 |
!
|
|
1298 |
|
|
1299 |
asTruncatedMilliseconds
|
22860
|
1300 |
"answer the duration as milliseconds (truncated).
|
|
1301 |
Values smaller than 1 ms will be returned as 0.
|
|
1302 |
This is the total number of milliseconds - not just the fractional part"
|
22843
|
1303 |
|
22860
|
1304 |
^ timeEncoding
|
22843
|
1305 |
|
|
1306 |
"
|
22860
|
1307 |
0.1 milliseconds asTruncatedMilliseconds
|
|
1308 |
0.1 milliseconds asExactMilliseconds
|
|
1309 |
|
22843
|
1310 |
10 milliseconds asMilliseconds
|
|
1311 |
10 seconds asMilliseconds
|
|
1312 |
"
|
|
1313 |
!
|
|
1314 |
|
22862
|
1315 |
asTruncatedMinutes
|
|
1316 |
"answer the duration as minutes (truncated).
|
|
1317 |
Values smaller than 1 m will be returned as 0.
|
|
1318 |
|
|
1319 |
To get the exact number, use asExactMinutes."
|
|
1320 |
|
|
1321 |
^ self asTruncatedSeconds // 60
|
|
1322 |
|
|
1323 |
"
|
|
1324 |
(2 hours + 10 minutes) asTruncatedMinutes
|
|
1325 |
(2 hours + 10 minutes + 30 seconds) asTruncatedMinutes
|
|
1326 |
2 minutes asTruncatedMinutes
|
|
1327 |
10 milliseconds asTruncatedMinutes
|
|
1328 |
"
|
|
1329 |
!
|
|
1330 |
|
22843
|
1331 |
asTruncatedNanoseconds
|
22860
|
1332 |
"answer the duration as nanoseconds (truncated).
|
|
1333 |
Values smaller than 1 ns will be returned as 0.
|
|
1334 |
This is the total number of nanoseconds - not just the fractional part"
|
22843
|
1335 |
|
22860
|
1336 |
^ (timeEncoding * 1000000) + ((additionalPicoseconds ? 0) // (1000))
|
22843
|
1337 |
|
|
1338 |
"
|
22860
|
1339 |
10 picoseconds asTruncatedNanoseconds
|
|
1340 |
10 picoseconds asExactNanoseconds
|
|
1341 |
|
|
1342 |
10 milliseconds asTruncatedNanoseconds
|
|
1343 |
10 seconds asTruncatedNanoseconds
|
22843
|
1344 |
"
|
|
1345 |
!
|
|
1346 |
|
|
1347 |
asTruncatedSeconds
|
|
1348 |
"answer the duration as seconds (truncated).
|
22860
|
1349 |
Values smaller than 1 s will be returned as 0.
|
|
1350 |
This is the total number of seconds - not just the fractional part.
|
22843
|
1351 |
To get the exact number, use asExactSeconds."
|
|
1352 |
|
22860
|
1353 |
^ timeEncoding // 1000
|
22843
|
1354 |
|
|
1355 |
"
|
|
1356 |
10 milliseconds asTruncatedSeconds
|
|
1357 |
10 milliseconds asExactSeconds
|
|
1358 |
2 minutes asTruncatedSeconds
|
|
1359 |
"
|
22860
|
1360 |
!
|
|
1361 |
|
|
1362 |
secondsAsFixedPoint
|
|
1363 |
"answer the duration in seconds as a fixedPoint number."
|
|
1364 |
|
|
1365 |
^ self secondsAsFixedPoint:4
|
|
1366 |
|
|
1367 |
"
|
|
1368 |
(10 milliseconds) secondsAsFixedPoint
|
|
1369 |
(10 milliseconds) secondsAsFixedPoint asFixedPoint:3
|
|
1370 |
"
|
|
1371 |
|
|
1372 |
"Modified (comment): / 14-09-2017 / 15:15:24 / stefan"
|
|
1373 |
!
|
|
1374 |
|
|
1375 |
secondsAsFixedPoint:scale
|
|
1376 |
"answer the duration in seconds as a fixedPoint number with given scale."
|
|
1377 |
|
|
1378 |
|t|
|
|
1379 |
|
|
1380 |
t := FixedPoint numerator:timeEncoding denominator:1000 scale:scale.
|
|
1381 |
additionalPicoseconds notNil ifTrue:[
|
|
1382 |
t := t + (FixedPoint numerator:additionalPicoseconds denominator:((1000*1000)*(1000*1000)) scale:scale)
|
|
1383 |
].
|
|
1384 |
^ t
|
|
1385 |
|
|
1386 |
"
|
|
1387 |
(1000 milliseconds) secondsAsFixedPoint
|
|
1388 |
(10 milliseconds) secondsAsFixedPoint
|
|
1389 |
(10 microseconds) secondsAsFixedPoint scale:8
|
|
1390 |
(10 nanoseconds) secondsAsFixedPoint scale:8
|
|
1391 |
(1000001 microseconds) secondsAsFixedPoint scale:8
|
|
1392 |
"
|
|
1393 |
|
|
1394 |
"Modified (comment): / 14-09-2017 / 15:15:24 / stefan"
|
|
1395 |
!
|
|
1396 |
|
|
1397 |
secondsAsFloat
|
|
1398 |
"answer the duration in seconds as a float."
|
|
1399 |
|
|
1400 |
|t|
|
|
1401 |
|
|
1402 |
t := timeEncoding / 1000.0.
|
|
1403 |
additionalPicoseconds notNil ifTrue:[
|
|
1404 |
t := t + (additionalPicoseconds / ((1000.0*1000.0) * (1000.0*1000.0))).
|
|
1405 |
].
|
|
1406 |
^ t
|
|
1407 |
|
|
1408 |
"
|
|
1409 |
(1000 milliseconds) secondsAsFloat
|
|
1410 |
(10 milliseconds) secondsAsFloat
|
|
1411 |
(10 microseconds) secondsAsFloat
|
|
1412 |
(10 nanoseconds) secondsAsFloat
|
|
1413 |
(1000001 microseconds) secondsAsFloat
|
|
1414 |
"
|
|
1415 |
|
|
1416 |
"Modified (comment): / 14-09-2017 / 15:15:18 / stefan"
|
|
1417 |
!
|
|
1418 |
|
|
1419 |
secondsAsFraction
|
|
1420 |
"answer the duration in seconds as a fraction
|
|
1421 |
(might return an integer, if the duration is an exact multiple
|
|
1422 |
of millis)."
|
|
1423 |
|
|
1424 |
|t|
|
|
1425 |
|
|
1426 |
t := timeEncoding / 1000.
|
|
1427 |
additionalPicoseconds notNil ifTrue:[
|
|
1428 |
t := t + (additionalPicoseconds / ((1000*1000)*(1000*1000))).
|
|
1429 |
].
|
|
1430 |
^ t
|
|
1431 |
|
|
1432 |
"
|
|
1433 |
(1000 milliseconds) secondsAsFraction
|
|
1434 |
(10 milliseconds) secondsAsFraction
|
|
1435 |
(10 microseconds) secondsAsFraction
|
|
1436 |
(10 nanoseconds) secondsAsFraction
|
|
1437 |
(1000001 microseconds) secondsAsFraction asFloat
|
|
1438 |
"
|
|
1439 |
|
|
1440 |
"Modified (comment): / 19-01-2018 / 17:29:24 / stefan"
|
|
1441 |
!
|
|
1442 |
|
|
1443 |
secondsAsLongFloat
|
|
1444 |
"answer the duration as longfloat seconds."
|
|
1445 |
|
|
1446 |
|t|
|
|
1447 |
|
|
1448 |
t := timeEncoding / 1000 asLongFloat.
|
|
1449 |
additionalPicoseconds notNil ifTrue:[
|
|
1450 |
t := t + (additionalPicoseconds / (((1000*1000) asLongFloat) * (1000*1000) asLongFloat)).
|
|
1451 |
].
|
|
1452 |
^ t
|
|
1453 |
|
|
1454 |
"
|
|
1455 |
(10 milliseconds) secondsAsLongFloat
|
|
1456 |
(10 microseconds) secondsAsLongFloat
|
|
1457 |
(10 nanoseconds) secondsAsLongFloat
|
|
1458 |
(1000001 microseconds) secondsAsLongFloat
|
|
1459 |
"
|
16356
|
1460 |
! !
|
|
1461 |
|
23233
|
1462 |
!TimeDuration methodsFor:'double dispatching'!
|
|
1463 |
|
|
1464 |
differenceFromInteger:anInteger
|
|
1465 |
"treat the integer as a number of seconds.
|
|
1466 |
Might be questionable, but adding integers to timeDurations is also allowed,
|
|
1467 |
and addition is kommutative...
|
|
1468 |
...maybe mixing should be forbidden."
|
|
1469 |
|
|
1470 |
^ (self species seconds:anInteger) - self
|
|
1471 |
|
|
1472 |
"
|
|
1473 |
10 seconds - 5 seconds
|
|
1474 |
10 - 5 seconds
|
|
1475 |
10 seconds - 5
|
|
1476 |
|
|
1477 |
10 seconds * 2
|
|
1478 |
2 * 10 seconds
|
|
1479 |
"
|
|
1480 |
|
|
1481 |
"Created: / 16-09-2017 / 12:49:33 / cg"
|
|
1482 |
!
|
|
1483 |
|
|
1484 |
differenceFromTimeDuration:aTimeDuration
|
|
1485 |
"return a new timeDuration"
|
|
1486 |
|
|
1487 |
|newMillis newPicos|
|
|
1488 |
|
23238
|
1489 |
newMillis := aTimeDuration getMilliseconds - timeEncoding.
|
23233
|
1490 |
newPicos := (aTimeDuration additionalPicoseconds) - (additionalPicoseconds ? 0).
|
|
1491 |
|
|
1492 |
^ aTimeDuration species basicNew
|
|
1493 |
setMilliseconds:newMillis additionalPicoseconds:newPicos
|
|
1494 |
|
|
1495 |
"
|
|
1496 |
(TimeDuration fromString:'1m') - (TimeDuration fromString:'10s')
|
|
1497 |
1 minutes - 10 seconds
|
|
1498 |
10 - 1 minutes
|
|
1499 |
"
|
|
1500 |
|
|
1501 |
"Created: / 25-07-2018 / 21:17:50 / Stefan Vogel"
|
23238
|
1502 |
"Modified: / 27-07-2018 / 10:32:53 / Stefan Vogel"
|
23233
|
1503 |
!
|
|
1504 |
|
|
1505 |
differenceFromTimestamp:aTimestamp
|
|
1506 |
"return the timestamp this timeDuration before aTimestamp"
|
|
1507 |
|
23238
|
1508 |
|newMillis newPicos|
|
|
1509 |
|
|
1510 |
newMillis := aTimestamp getMilliseconds - timeEncoding.
|
|
1511 |
newPicos := (aTimestamp additionalPicoseconds) - (additionalPicoseconds ? 0).
|
|
1512 |
|
|
1513 |
^ aTimestamp species basicNew
|
|
1514 |
setMilliseconds:newMillis additionalPicoseconds:newPicos.
|
23233
|
1515 |
|
|
1516 |
"
|
|
1517 |
Timestamp now - 100 seconds
|
23238
|
1518 |
Time now - 100 seconds
|
23233
|
1519 |
Timestamp now - 100
|
|
1520 |
"
|
|
1521 |
|
|
1522 |
"Modified (comment): / 16-09-2017 / 12:51:38 / cg"
|
23238
|
1523 |
"Modified (format): / 27-07-2018 / 10:50:13 / Stefan Vogel"
|
23233
|
1524 |
!
|
|
1525 |
|
|
1526 |
productFromFloat:aFloat
|
|
1527 |
"sent when aFloat does not know how to multiply the receiver.
|
|
1528 |
Return a new timeDuration"
|
|
1529 |
|
|
1530 |
^ self productFromNumber:aFloat
|
|
1531 |
|
|
1532 |
"
|
|
1533 |
5.1 * (TimeDuration fromString:'10s')
|
|
1534 |
"
|
|
1535 |
|
|
1536 |
"Modified (comment): / 12-06-2017 / 20:51:38 / cg"
|
|
1537 |
!
|
|
1538 |
|
|
1539 |
productFromFraction:aFraction
|
|
1540 |
"sent when aFraction does not know how to multiply the receiver.
|
|
1541 |
Return a new timeDuration"
|
|
1542 |
|
|
1543 |
^ self productFromNumber:aFraction
|
|
1544 |
|
|
1545 |
"
|
|
1546 |
(TimeDuration fromString:'10s') * 5
|
|
1547 |
"
|
|
1548 |
|
|
1549 |
"Modified (comment): / 12-06-2017 / 20:51:25 / cg"
|
|
1550 |
!
|
|
1551 |
|
|
1552 |
productFromInteger:anInteger
|
|
1553 |
"sent when an integer does not know how to multiply the receiver"
|
|
1554 |
|
|
1555 |
^ self productFromNumber:anInteger
|
|
1556 |
|
|
1557 |
"
|
|
1558 |
5 * (TimeDuration fromString:'10s')
|
|
1559 |
"
|
|
1560 |
|
|
1561 |
"Modified (comment): / 12-06-2017 / 20:50:56 / cg"
|
|
1562 |
!
|
|
1563 |
|
|
1564 |
productFromNumber:aNumber
|
|
1565 |
"sent when an integer does not know how to multiply the receiver.
|
|
1566 |
Return a new timeDuration"
|
|
1567 |
|
|
1568 |
^ self species basicNew
|
23238
|
1569 |
setMilliseconds:(timeEncoding * aNumber) additionalPicoseconds:(additionalPicoseconds ? 0) * aNumber.
|
23233
|
1570 |
|
|
1571 |
"
|
|
1572 |
5.1 * (TimeDuration fromString:'10s')
|
|
1573 |
(TimeDuration fromString:'10s') * 5
|
|
1574 |
"
|
|
1575 |
|
|
1576 |
"Modified (comment): / 16-09-2017 / 12:52:16 / cg"
|
23238
|
1577 |
"Modified: / 27-07-2018 / 10:33:21 / Stefan Vogel"
|
23233
|
1578 |
!
|
|
1579 |
|
|
1580 |
productFromTimeDuration:aTimeDuration
|
|
1581 |
"return a new timeDuration"
|
|
1582 |
|
|
1583 |
|newMillis newPicos|
|
|
1584 |
|
23238
|
1585 |
newMillis := timeEncoding * aTimeDuration getMilliseconds.
|
23233
|
1586 |
newPicos := (additionalPicoseconds ? 0) * (aTimeDuration additionalPicoseconds).
|
|
1587 |
|
|
1588 |
^ aTimeDuration species basicNew
|
|
1589 |
setMilliseconds:newMillis additionalPicoseconds:newPicos
|
|
1590 |
|
|
1591 |
"
|
|
1592 |
(TimeDuration fromString:'1m') * (TimeDuration fromString:'10s')
|
|
1593 |
1 minutes * 10 seconds
|
|
1594 |
10 * 1 minutes
|
|
1595 |
"
|
|
1596 |
|
|
1597 |
"Created: / 25-07-2018 / 21:11:40 / Stefan Vogel"
|
23238
|
1598 |
"Modified: / 27-07-2018 / 10:33:29 / Stefan Vogel"
|
23233
|
1599 |
!
|
|
1600 |
|
|
1601 |
sumFromInteger:anInteger
|
|
1602 |
"treat the integer as a number of seconds.
|
|
1603 |
Might be questionable, but adding integers to timeDurations is also allowed,
|
|
1604 |
and addition is kommutative...
|
|
1605 |
...maybe mixing should be forbidden."
|
|
1606 |
|
|
1607 |
^ self addSeconds:anInteger
|
|
1608 |
|
|
1609 |
"
|
|
1610 |
10 + 5 seconds
|
|
1611 |
10 seconds + 5
|
|
1612 |
"
|
|
1613 |
|
|
1614 |
"Created: / 16-09-2017 / 12:46:20 / cg"
|
|
1615 |
!
|
|
1616 |
|
|
1617 |
sumFromTimeDuration:aTimeDuration
|
|
1618 |
"return a new timeDuration"
|
|
1619 |
|
|
1620 |
|newMillis newPicos|
|
|
1621 |
|
23238
|
1622 |
newMillis := timeEncoding + aTimeDuration getMilliseconds.
|
23233
|
1623 |
newPicos := (additionalPicoseconds ? 0) + (aTimeDuration additionalPicoseconds).
|
|
1624 |
|
|
1625 |
^ aTimeDuration species basicNew
|
|
1626 |
setMilliseconds:newMillis additionalPicoseconds:newPicos
|
|
1627 |
|
|
1628 |
"
|
|
1629 |
(TimeDuration fromString:'1m') + (TimeDuration fromString:'10s')
|
|
1630 |
1 minutes + 10 seconds
|
|
1631 |
10 + 1 minutes
|
|
1632 |
"
|
|
1633 |
|
|
1634 |
"Created: / 16-09-2017 / 12:43:28 / cg"
|
23238
|
1635 |
"Modified: / 27-07-2018 / 10:33:47 / Stefan Vogel"
|
23233
|
1636 |
!
|
|
1637 |
|
|
1638 |
sumFromTimestamp:aTimestamp
|
|
1639 |
"return the timestamp this timeDuration after aTimestamp"
|
|
1640 |
|
23238
|
1641 |
|newMillis newPicos|
|
|
1642 |
|
|
1643 |
newMillis := timeEncoding + aTimestamp getMilliseconds.
|
|
1644 |
newPicos := (additionalPicoseconds ? 0) + (aTimestamp additionalPicoseconds).
|
|
1645 |
|
|
1646 |
^ aTimestamp species basicNew
|
|
1647 |
setMilliseconds:newMillis additionalPicoseconds:newPicos
|
23233
|
1648 |
|
|
1649 |
"
|
|
1650 |
Timestamp now + 100 seconds
|
|
1651 |
"
|
|
1652 |
|
|
1653 |
"Modified (comment): / 16-09-2017 / 12:53:14 / cg"
|
23238
|
1654 |
"Modified: / 27-07-2018 / 10:30:13 / Stefan Vogel"
|
23233
|
1655 |
! !
|
16356
|
1656 |
|
24159
|
1657 |
|
24365
|
1658 |
|
16356
|
1659 |
!TimeDuration methodsFor:'printing'!
|
|
1660 |
|
|
1661 |
addPrintBindingsTo:aDictionary language:languageOrNil
|
|
1662 |
"private print support: add bindings for printing to aDictionary.
|
|
1663 |
languageOrNil can only be #en or nil for the current language.
|
|
1664 |
|
|
1665 |
Additional formats available here (for timeDuration) are:
|
|
1666 |
%(Hd) hours in day (i.e. 0..23)
|
|
1667 |
%(hd) hours in day padded to 2 chars (i.e. 00..23)
|
|
1668 |
|
19930
|
1669 |
%(yrR) years rounded (i.e. for 730 days, we get 2 asFixedPoint:1 )
|
|
1670 |
%(monR) month rounded (i.e. for 45 days, we get 1.5 asFixedPoint:1 )
|
|
1671 |
%(w) weeks
|
|
1672 |
%(wR) weeks rounded (i.e. for 45 days, we get 6.xxx asFixedPoint:1 )
|
|
1673 |
%(dR) days rounded (i.e. for 36 hours, we get 1.5 asFixedPoint:1 )
|
22843
|
1674 |
%(dw) days in week (rest days after taking out the weeks)
|
19930
|
1675 |
%(hR) hours rounded (i.e. for 3h 30m, we get 3.5 asFixedPoint:1 )
|
|
1676 |
%(mR) minutes rounded (i.e. for 2m 30s, we get 2.5 asFixedPoint:1 )
|
|
1677 |
%(sR) seconds rounded to 1 postDecimal (i.e. for 2s 100ms, we get 2.1 asFixedPoint:1 )
|
16356
|
1678 |
"
|
|
1679 |
|
19930
|
1680 |
|hoursInDay s yearsRounded monthsRounded weeksRounded daysRounded hoursRounded minutesRounded secondsRounded|
|
16356
|
1681 |
|
16830
|
1682 |
self addBasicPrintBindingsTo:aDictionary language:languageOrNil.
|
16356
|
1683 |
aDictionary at:$d put:self days.
|
|
1684 |
|
|
1685 |
hoursInDay := self hours \\ 24.
|
|
1686 |
aDictionary at:#Hd put:(s := hoursInDay printString).
|
|
1687 |
aDictionary at:#hd put:(s leftPaddedTo:2 with:$0).
|
|
1688 |
|
19930
|
1689 |
aDictionary at:#dw put:(self days \\ 7).
|
|
1690 |
|
16356
|
1691 |
yearsRounded := (self hours / 24 / 365).
|
|
1692 |
yearsRounded isInteger ifFalse:[
|
|
1693 |
yearsRounded := yearsRounded asFixedPoint:1.
|
|
1694 |
].
|
|
1695 |
aDictionary at:#yrR put:yearsRounded.
|
|
1696 |
|
19930
|
1697 |
monthsRounded := self days / 30.
|
16356
|
1698 |
monthsRounded isInteger ifFalse:[
|
|
1699 |
monthsRounded := monthsRounded asFixedPoint:1.
|
|
1700 |
monthsRounded roundedToScale = monthsRounded asInteger ifTrue:[
|
|
1701 |
monthsRounded := monthsRounded truncated
|
|
1702 |
].
|
|
1703 |
].
|
|
1704 |
aDictionary at:#monR put:monthsRounded.
|
|
1705 |
|
19930
|
1706 |
aDictionary at:#w put:(self days // 7).
|
|
1707 |
weeksRounded := self days / 7.
|
|
1708 |
weeksRounded isInteger ifFalse:[
|
|
1709 |
weeksRounded := weeksRounded asFixedPoint:1.
|
|
1710 |
weeksRounded roundedToScale = weeksRounded asInteger ifTrue:[
|
|
1711 |
weeksRounded := weeksRounded truncated
|
|
1712 |
].
|
|
1713 |
].
|
|
1714 |
aDictionary at:#wR put:weeksRounded.
|
|
1715 |
|
16356
|
1716 |
daysRounded := (self hours / 24).
|
|
1717 |
daysRounded isInteger ifFalse:[
|
|
1718 |
daysRounded := daysRounded asFixedPoint:1.
|
|
1719 |
daysRounded roundedToScale = daysRounded asInteger ifTrue:[
|
|
1720 |
daysRounded := daysRounded truncated
|
|
1721 |
].
|
|
1722 |
].
|
|
1723 |
aDictionary at:#dR put:daysRounded.
|
|
1724 |
|
|
1725 |
hoursRounded := self hours + (self minutes / 60).
|
|
1726 |
hoursRounded isInteger ifFalse:[
|
|
1727 |
hoursRounded := hoursRounded asFixedPoint:1.
|
|
1728 |
hoursRounded roundedToScale = hoursRounded asInteger ifTrue:[
|
|
1729 |
hoursRounded := hoursRounded truncated
|
|
1730 |
].
|
|
1731 |
].
|
|
1732 |
aDictionary at:#hR put:hoursRounded.
|
|
1733 |
|
|
1734 |
minutesRounded := self minutes + (self seconds / 60).
|
|
1735 |
minutesRounded isInteger ifFalse:[
|
|
1736 |
minutesRounded := minutesRounded asFixedPoint:1.
|
|
1737 |
minutesRounded roundedToScale = minutesRounded asInteger ifTrue:[
|
|
1738 |
minutesRounded := minutesRounded truncated
|
|
1739 |
].
|
|
1740 |
].
|
|
1741 |
aDictionary at:#mR put:minutesRounded.
|
|
1742 |
|
|
1743 |
secondsRounded := self seconds + (self milliseconds / 1000).
|
|
1744 |
secondsRounded isInteger ifFalse:[
|
|
1745 |
secondsRounded := secondsRounded asFixedPoint:1.
|
|
1746 |
secondsRounded roundedToScale = secondsRounded asInteger ifTrue:[
|
|
1747 |
secondsRounded := secondsRounded truncated
|
|
1748 |
].
|
|
1749 |
].
|
|
1750 |
aDictionary at:#sR put:secondsRounded.
|
|
1751 |
|
|
1752 |
"Modified: / 22-02-2011 / 15:55:38 / cg"
|
|
1753 |
!
|
|
1754 |
|
|
1755 |
formatForApproximatePrinting
|
|
1756 |
"Return a format which is suitable for a human - not meant to be read back.
|
|
1757 |
In contrast to the regular format, this one only gives a rounded approximation
|
|
1758 |
of the time duration as useful in information-dialogs or other user-hint-GUI elements.
|
|
1759 |
For example, in a timeDuration of more than 2hours, the user might not be interested in
|
|
1760 |
individual seconds or even milliseconds.
|
|
1761 |
The way this hidding/rounding is done is pure magic heuristics."
|
|
1762 |
|
22930
|
1763 |
|hours mins secs millis|
|
16356
|
1764 |
|
|
1765 |
hours := self hours.
|
|
1766 |
|
22286
|
1767 |
hours >= (24*365*2) ifTrue:[
|
16356
|
1768 |
^ '%(yrR)yr'.
|
|
1769 |
].
|
|
1770 |
hours >= (24*40) ifTrue:[
|
|
1771 |
^ '%(monR)mon'.
|
|
1772 |
].
|
|
1773 |
hours >= 48 ifTrue:[
|
|
1774 |
^ '%(dR)d'.
|
|
1775 |
].
|
|
1776 |
hours >= 24 ifTrue:[
|
|
1777 |
^ '%dd %(Hd)h'.
|
|
1778 |
].
|
|
1779 |
hours >= 2 ifTrue:[
|
|
1780 |
^ '%(hR)h'.
|
|
1781 |
].
|
|
1782 |
hours >= 1 ifTrue:[
|
|
1783 |
^ '%Hh %Mm'.
|
|
1784 |
].
|
|
1785 |
|
|
1786 |
"/ no hours
|
|
1787 |
mins := self minutes.
|
|
1788 |
mins >= 5 ifTrue:[
|
|
1789 |
^ '%(mR)m'.
|
|
1790 |
].
|
|
1791 |
mins >= 1 ifTrue:[
|
|
1792 |
^ '%Mm %Ss'.
|
|
1793 |
].
|
|
1794 |
|
|
1795 |
"/ no mins
|
|
1796 |
secs := self seconds.
|
|
1797 |
secs >= 1 ifTrue:[
|
|
1798 |
secs >= 30 ifTrue:[
|
|
1799 |
^ '%(S)s'.
|
|
1800 |
].
|
|
1801 |
^ '%(sR)s'.
|
|
1802 |
].
|
|
1803 |
|
|
1804 |
"/ no secs
|
22930
|
1805 |
millis := self milliseconds.
|
|
1806 |
millis > 500 ifTrue:[
|
16356
|
1807 |
^ '%(sR)s'
|
|
1808 |
].
|
|
1809 |
|
|
1810 |
^ self formatForPrinting
|
|
1811 |
|
|
1812 |
"
|
|
1813 |
(TimeDuration hours:0 minutes:0 seconds:0 millis:12) printStringForApproximation
|
|
1814 |
|
|
1815 |
(TimeDuration hours:0 minutes:0 seconds:10 millis:123) printStringForApproximation
|
|
1816 |
(TimeDuration hours:0 minutes:1 seconds:10 millis:123) printStringForApproximation
|
|
1817 |
(TimeDuration hours:0 minutes:2 seconds:10 millis:123) printStringForApproximation
|
|
1818 |
(TimeDuration hours:0 minutes:33 seconds:0 millis:123) printStringForApproximation
|
|
1819 |
(TimeDuration hours:2 minutes:0 seconds:0 millis:123) printStringForApproximation
|
|
1820 |
(TimeDuration hours:2 minutes:33 seconds:0 millis:123) printStringForApproximation
|
|
1821 |
(TimeDuration hours:100 minutes:33 seconds:0 millis:123) printStringForApproximation
|
|
1822 |
(TimeDuration hours:10000 minutes:33 seconds:0 millis:123) printStringForApproximation
|
|
1823 |
(TimeDuration hours:1000000 minutes:33 seconds:0 millis:123) printStringForApproximation
|
|
1824 |
|
|
1825 |
(TimeDuration hours:2 minutes:33 seconds:0 millis:0) printStringForApproximation
|
|
1826 |
(TimeDuration hours:2 minutes:0 seconds:0 millis:0) printStringForApproximation
|
|
1827 |
(TimeDuration hours:24 minutes:0 seconds:0 millis:0) printStringForApproximation
|
22930
|
1828 |
|
|
1829 |
(TimeDuration fromMicroseconds:20) printStringForApproximation
|
16356
|
1830 |
"
|
22286
|
1831 |
|
|
1832 |
"Modified: / 21-09-2017 / 22:19:53 / cg"
|
16356
|
1833 |
!
|
|
1834 |
|
|
1835 |
formatForPrinting
|
|
1836 |
"Return the format for printing"
|
|
1837 |
|
|
1838 |
DefaultFormatForPrinting notNil ifTrue:[
|
|
1839 |
^ DefaultFormatForPrinting
|
|
1840 |
].
|
|
1841 |
^ self formatForPrinting:false
|
|
1842 |
|
|
1843 |
"
|
|
1844 |
(TimeDuration readFrom:'10h 3s') formatForPrinting
|
|
1845 |
(TimeDuration readFrom:'3s') formatForPrinting
|
|
1846 |
(TimeDuration readFrom:'1d 2ms') formatForPrinting
|
19930
|
1847 |
(TimeDuration readFrom:'1 week') formatForPrinting
|
16356
|
1848 |
"
|
|
1849 |
!
|
|
1850 |
|
|
1851 |
formatForShortPrinting
|
|
1852 |
"Return the short format for printing (without ms)"
|
|
1853 |
|
|
1854 |
^ self formatForPrinting:true
|
|
1855 |
!
|
|
1856 |
|
|
1857 |
printAsApproximationOn:aStream
|
|
1858 |
"append a human readable printed representation of the receiver to aStream.
|
|
1859 |
The format is meant for a human and does not give all information;
|
|
1860 |
especially, useless detail is hidden.
|
|
1861 |
This means, that seconds are rounded or hidden, if the dT is more than a few minutes;
|
21446
|
1862 |
minutes rounded into the hour or hidden, if it's more than a few hours etc.
|
16356
|
1863 |
The way this is done is pure magic heuristics - let me know, if you have a better algorithm."
|
|
1864 |
|
|
1865 |
^ self
|
|
1866 |
printOn:aStream
|
|
1867 |
format:(self formatForApproximatePrinting).
|
|
1868 |
|
|
1869 |
"
|
|
1870 |
(TimeDuration hours:0 minutes:0 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
1871 |
(TimeDuration hours:0 minutes:0 seconds:10 milliseconds:123) printAsApproximationOn:Transcript
|
|
1872 |
(TimeDuration hours:0 minutes:33 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
1873 |
(TimeDuration hours:2 minutes:0 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
1874 |
(TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
1875 |
(TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
1876 |
(TimeDuration hours:10000 minutes:33 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
1877 |
(TimeDuration hours:1000000 minutes:33 seconds:0 milliseconds:123) printAsApproximationOn:Transcript
|
|
1878 |
|
|
1879 |
(TimeDuration hours:2 minutes:33 seconds:0) printAsApproximationOn:Transcript
|
|
1880 |
(TimeDuration hours:2 minutes:0 seconds:0) printAsApproximationOn:Transcript
|
|
1881 |
(TimeDuration hours:24 minutes:0 seconds:0) printAsApproximationOn:Transcript
|
|
1882 |
"
|
|
1883 |
|
|
1884 |
"Modified: / 18-07-2007 / 14:06:17 / cg"
|
21446
|
1885 |
"Modified (comment): / 13-02-2017 / 20:32:54 / cg"
|
16356
|
1886 |
!
|
|
1887 |
|
|
1888 |
printOn:aStream
|
|
1889 |
"append a human readable printed representation of the receiver to aStream.
|
|
1890 |
The format is suitable for a human - not meant to be read back."
|
|
1891 |
|
|
1892 |
timeEncoding negative ifTrue:[
|
20704
|
1893 |
aStream nextPutAll:'-('.
|
16356
|
1894 |
(self class basicNew timeEncoding:timeEncoding negated) printOn:aStream.
|
20704
|
1895 |
aStream nextPutAll:')'.
|
16356
|
1896 |
^ self
|
|
1897 |
].
|
|
1898 |
^ self
|
|
1899 |
printOn:aStream
|
|
1900 |
format:(self formatForPrinting).
|
|
1901 |
|
|
1902 |
"
|
|
1903 |
TimeDuration hours:0 minutes:0 seconds:0 millis:12
|
|
1904 |
|
|
1905 |
TimeDuration hours:0 minutes:0 seconds:0 millis:123
|
|
1906 |
TimeDuration hours:0 minutes:0 seconds:10 millis:123
|
|
1907 |
TimeDuration hours:0 minutes:33 seconds:0 millis:123
|
|
1908 |
TimeDuration hours:2 minutes:0 seconds:0 millis:123
|
|
1909 |
TimeDuration hours:2 minutes:33 seconds:0 millis:123
|
|
1910 |
TimeDuration hours:100 minutes:33 seconds:0 millis:123
|
|
1911 |
TimeDuration hours:10000 minutes:33 seconds:0 millis:123
|
|
1912 |
TimeDuration hours:1000000 minutes:33 seconds:0 millis:123
|
|
1913 |
|
|
1914 |
TimeDuration hours:2 minutes:33 seconds:0 millis:0
|
|
1915 |
TimeDuration hours:2 minutes:0 seconds:0 millis:0
|
|
1916 |
TimeDuration hours:24 minutes:0 seconds:0 millis:0
|
|
1917 |
|
|
1918 |
(TimeDuration hours:0 minutes:0 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1919 |
(TimeDuration hours:0 minutes:0 seconds:10 millis:123) printStringFormat:'%h:%m:%s'
|
|
1920 |
(TimeDuration hours:0 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1921 |
(TimeDuration hours:2 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1922 |
(TimeDuration hours:100 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1923 |
(TimeDuration hours:10000 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1924 |
(TimeDuration hours:1000000 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1925 |
"
|
|
1926 |
|
|
1927 |
"Modified: / 18-07-2007 / 14:06:17 / cg"
|
|
1928 |
!
|
|
1929 |
|
|
1930 |
printShortOn:aStream
|
|
1931 |
"append a human readable printed representation
|
|
1932 |
of the receiver to aStream (without milliseconds)"
|
|
1933 |
|
|
1934 |
^ self
|
|
1935 |
printOn:aStream
|
|
1936 |
format:(self formatForShortPrinting).
|
|
1937 |
|
|
1938 |
"
|
|
1939 |
TimeDuration hours:0 minutes:0 seconds:0 millis:12
|
|
1940 |
|
|
1941 |
TimeDuration hours:0 minutes:0 seconds:0 millis:123
|
|
1942 |
TimeDuration hours:0 minutes:0 seconds:10 millis:123
|
|
1943 |
TimeDuration hours:0 minutes:33 seconds:0 millis:123
|
|
1944 |
TimeDuration hours:2 minutes:0 seconds:0 millis:123
|
|
1945 |
TimeDuration hours:2 minutes:33 seconds:0 millis:123
|
|
1946 |
TimeDuration hours:100 minutes:33 seconds:0 millis:123
|
|
1947 |
TimeDuration hours:10000 minutes:33 seconds:0 millis:123
|
|
1948 |
TimeDuration hours:1000000 minutes:33 seconds:0 millis:123
|
|
1949 |
|
|
1950 |
TimeDuration hours:2 minutes:33 seconds:0 millis:0
|
|
1951 |
TimeDuration hours:2 minutes:0 seconds:0 millis:0
|
|
1952 |
TimeDuration hours:24 minutes:0 seconds:0 millis:0
|
|
1953 |
|
|
1954 |
(TimeDuration hours:0 minutes:0 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1955 |
(TimeDuration hours:0 minutes:0 seconds:10 millis:123) printStringFormat:'%h:%m:%s'
|
|
1956 |
(TimeDuration hours:0 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1957 |
(TimeDuration hours:2 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1958 |
(TimeDuration hours:100 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1959 |
(TimeDuration hours:10000 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1960 |
(TimeDuration hours:1000000 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s'
|
|
1961 |
"
|
|
1962 |
|
|
1963 |
"Modified: / 18-07-2007 / 14:06:17 / cg"
|
|
1964 |
!
|
|
1965 |
|
|
1966 |
printStringForApproximation
|
|
1967 |
"return a human readable printed representation of the receiver to aStream.
|
|
1968 |
The format is meant for a human and does not give all information;
|
|
1969 |
especially, useless detail is hidden.
|
|
1970 |
This means, that seconds are rounded or hidden, if the dT is more than a few minutes;
|
21446
|
1971 |
minutes rounded into the hour or hidden, if it's more than a few hours etc.
|
16356
|
1972 |
The way this is done is pure magic heuristics - let me know, if you have a better algorithm."
|
|
1973 |
|
20837
|
1974 |
^ self printStringFormat:(self formatForApproximatePrinting).
|
16356
|
1975 |
|
|
1976 |
"
|
|
1977 |
(TimeDuration hours:0 minutes:0 seconds:0 milliseconds:123) printStringForApproximation
|
|
1978 |
(TimeDuration hours:0 minutes:0 seconds:10 milliseconds:123) printStringForApproximation
|
|
1979 |
(TimeDuration hours:0 minutes:33 seconds:0 milliseconds:123) printStringForApproximation
|
|
1980 |
(TimeDuration hours:2 minutes:0 seconds:0 milliseconds:123) printStringForApproximation
|
|
1981 |
(TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123) printStringForApproximation
|
|
1982 |
(TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123) printStringForApproximation
|
|
1983 |
(TimeDuration hours:10000 minutes:33 seconds:0 milliseconds:123) printStringForApproximation
|
|
1984 |
(TimeDuration hours:1000000 minutes:33 seconds:0 milliseconds:123) printStringForApproximation
|
|
1985 |
|
|
1986 |
(TimeDuration hours:2 minutes:33 seconds:0) printStringForApproximation
|
|
1987 |
(TimeDuration hours:2 minutes:0 seconds:0) printStringForApproximation
|
|
1988 |
(TimeDuration hours:24 minutes:0 seconds:0) printStringForApproximation
|
|
1989 |
"
|
21446
|
1990 |
|
|
1991 |
"Modified (comment): / 13-02-2017 / 20:32:59 / cg"
|
16356
|
1992 |
! !
|
|
1993 |
|
|
1994 |
!TimeDuration methodsFor:'private'!
|
|
1995 |
|
22843
|
1996 |
additionalPicoseconds
|
|
1997 |
"get the optional additional picoseconds (0..999999999)
|
|
1998 |
notice: that is NOT the total number of picoseconds,
|
|
1999 |
but the fractional part only.
|
|
2000 |
Use this only for printing."
|
|
2001 |
|
|
2002 |
^ additionalPicoseconds ? 0
|
|
2003 |
!
|
|
2004 |
|
|
2005 |
additionalPicoseconds:picosecondPart
|
|
2006 |
"set the optional additional picoseconds (0..999999999)"
|
16356
|
2007 |
|
22843
|
2008 |
self assert:(picosecondPart isInteger).
|
|
2009 |
self assert:(picosecondPart < (1000*1000*1000)).
|
|
2010 |
additionalPicoseconds := picosecondPart
|
|
2011 |
!
|
16356
|
2012 |
|
22843
|
2013 |
formatForPrinting:shortFlag
|
|
2014 |
"Return a format which is suitable for a human (i.e. not ISO8601)
|
|
2015 |
(not meant to be read back because it will not print tiny fractions,
|
|
2016 |
but instead round it heuristically.
|
|
2017 |
However, the reader can read that format, but you'll loose some precision if you do).
|
|
2018 |
If shortFlag is true, some millisecond-info is omitted for longer times.
|
|
2019 |
For timeDurations to be read back exactly, use iso8601 format."
|
|
2020 |
|
|
2021 |
|fmt days weeks hours mins secs
|
|
2022 |
overAllSeconds overAllMicros overAllNanos millis|
|
|
2023 |
|
|
2024 |
days := self days. "/ that's the total
|
19930
|
2025 |
weeks := days // 7.
|
16356
|
2026 |
hours := self hours.
|
|
2027 |
mins := self minutes.
|
|
2028 |
secs := self seconds.
|
|
2029 |
millis := self milliseconds.
|
|
2030 |
|
22843
|
2031 |
"/ q: for up to 2 weeks, whould we better generate: "10d" instead of "1w 3d" ??
|
19930
|
2032 |
weeks > 0 ifTrue:[
|
22843
|
2033 |
"/ notice: dw here, which prints the remaining days, after taking out the weeks
|
19930
|
2034 |
fmt := '%(w)w %(dw)d %(Hd)h %Mm'.
|
|
2035 |
secs = 0 ifTrue:[
|
|
2036 |
fmt := '%(w)w %(dw)d %(Hd)h %Mm'.
|
|
2037 |
mins = 0 ifTrue:[
|
|
2038 |
fmt := '%(w)w %(dw)d %(Hd)h'.
|
|
2039 |
(hours \\ 24) = 0 ifTrue:[
|
|
2040 |
fmt := '%(w)w %(dw)d'.
|
|
2041 |
(days \\ 7) = 0 ifTrue:[
|
|
2042 |
fmt := '%(w)w'.
|
|
2043 |
].
|
|
2044 |
].
|
|
2045 |
].
|
|
2046 |
].
|
|
2047 |
] ifFalse:[days > 0 ifTrue:[
|
22843
|
2048 |
"/ notice: d here, which prints the total number of days
|
16356
|
2049 |
fmt := '%dd %(Hd)h %Mm'.
|
|
2050 |
secs = 0 ifTrue:[
|
|
2051 |
fmt := '%dd %(Hd)h %Mm'.
|
|
2052 |
mins = 0 ifTrue:[
|
|
2053 |
fmt := '%dd %(Hd)h'.
|
|
2054 |
(hours \\ 24) = 0 ifTrue:[
|
|
2055 |
fmt := '%dd'.
|
|
2056 |
].
|
|
2057 |
].
|
|
2058 |
].
|
|
2059 |
] ifFalse:[
|
|
2060 |
hours > 0 ifTrue:[
|
|
2061 |
fmt := '%Hh %Mm'.
|
|
2062 |
secs = 0 ifTrue:[
|
|
2063 |
fmt := '%Hh %Mm'.
|
|
2064 |
mins = 0 ifTrue:[
|
|
2065 |
fmt := '%Hh'.
|
|
2066 |
].
|
|
2067 |
].
|
|
2068 |
] ifFalse:[
|
|
2069 |
mins > 0 ifTrue:[
|
|
2070 |
fmt := '%Mm'.
|
|
2071 |
secs = 0 ifTrue:[
|
|
2072 |
fmt := '%Mm'
|
|
2073 |
].
|
|
2074 |
] ifFalse:[
|
|
2075 |
fmt := ''
|
|
2076 |
].
|
|
2077 |
].
|
19930
|
2078 |
]].
|
22843
|
2079 |
((secs ~= 0) or:[millis ~= 0 or:[(additionalPicoseconds?0) ~= 0]]) ifTrue:[
|
16356
|
2080 |
fmt size ~~ 0 ifTrue:[
|
|
2081 |
fmt := fmt , ' '
|
|
2082 |
].
|
22843
|
2083 |
((millis = 0) and:[(additionalPicoseconds?0) = 0]) ifTrue:[
|
16356
|
2084 |
fmt := fmt , '%Ss'
|
|
2085 |
] ifFalse:[
|
22843
|
2086 |
(secs = 0 and:[(additionalPicoseconds?0) = 0]) ifTrue:[
|
16356
|
2087 |
fmt := fmt , '%Ims'
|
|
2088 |
] ifFalse:[
|
|
2089 |
shortFlag ifFalse:[
|
|
2090 |
"/ show millis
|
22843
|
2091 |
(millis ~= 0) ifTrue:[
|
|
2092 |
fmt := fmt , '%S.%is'
|
|
2093 |
] ifFalse:[
|
22857
|
2094 |
overAllMicros := self microseconds.
|
22843
|
2095 |
overAllMicros > 2 ifTrue:[
|
22997
|
2096 |
fmt := fmt , '%(micro)µs'.
|
22843
|
2097 |
] ifFalse:[
|
22857
|
2098 |
overAllNanos := self nanoseconds.
|
22843
|
2099 |
overAllNanos > 2 ifTrue:[
|
|
2100 |
fmt := fmt , '%(nano)ns'.
|
|
2101 |
] ifFalse:[
|
|
2102 |
fmt := fmt , '%(pico)ps'.
|
|
2103 |
].
|
|
2104 |
].
|
|
2105 |
].
|
16356
|
2106 |
] ifTrue:[
|
|
2107 |
"/ only show millis if the number of seconds is small
|
|
2108 |
overAllSeconds := self asSeconds.
|
|
2109 |
overAllSeconds > 2 ifTrue:[
|
|
2110 |
overAllSeconds > 10 ifTrue:[
|
|
2111 |
overAllSeconds > 300 ifTrue:[
|
22519
|
2112 |
"/ no decimal above 300 seconds
|
16356
|
2113 |
fmt := fmt , '%Ss'
|
|
2114 |
] ifFalse:[
|
22519
|
2115 |
"/ 1 decimals up to 300 seconds
|
16356
|
2116 |
fmt := fmt , '%S.%(milli1)s'
|
|
2117 |
]
|
|
2118 |
] ifFalse:[
|
22519
|
2119 |
"/ 2 decimals up to 10seconds
|
16356
|
2120 |
fmt := fmt , '%S.%(milli2)s'
|
|
2121 |
]
|
|
2122 |
] ifFalse:[
|
22519
|
2123 |
"/ millis up to 2seconds
|
22843
|
2124 |
fmt := fmt , '%S.%is'.
|
16356
|
2125 |
]
|
|
2126 |
]
|
|
2127 |
]
|
|
2128 |
].
|
|
2129 |
] ifFalse:[
|
|
2130 |
fmt isEmpty ifTrue:[
|
|
2131 |
fmt := '%Ss'
|
|
2132 |
].
|
|
2133 |
].
|
|
2134 |
|
|
2135 |
^ fmt.
|
|
2136 |
|
|
2137 |
"
|
22817
|
2138 |
3001 seconds formatForPrinting:false
|
|
2139 |
3001 seconds formatForPrinting:true
|
|
2140 |
|
22843
|
2141 |
(TimeDuration fromString:'1w 3d') formatForPrinting
|
|
2142 |
(TimeDuration fromString:'1w 3d') printString
|
|
2143 |
(TimeDuration fromString:'7d') printString
|
|
2144 |
(TimeDuration fromString:'6d') printString
|
|
2145 |
|
|
2146 |
(TimeDuration fromMilliseconds:0.5) printString
|
|
2147 |
(TimeDuration fromMilliseconds:0.05) printString
|
|
2148 |
(TimeDuration fromMilliseconds:0.005) printString
|
|
2149 |
(TimeDuration fromMilliseconds:0.0005) printString
|
|
2150 |
(TimeDuration fromMilliseconds:0.00005) printString
|
|
2151 |
(TimeDuration fromMilliseconds:0.000005) printString
|
|
2152 |
(TimeDuration fromMilliseconds:0.0000005) printString
|
|
2153 |
(TimeDuration fromMilliseconds:0.00000005) printString
|
|
2154 |
(TimeDuration fromMilliseconds:0.000000005) printString
|
|
2155 |
|
21757
|
2156 |
(TimeDuration hours:0 minutes:0 seconds:0 milliseconds:12) formatForPrinting
|
22519
|
2157 |
(TimeDuration hours:0 minutes:0 seconds:2 milliseconds:12) formatForPrinting
|
|
2158 |
(TimeDuration hours:0 minutes:0 seconds:8 milliseconds:12) formatForPrinting
|
|
2159 |
(TimeDuration hours:0 minutes:0 seconds:10 milliseconds:12) formatForPrinting
|
16356
|
2160 |
|
21757
|
2161 |
(TimeDuration hours:0 minutes:0 seconds:0 milliseconds:123) formatForPrinting
|
|
2162 |
(TimeDuration hours:0 minutes:0 seconds:10 milliseconds:123) formatForPrinting
|
|
2163 |
(TimeDuration hours:0 minutes:33 seconds:0 milliseconds:123) formatForPrinting
|
|
2164 |
(TimeDuration hours:2 minutes:0 seconds:0 milliseconds:123) formatForPrinting
|
|
2165 |
(TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123) formatForPrinting
|
|
2166 |
(TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123) formatForPrinting
|
|
2167 |
(TimeDuration hours:10000 minutes:33 seconds:0 milliseconds:123) formatForPrinting
|
|
2168 |
(TimeDuration hours:1000000 minutes:33 seconds:0 milliseconds:123) formatForPrinting
|
16356
|
2169 |
|
21757
|
2170 |
(TimeDuration hours:0 minutes:38 seconds:22 milliseconds:123) formatForPrinting:true
|
16356
|
2171 |
|
21757
|
2172 |
(TimeDuration hours:2 minutes:33 seconds:0 milliseconds:0) formatForPrinting
|
|
2173 |
(TimeDuration hours:2 minutes:0 seconds:0 milliseconds:0) formatForPrinting
|
|
2174 |
(TimeDuration hours:24 minutes:0 seconds:0 milliseconds:0) formatForPrinting
|
16356
|
2175 |
|
21757
|
2176 |
(TimeDuration hours:0 minutes:0 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
|
|
2177 |
(TimeDuration hours:0 minutes:0 seconds:10 milliseconds:123) printStringFormat:'%h:%m:%s'
|
|
2178 |
(TimeDuration hours:0 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
|
|
2179 |
(TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
|
|
2180 |
(TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
|
|
2181 |
(TimeDuration hours:10000 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
|
|
2182 |
(TimeDuration hours:1000000 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
|
16356
|
2183 |
"
|
|
2184 |
|
|
2185 |
"Modified: / 18-07-2007 / 14:06:17 / cg"
|
21757
|
2186 |
"Modified (comment): / 17-05-2017 / 16:35:56 / mawalch"
|
22997
|
2187 |
"Modified: / 23-05-2018 / 10:36:21 / Claus Gittinger"
|
16356
|
2188 |
!
|
|
2189 |
|
22843
|
2190 |
getHours
|
|
2191 |
"return the number of hours (truncated).
|
|
2192 |
This is the total number of hours - not just the fractional part"
|
|
2193 |
|
|
2194 |
^ self getSeconds // 60 // 60
|
|
2195 |
!
|
|
2196 |
|
|
2197 |
getMicroseconds
|
|
2198 |
"return the number of microseconds (truncated).
|
|
2199 |
This is the total number of microseconds - not just the fractional part"
|
|
2200 |
|
|
2201 |
^ (timeEncoding * 1000) + ((additionalPicoseconds ? 0) // (1000 * 1000))
|
|
2202 |
!
|
|
2203 |
|
16356
|
2204 |
getMilliseconds
|
22843
|
2205 |
"return the number of milliseconds (truncated).
|
22625
|
2206 |
This is the total number of milliseconds - not just the fractional part"
|
16356
|
2207 |
|
|
2208 |
^ timeEncoding
|
|
2209 |
|
|
2210 |
"Modified: / 18-07-2007 / 13:44:33 / cg"
|
|
2211 |
!
|
|
2212 |
|
22843
|
2213 |
getMinutes
|
|
2214 |
"return the number of minutes (truncated).
|
|
2215 |
This is the total number of minutes - not just the fractional part"
|
|
2216 |
|
|
2217 |
^ self getSeconds // 60
|
|
2218 |
!
|
|
2219 |
|
|
2220 |
getPicoseconds
|
|
2221 |
"return the number of picoseconds (truncated).
|
|
2222 |
This is the total number of picoseconds - not just the fractional part"
|
|
2223 |
|
|
2224 |
^ (timeEncoding * 1000000000) + ((additionalPicoseconds ? 0))
|
|
2225 |
!
|
|
2226 |
|
16356
|
2227 |
getSeconds
|
22625
|
2228 |
"return the number of seconds (truncated).
|
|
2229 |
This is the total number of seconds - not just the fractional part"
|
16356
|
2230 |
|
|
2231 |
^ timeEncoding // 1000
|
|
2232 |
|
|
2233 |
"Modified: / 18-07-2007 / 13:44:37 / cg"
|
22281
|
2234 |
"Modified (comment): / 21-09-2017 / 18:50:03 / cg"
|
16356
|
2235 |
!
|
|
2236 |
|
20704
|
2237 |
possiblyNegatedValueFromTimeEncodingInto:aBlock
|
|
2238 |
timeEncoding < 0 ifTrue:[
|
|
2239 |
^ (aBlock value:(timeEncoding negated)) negated
|
|
2240 |
].
|
|
2241 |
^ aBlock value:timeEncoding
|
|
2242 |
!
|
|
2243 |
|
16356
|
2244 |
setHours:h minutes:m seconds:s millis:millis
|
|
2245 |
<resource: #obsolete>
|
|
2246 |
"set my time given individual values"
|
|
2247 |
|
|
2248 |
self obsoleteMethodWarning:'use setHours:minutes:seconds:milliseconds:'.
|
|
2249 |
self setHours:h minutes:m seconds:s milliseconds:millis.
|
|
2250 |
!
|
|
2251 |
|
|
2252 |
setHours:h minutes:m seconds:s milliseconds:millis
|
|
2253 |
"set my time given individual values"
|
|
2254 |
|
|
2255 |
self setMilliseconds:((h * 60 * 60 ) + (m * 60) + s) * 1000 + millis.
|
|
2256 |
!
|
|
2257 |
|
22843
|
2258 |
setMicroseconds:micros
|
|
2259 |
"set my duration given microseconds."
|
|
2260 |
|
|
2261 |
|restMicros|
|
|
2262 |
|
23238
|
2263 |
timeEncoding := micros // 1000.
|
22843
|
2264 |
micros isInteger ifTrue:[
|
22990
|
2265 |
additionalPicoseconds := (micros \\ 1000) * 1000000
|
22843
|
2266 |
] ifFalse:[
|
|
2267 |
restMicros := micros - (timeEncoding * 1000).
|
22990
|
2268 |
additionalPicoseconds := (restMicros * 1000000) truncated.
|
22843
|
2269 |
].
|
|
2270 |
|
|
2271 |
"
|
|
2272 |
self new setMicroseconds:100
|
|
2273 |
self new setMicroseconds:2
|
|
2274 |
self new setMicroseconds:1.5
|
|
2275 |
self new setMicroseconds:0.1
|
|
2276 |
"
|
|
2277 |
|
|
2278 |
"Modified: / 18-07-2007 / 13:44:16 / cg"
|
23238
|
2279 |
"Modified: / 27-07-2018 / 11:53:05 / Stefan Vogel"
|
22843
|
2280 |
!
|
|
2281 |
|
16356
|
2282 |
setMilliseconds:millis
|
|
2283 |
"set my duration given milliseconds.
|
|
2284 |
Duration can be longer than a day"
|
|
2285 |
|
22843
|
2286 |
millis isInteger ifTrue:[
|
|
2287 |
timeEncoding := millis.
|
|
2288 |
] ifFalse:[
|
22866
|
2289 |
timeEncoding := millis // 1.
|
22990
|
2290 |
additionalPicoseconds := ((millis \\ 1) * 1000000000) truncated.
|
22843
|
2291 |
]
|
|
2292 |
|
|
2293 |
"Modified: / 18-07-2007 / 13:44:16 / cg"
|
22990
|
2294 |
"Modified: / 22-05-2018 / 16:51:30 / Stefan Vogel"
|
22843
|
2295 |
!
|
|
2296 |
|
22863
|
2297 |
setMilliseconds:millis additionalPicoseconds:picos
|
|
2298 |
"set my duration given milliseconds and addon picos.
|
|
2299 |
Duration can be longer than a day;
|
|
2300 |
values may be negative (eg. if resulting from a subtraction)"
|
|
2301 |
|
|
2302 |
|rest newMillis newPicos|
|
|
2303 |
|
|
2304 |
millis isInteger ifTrue:[
|
|
2305 |
newMillis := millis.
|
|
2306 |
newPicos := 0.
|
|
2307 |
] ifFalse:[
|
|
2308 |
newMillis := millis truncated.
|
|
2309 |
rest := millis - newMillis.
|
22990
|
2310 |
newPicos := (rest * 1000000000) truncated.
|
22863
|
2311 |
].
|
|
2312 |
|
22990
|
2313 |
picos ~= 0 ifTrue:[
|
|
2314 |
newPicos := (newPicos + picos) truncated.
|
|
2315 |
newMillis := newMillis + (newPicos // 1000000000).
|
|
2316 |
newPicos := newPicos \\ 1000000000.
|
22863
|
2317 |
].
|
|
2318 |
timeEncoding := newMillis.
|
|
2319 |
additionalPicoseconds := newPicos.
|
|
2320 |
|
|
2321 |
"Modified: / 18-07-2007 / 13:44:16 / cg"
|
22990
|
2322 |
"Modified: / 22-05-2018 / 16:55:53 / Stefan Vogel"
|
22863
|
2323 |
!
|
|
2324 |
|
22843
|
2325 |
setNanoseconds:nanos
|
|
2326 |
"set my duration given nanoseconds."
|
|
2327 |
|
22891
|
2328 |
|millis restNanos|
|
22843
|
2329 |
|
22891
|
2330 |
millis := nanos // (1000*1000).
|
23238
|
2331 |
timeEncoding := millis.
|
22843
|
2332 |
nanos isInteger ifTrue:[
|
22891
|
2333 |
restNanos := nanos \\ (1000*1000).
|
|
2334 |
additionalPicoseconds := restNanos * 1000
|
22843
|
2335 |
] ifFalse:[
|
22990
|
2336 |
restNanos := nanos - (millis * 1000000).
|
|
2337 |
additionalPicoseconds := (restNanos * 1000) truncated.
|
22843
|
2338 |
].
|
|
2339 |
|
|
2340 |
"
|
|
2341 |
self new setMicroseconds:4
|
|
2342 |
self new setNanoseconds:4
|
22891
|
2343 |
self new setNanoseconds:4000
|
|
2344 |
self new setNanoseconds:4000000
|
|
2345 |
self new setNanoseconds:40000000
|
22843
|
2346 |
self new setNanoseconds:0.1
|
|
2347 |
"
|
|
2348 |
|
|
2349 |
"Modified: / 18-07-2007 / 13:44:16 / cg"
|
23238
|
2350 |
"Modified: / 27-07-2018 / 11:53:41 / Stefan Vogel"
|
22843
|
2351 |
!
|
|
2352 |
|
|
2353 |
setPicoseconds:picos
|
|
2354 |
"set my duration given picoseconds."
|
|
2355 |
|
22990
|
2356 |
timeEncoding := picos // 1000000000.
|
|
2357 |
additionalPicoseconds := (picos \\ 1000000000) truncated.
|
22843
|
2358 |
|
|
2359 |
"
|
|
2360 |
self new setMicroseconds:4
|
|
2361 |
self new setNanoseconds:4
|
|
2362 |
self new setPicoseconds:4
|
|
2363 |
self new setPicoseconds:4.5
|
|
2364 |
|
|
2365 |
self assert: (self new setPicoseconds:4000) = (self new setNanoseconds:4) .
|
|
2366 |
self assert: (self new setPicoseconds:4000000) = (self new setNanoseconds:4000) .
|
|
2367 |
self assert: (self new setPicoseconds:4000000) = (self new setMicroseconds:4) .
|
|
2368 |
self assert: (self new setPicoseconds:4000000000) = (self new setNanoseconds:4000000) .
|
|
2369 |
self assert: (self new setPicoseconds:4000000000) = (self new setMicroseconds:4000) .
|
|
2370 |
self assert: (self new setPicoseconds:4000000000) = (self new setMilliseconds:4) .
|
|
2371 |
self assert: (self new setPicoseconds:4000000000000) = (self new setMilliseconds:4000) .
|
|
2372 |
self assert: (self new setPicoseconds:4000000000000) = (self new setMicroseconds:4000000) .
|
|
2373 |
self assert: (self new setPicoseconds:4000000000000) = (self new setNanoseconds:4000000000) .
|
|
2374 |
self assert: (self new setPicoseconds:4000000000000) = (self new setSeconds:4) .
|
|
2375 |
"
|
16356
|
2376 |
|
|
2377 |
"Modified: / 18-07-2007 / 13:44:16 / cg"
|
22990
|
2378 |
"Modified: / 22-05-2018 / 16:51:45 / Stefan Vogel"
|
16356
|
2379 |
!
|
|
2380 |
|
|
2381 |
setSeconds:secs
|
|
2382 |
"set my timeduration given seconds.
|
|
2383 |
Notice that (in contrast to Time), there is no modulu operation here.
|
22843
|
2384 |
Duration can be longer than a day, and (much) smaller than a second"
|
16356
|
2385 |
|
|
2386 |
self setMilliseconds:(secs * 1000).
|
|
2387 |
|
|
2388 |
"Modified: / 18-07-2007 / 13:44:24 / cg"
|
|
2389 |
! !
|
|
2390 |
|
|
2391 |
!TimeDuration methodsFor:'testing'!
|
|
2392 |
|
|
2393 |
isTimeDuration
|
|
2394 |
^ true
|
|
2395 |
!
|
|
2396 |
|
|
2397 |
isZero
|
|
2398 |
^ self = self class zero
|
|
2399 |
!
|
|
2400 |
|
|
2401 |
negative
|
|
2402 |
^ timeEncoding < 0
|
|
2403 |
! !
|
|
2404 |
|
|
2405 |
!TimeDuration class methodsFor:'documentation'!
|
|
2406 |
|
|
2407 |
version
|
18564
|
2408 |
^ '$Header$'
|
16356
|
2409 |
!
|
|
2410 |
|
|
2411 |
version_CVS
|
18564
|
2412 |
^ '$Header$'
|
16356
|
2413 |
! !
|
|
2414 |
|
|
2415 |
|
|
2416 |
TimeDuration initialize!
|