#BUGFIX by cg
authorClaus Gittinger <cg@exept.de>
Thu, 10 May 2018 01:52:41 +0200
changeset 22857 d3675e380987
parent 22856 e8a1e761cdcd
child 22858 7b475b237f26
#BUGFIX by cg class: TimeDuration added: #asPicoeconds comment/format in: #asFixedPoint: #asFloat #asLongFloat #asMicroseconds #asNanoseconds changed: #asFixedPoint #asFraction #formatForPrinting: (send #nanoseconds instead of #asNanoseconds, send #microseconds instead of #asMicroseconds) class: TimeDuration class changed: #readFrom:defaultUnit:onError:
TimeDuration.st
--- a/TimeDuration.st	Thu May 10 01:38:39 2018 +0200
+++ b/TimeDuration.st	Thu May 10 01:52:41 2018 +0200
@@ -251,7 +251,7 @@
 
     ^ [
         |seconds millis picos restMillis 
-         t1 str val fraction uIdx unit unitChar1 negative defaultUnitOrNil|
+         t1 str val fraction mantissa uIdx unit unitChar1 negative defaultUnitOrNil|
 
         defaultUnitOrNil := defaultUnitOrNilArg.
         str := aStringOrStream readStream.
@@ -295,15 +295,19 @@
                     val := Integer readFrom:str onError:nil.
                     val isNil ifTrue:[^ exceptionBlock value].
                     seconds := seconds + val.
-                    str peek == $. ifTrue:[
+                    (str peek == $. or:[str peek == $,]) ifTrue:[
                         "/ hour:minutes:seconds.millis format
                         str next.
                         "/ the old code here was wrong in assuming that exactly 3 digits
                         "/ are coming; thus hh:mm:ss.1 was interpreted as 1ms (instead of 100)
                         "/ thus: count the zeros...
                         str peek isDigit ifTrue:[
-                            fraction := Number readMantissaFrom:str radix:10.
-                            fraction isNil ifTrue:[^ exceptionBlock value].
+                            "/ fraction := Number readMantissaFrom:str radix:10.
+                            "/ fraction isNil ifTrue:[^ exceptionBlock value].
+                            "/ ignore the float value; take the fraction
+
+                            mantissa := Number readMantissaAndScaleFrom:str radix:10.
+                            fraction := (mantissa at:2) / (10 raisedTo:(mantissa at:3)).
                             millis := (fraction * 1000) rounded.
                         ] ifFalse:[
                             millis := 0
@@ -314,7 +318,10 @@
             ].
             ((str peek == $.) or:[(str peek == $,)]) ifTrue:[
                 str next.
-                fraction := Number readMantissaFrom:str radix:10.
+                "/ fraction := Number readMantissaFrom:str radix:10.
+                "/ ignore the float value; take the fraction
+                mantissa := Number readMantissaAndScaleFrom:str radix:10.
+                fraction := (mantissa at:2) / (10 raisedTo:(mantissa at:3)).
                 val := val + fraction.
             ].
             str skipSeparators.
@@ -882,7 +889,7 @@
     "answer the duration as seconds.
      Better use the explicit getSeconds"
 
-    ^ FixedPoint numerator:timeEncoding denominator:1000 scale:4 
+    ^ self asFixedPoint:4 
 
     "
      (10 milliseconds) asFixedPoint
@@ -896,11 +903,19 @@
     "answer the duration as seconds.
      Better use the explicit getSeconds"
 
-    ^ FixedPoint numerator:timeEncoding denominator:1000 scale:scale 
+    |t|
+
+    t := FixedPoint numerator:timeEncoding denominator:1000 scale:scale.
+    additionalPicoseconds notNil ifTrue:[
+        t := t + (FixedPoint numerator:additionalPicoseconds denominator:((1000*1000)*(1000*1000)) scale:scale)
+    ].
+    ^ t
 
     "
-     (10 milliseconds) asFixedPoint  
-     (10 milliseconds) asFixedPoint:3 
+     (10 milliseconds) asFixedPoint
+     (10 microseconds) asFixedPoint scale:8
+     (10 nanoseconds) asFixedPoint scale:8   
+     (1000001 microseconds) asFixedPoint scale:8
     "
 
     "Modified (comment): / 14-09-2017 / 15:15:24 / stefan"
@@ -910,10 +925,19 @@
     "answer the duration as seconds.
      Better use the explicit getSeconds"
 
-    ^ timeEncoding / 1000.0
+    |t|
+
+    t := timeEncoding / 1000.0.
+    additionalPicoseconds notNil ifTrue:[
+        t := t + (additionalPicoseconds / ((1000.0*1000.0) * (1000.0*1000.0))).
+    ].
+    ^ t
 
     "
      (10 milliseconds) asFloat
+     (10 microseconds) asFloat
+     (10 nanoseconds) asFloat
+     (1000001 microseconds) asFloat
     "
 
     "Modified (comment): / 14-09-2017 / 15:15:18 / stefan"
@@ -923,11 +947,19 @@
     "answer the duration as seconds.
      Better use the explicit getSeconds"
 
-    ^ timeEncoding / 1000
+    |t|
+
+    t := timeEncoding / 1000.
+    additionalPicoseconds notNil ifTrue:[
+        t := t + (additionalPicoseconds / ((1000*1000)*(1000*1000))).
+    ].
+    ^ t
 
     "
      (10 milliseconds) asFraction
-     (1 seconds) asFraction
+     (10 microseconds) asFraction
+     (10 nanoseconds) asFraction
+     (1000001 microseconds) asFraction asFloat
     "
 
     "Modified (comment): / 19-01-2018 / 17:29:24 / stefan"
@@ -951,13 +983,20 @@
     "answer the duration as longfloat seconds.
      Better use the explicit getSeconds"
 
-    ^ timeEncoding / 1000 asLongFloat
+    |t|
+
+    t := timeEncoding / 1000 asLongFloat.
+    additionalPicoseconds notNil ifTrue:[
+        t := t + (additionalPicoseconds / (((1000*1000) asLongFloat) * (1000*1000) asLongFloat)).
+    ].
+    ^ t
 
     "
-     (10 milliseconds)  asLongFloat
+     (10 milliseconds) asLongFloat
+     (10 microseconds) asLongFloat
+     (10 nanoseconds) asLongFloat
+     (1000001 microseconds) asLongFloat
     "
-
-    "Modified (comment): / 21-06-2017 / 13:59:54 / cg"
 !
 
 asMicroseconds
@@ -966,8 +1005,9 @@
     ^ self getMicroseconds
 
     "
-     10 milliseconds getMicroseconds
-     10 seconds getMicroseconds
+     10 milliseconds asMicroseconds
+     1.5 milliseconds asMicroseconds
+     10 seconds asMicroseconds
     "
 !
 
@@ -988,8 +1028,8 @@
     ^ self getNanoseconds
 
     "
-     10 milliseconds getMicroseconds
-     10 seconds getMicroseconds
+     10 milliseconds asNanoseconds
+     10 seconds asNanoseconds
     "
 !
 
@@ -1002,6 +1042,17 @@
     "Modified: / 21-09-2017 / 18:57:57 / cg"
 !
 
+asPicoeconds
+    "answer the duration as picoseconds (truncated)."
+
+    ^ self getPicoseconds
+
+    "
+     10 milliseconds asPicoeconds
+     10 seconds asPicoeconds
+    "
+!
+
 asSeconds
     "answer the duration as seconds (truncated, for now).
      To get the exact number, use asExactSeconds.
@@ -1532,11 +1583,11 @@
                     (millis ~= 0) ifTrue:[
                         fmt := fmt , '%S.%is'
                     ] ifFalse:[
-                        overAllMicros := self asMicroseconds.
+                        overAllMicros := self microseconds.
                         overAllMicros > 2 ifTrue:[
                             fmt := fmt , '%(micro)us'.
                         ] ifFalse:[
-                            overAllNanos := self asNanoseconds.
+                            overAllNanos := self nanoseconds.
                             overAllNanos > 2 ifTrue:[
                                 fmt := fmt , '%(nano)ns'.
                             ] ifFalse:[