TimeDuration.st
changeset 22863 ea25b6e2cdae
parent 22862 8139ede8f0cb
child 22864 ed881ba3a4d2
--- a/TimeDuration.st	Thu May 10 12:41:36 2018 +0200
+++ b/TimeDuration.st	Thu May 10 13:02:44 2018 +0200
@@ -705,6 +705,7 @@
         ^ self species basicNew 
             setMilliseconds:(self getMilliseconds * aNumber) asInteger
     ].
+
     "/ notice: although noone seems to implement it (currently),
     "/ there are additional packages which add support (i.e. goodies/physic),
     "/ so do not remove the call below.
@@ -726,14 +727,21 @@
 !
 
 + aTimeDurationOrNumberOfSeconds
-    "return a new timeDuration"
+    "return a new timeDuration.
+     The argument may be a timeDuration or
+     a number, which is interpreted as seconds."
+
+    |newMillis newPicos|
 
     aTimeDurationOrNumberOfSeconds isNumber ifTrue:[
-        ^ self species basicNew 
-            setMilliseconds:(self getMilliseconds + (aTimeDurationOrNumberOfSeconds * 1000) asInteger)
+        newMillis := (self getMilliseconds + (aTimeDurationOrNumberOfSeconds * 1000) asInteger).
+        newPicos := additionalPicoseconds ? 0.
+    ] ifFalse:[
+        newMillis := (self getMilliseconds + aTimeDurationOrNumberOfSeconds getMilliseconds).
+        newPicos := (additionalPicoseconds ? 0) + (aTimeDurationOrNumberOfSeconds additionalPicoseconds).
     ].
     ^ self species basicNew
-        setMilliseconds:(self getMilliseconds + aTimeDurationOrNumberOfSeconds getMilliseconds)
+        setMilliseconds:newMillis additionalPicoseconds:newPicos
 
     "
      (TimeDuration fromString:'1m') + (TimeDuration fromString:'10s') 
@@ -741,6 +749,29 @@
     "
 !
 
+- aTimeDurationOrNumberOfSeconds
+    "return a new timeDuration.
+     The argument may be a timeDuration or
+     a number, which is interpreted as seconds."
+
+    |newMillis newPicos|
+
+    aTimeDurationOrNumberOfSeconds isNumber ifTrue:[
+        newMillis := (self getMilliseconds - (aTimeDurationOrNumberOfSeconds * 1000) asInteger).
+        newPicos := additionalPicoseconds ? 0.
+    ] ifFalse:[
+        newMillis := (self getMilliseconds - aTimeDurationOrNumberOfSeconds getMilliseconds).
+        newPicos := (additionalPicoseconds ? 0) - (aTimeDurationOrNumberOfSeconds additionalPicoseconds).
+    ].
+    ^ self species basicNew
+        setMilliseconds:newMillis additionalPicoseconds:newPicos
+
+    "
+     (TimeDuration fromString:'1m') - (TimeDuration fromString:'10s') 
+     1 minutes - 10 seconds
+    "
+!
+
 / aTimeDurationOrNumberOfSeconds
     "if the argument is a number, return a new timeDuration.
      Otherwise, return the quotient as a number."
@@ -752,6 +783,10 @@
     aTimeDurationOrNumberOfSeconds isTimeDuration ifTrue:[
         ^ (self getMilliseconds / aTimeDurationOrNumberOfSeconds getMilliseconds)
     ].
+
+    "/ notice: although noone seems to implement it (currently),
+    "/ there are additional packages which add support (i.e. goodies/physic),
+    "/ so do not remove the call below.
     ^ aTimeDurationOrNumberOfSeconds quotientFromTimeDuration:self
     
     "
@@ -2001,6 +2036,35 @@
     "Modified: / 18-07-2007 / 13:44:16 / cg"
 !
 
+setMilliseconds:millis additionalPicoseconds:picos
+    "set my duration given milliseconds and addon picos.
+     Duration can be longer than a day; 
+     values may be negative (eg. if resulting from a subtraction)"
+
+    |rest newMillis newPicos|
+
+    millis isInteger ifTrue:[
+        newMillis := millis.
+        newPicos := 0.
+    ] ifFalse:[
+        newMillis := millis truncated.
+        rest := millis - newMillis.
+        newPicos := (rest * 1000 * 1000 * 1000) rounded asInteger.
+    ].
+
+    picos ~~ 0 ifTrue:[
+        newPicos := newPicos + picos.
+        newPicos >= (1000*1000*1000) ifTrue:[
+            newMillis := newMillis + (newPicos // (1000*1000*1000)).
+            newPicos := newPicos \\ (1000*1000*1000).
+        ].
+    ].
+    timeEncoding := newMillis.
+    additionalPicoseconds := newPicos.
+
+    "Modified: / 18-07-2007 / 13:44:16 / cg"
+!
+
 setNanoseconds:nanos
     "set my duration given nanoseconds."