TimeDuration.st
changeset 22860 5a2c1731a17b
parent 22858 7b475b237f26
child 22861 7764618c5649
--- a/TimeDuration.st	Thu May 10 02:01:15 2018 +0200
+++ b/TimeDuration.st	Thu May 10 11:49:56 2018 +0200
@@ -579,85 +579,6 @@
 
 !TimeDuration methodsFor:'accessing'!
 
-asExactMicroseconds
-    "return the exact number of mcroseconds.
-     In contrast to getMicroSeconds, which returns them truncated."
-
-    additionalPicoseconds isNil ifTrue:[
-        ^ (timeEncoding * 1000) "/ millis as micros
-    ].
-    ^ (timeEncoding * 1000)                    "/ millis as micros 
-    + (additionalPicoseconds / (1000 * 1000))  "/ picos as microseconds.
-
-    "
-     40 milliseconds asExactMicroseconds
-     40 microseconds asExactMicroseconds
-     40 nanoseconds asExactMicroseconds
-     40 picoseconds asExactMicroseconds
-    "
-
-    "Created: / 21-09-2017 / 18:52:26 / cg"
-!
-
-asExactMilliseconds
-    "return the exact number of milliseconds.
-     In contrast to getMilliSeconds, which returns them truncated."
-
-    additionalPicoseconds isNil ifTrue:[
-        ^ (timeEncoding) "/ millis
-    ].
-    ^ (timeEncoding)                         "/ millis 
-    + (additionalPicoseconds / (1000 * 1000 * 1000))  "/ picos as milliseconds.
-
-    "
-     40 milliseconds asExactMilliseconds
-     40 microseconds asExactMilliseconds
-     40 nanoseconds asExactMilliseconds
-     40 picoseconds asExactMilliseconds
-    "
-
-    "Created: / 21-09-2017 / 18:52:26 / cg"
-!
-
-asExactNanoseconds
-    "return the exact number of nanoseconds.
-     In contrast to getnanoSeconds, which returns them truncated."
-
-    additionalPicoseconds isNil ifTrue:[
-        ^ (timeEncoding * 1000 * 1000) "/ millis as nanos
-    ].
-    ^ (timeEncoding * 1000 * 1000)          "/ millis as nanos 
-    + (additionalPicoseconds / (1000))      "/ picos as nanoseconds.
-
-    "
-     40 milliseconds asExactNanoseconds
-     40 microseconds asExactNanoseconds
-     40 nanoseconds asExactNanoseconds
-     40 picoseconds asExactNanoseconds
-    "
-
-    "Created: / 21-09-2017 / 18:52:26 / cg"
-!
-
-asExactSeconds
-    "return the exact number of seconds.
-     In contrast to getSeconds, which returns them truncated."
-
-    additionalPicoseconds isNil ifTrue:[
-        ^ (timeEncoding / 1000) "/ millis as seconds
-    ].
-    ^ (timeEncoding / 1000)                         "/ millis as seconds
-    + additionalPicoseconds / (1000 * 1000 * 1000 * 1000)  "/ picos as seconds.
-
-    "
-     40 milliseconds getExactSeconds
-     40 microseconds getExactSeconds
-     40 nanoseconds getExactSeconds
-    "
-
-    "Created: / 21-09-2017 / 18:52:26 / cg"
-!
-
 days
     "get the (truncated) total number of days.
      Use this only for printing.
@@ -703,7 +624,7 @@
      notice: that is NOT the total number of millis,
      but the fractional part (within the second) only. 
      Use this only for printing.
-     asMilliseconds or getMilliseconds is probably what you want"
+     asMilliseconds is probably what you want"
 
     ^ self 
         possiblyNegatedValueFromTimeEncodingInto:[:t |
@@ -757,7 +678,7 @@
      notice: that is NOT the total number of seconds,
      but the fractional part only. 
      Use this only for printing.
-     asSeconds or getSeconds is probably what you want"
+     asSeconds is probably what you want"
 
     ^ self 
         possiblyNegatedValueFromTimeEncodingInto:[:t |
@@ -895,11 +816,96 @@
 
 !TimeDuration methodsFor:'converting'!
 
+asExactMicroseconds
+    "return the exact number of mcroseconds.
+     In contrast to asMicroSeconds, which returns them truncated,
+     this may return a non-integer value."
+
+    additionalPicoseconds isNil ifTrue:[
+        ^ (timeEncoding * 1000) "/ millis as micros
+    ].
+    ^ (timeEncoding * 1000)                    "/ millis as micros 
+    + (additionalPicoseconds / (1000 * 1000))  "/ picos as microseconds.
+
+    "
+     40 milliseconds asExactMicroseconds
+     40 microseconds asExactMicroseconds
+     40 nanoseconds asExactMicroseconds
+     40 picoseconds asExactMicroseconds
+    "
+
+    "Created: / 21-09-2017 / 18:52:26 / cg"
+!
+
+asExactMilliseconds
+    "return the exact number of milliseconds.
+     In contrast to asMilliSeconds, which returns them truncated,
+     this may return a non-integer value."
+
+    additionalPicoseconds isNil ifTrue:[
+        ^ (timeEncoding) "/ millis
+    ].
+    ^ (timeEncoding)                         "/ millis 
+    + (additionalPicoseconds / (1000 * 1000 * 1000))  "/ picos as milliseconds.
+
+    "
+     40 milliseconds asExactMilliseconds
+     40 microseconds asExactMilliseconds
+     40 nanoseconds asExactMilliseconds
+     40 picoseconds asExactMilliseconds
+    "
+
+    "Created: / 21-09-2017 / 18:52:26 / cg"
+!
+
+asExactNanoseconds
+    "return the exact number of nanoseconds.
+     In contrast to asNanoSeconds, which returns them truncated,
+     this may return a non-integer value."
+
+    additionalPicoseconds isNil ifTrue:[
+        ^ (timeEncoding * 1000 * 1000) "/ millis as nanos
+    ].
+    ^ (timeEncoding * 1000 * 1000)          "/ millis as nanos 
+    + (additionalPicoseconds / (1000))      "/ picos as nanoseconds.
+
+    "
+     40 milliseconds asExactNanoseconds
+     40 microseconds asExactNanoseconds
+     40 nanoseconds asExactNanoseconds
+     40 picoseconds asExactNanoseconds
+    "
+
+    "Created: / 21-09-2017 / 18:52:26 / cg"
+!
+
+asExactSeconds
+    "return the exact number of seconds.
+     In contrast to asSeconds, which returns them truncated,
+     this may return a non-integer value."
+
+    additionalPicoseconds isNil ifTrue:[
+        ^ (timeEncoding / 1000) "/ millis as seconds
+    ].
+    ^ (timeEncoding / 1000)                         "/ millis as seconds
+    + additionalPicoseconds / (1000 * 1000 * 1000 * 1000)  "/ picos as seconds.
+
+    "
+     40 milliseconds asExactSeconds
+     40 microseconds asExactSeconds
+     40 nanoseconds asExactSeconds
+    "
+
+    "Created: / 21-09-2017 / 18:52:26 / cg"
+!
+
 asFixedPoint
-    "answer the duration as seconds.
-     Better use the explicit getSeconds"
+    <resource: #obsolete>
+    "answer the duration in seconds as a fixedPoint number.
+     This method has a bad name (a historic leftover);
+     Please change any sender to use secondsAsFixedPoint"
 
-    ^ self asFixedPoint:4 
+    ^ self secondsAsFixedPoint 
 
     "
      (10 milliseconds) asFixedPoint
@@ -910,40 +916,34 @@
 !
 
 asFixedPoint:scale
-    "answer the duration as seconds.
-     Better use the explicit getSeconds"
-
-    |t|
+    <resource: #obsolete>
+    "answer the duration in seconds as a fixedPoint number with given scale.
+     This method has a bad name (a historic leftover);
+     Please change any sender to use secondsAsFixedPoint"
 
-    t := FixedPoint numerator:timeEncoding denominator:1000 scale:scale.
-    additionalPicoseconds notNil ifTrue:[
-        t := t + (FixedPoint numerator:additionalPicoseconds denominator:((1000*1000)*(1000*1000)) scale:scale)
-    ].
-    ^ t
+    ^ self secondsAsFixedPoint:scale
 
     "
-     (10 milliseconds) asFixedPoint
-     (10 microseconds) asFixedPoint scale:8
-     (10 nanoseconds) asFixedPoint scale:8   
-     (1000001 microseconds) asFixedPoint scale:8
+     (1000 milliseconds) secondsAsFixedPoint
+     (10 milliseconds) secondsAsFixedPoint
+     (10 microseconds) secondsAsFixedPoint scale:8
+     (10 nanoseconds) secondsAsFixedPoint scale:8   
+     (1000001 microseconds) secondsAsFixedPoint scale:8
     "
 
     "Modified (comment): / 14-09-2017 / 15:15:24 / stefan"
 !
 
 asFloat
-    "answer the duration as seconds.
-     Better use the explicit getSeconds"
-
-    |t|
+    <resource: #obsolete>
+    "answer the duration in seconds as a float.
+     This method has a bad name (a historic leftover);
+     Please change any sender to use secondsAsFloat"
 
-    t := timeEncoding / 1000.0.
-    additionalPicoseconds notNil ifTrue:[
-        t := t + (additionalPicoseconds / ((1000.0*1000.0) * (1000.0*1000.0))).
-    ].
-    ^ t
+    ^ self secondsAsFloat
 
     "
+     (1000 milliseconds) asFloat
      (10 milliseconds) asFloat
      (10 microseconds) asFloat
      (10 nanoseconds) asFloat
@@ -954,18 +954,17 @@
 !
 
 asFraction
-    "answer the duration as seconds.
-     Better use the explicit getSeconds"
-
-    |t|
+    <resource: #obsolete>
+    "answer the duration in seconds as a fraction
+     (might return an integer, if the duration is an exact multiple
+      of millis).
+     This method has a bad name (a historic leftover);
+     Please change any sender to use secondsAsFraction"
 
-    t := timeEncoding / 1000.
-    additionalPicoseconds notNil ifTrue:[
-        t := t + (additionalPicoseconds / ((1000*1000)*(1000*1000))).
-    ].
-    ^ t
+    ^ self secondsAsFraction
 
     "
+     (1000 milliseconds) asFraction
      (10 milliseconds) asFraction
      (10 microseconds) asFraction
      (10 nanoseconds) asFraction
@@ -976,10 +975,11 @@
 !
 
 asInteger
-    "answer the duration as (truncated) seconds.
-     Better use the explicit getSeconds"
+    <resource: #obsolete>
+    "answer the duration as (truncated) integer seconds.
+     Better use the explicit asTruncatedSeconds"
 
-    ^ self getSeconds
+    ^ self asTruncatedSeconds
 
     "
      10 milliseconds asInteger
@@ -990,16 +990,12 @@
 !
 
 asLongFloat
+    <resource: #obsolete>
     "answer the duration as longfloat seconds.
-     Better use the explicit getSeconds"
-
-    |t|
+     This method has a bad name (a historic leftover);
+     Please change any sender to use secondsAsFloat"
 
-    t := timeEncoding / 1000 asLongFloat.
-    additionalPicoseconds notNil ifTrue:[
-        t := t + (additionalPicoseconds / (((1000*1000) asLongFloat) * (1000*1000) asLongFloat)).
-    ].
-    ^ t
+    ^ self secondsAsLongFloat
 
     "
      (10 milliseconds) asLongFloat
@@ -1010,11 +1006,15 @@
 !
 
 asMicroseconds
-    "answer the duration as microseconds (truncated)."
+    "answer the duration as microseconds (truncated).
+     Values smaller than 1 us will be returned as 0"
 
-    ^ self getMicroseconds
+    ^ self asTruncatedMicroseconds
 
     "
+     100 nanoseconds asTruncatedMicroseconds
+     100 nanoseconds asExactMicroseconds
+
      10 milliseconds asMicroseconds
      1.5 milliseconds asMicroseconds
      10 seconds asMicroseconds
@@ -1022,29 +1022,39 @@
 !
 
 asMilliseconds
-    "answer the duration as milliseconds (truncated)."
+    "answer the duration as milliseconds (truncated).
+     Values smaller than 1 ms will be returned as 0"
 
-    ^ self getMilliseconds
+    ^ self asTruncatedMilliseconds
 
     "
+     10 microseconds asTruncatedMilliseconds
+     10 microseconds asExactMilliseconds
+     10 microseconds asMilliseconds
+
      10 milliseconds asMilliseconds
      10 seconds asMilliseconds
     "
 !
 
 asNanoseconds
-    "answer the duration as nanoseconds (truncated)."
+    "answer the duration as nanoseconds (truncated).
+     Values smaller than 1 ns will be returned as 0"
 
-    ^ self getNanoseconds
+    ^ self asTruncatedNanoseconds
 
     "
-     10 milliseconds asNanoseconds
-     10 seconds asNanoseconds
+     10 picoseconds asTruncatedNanoseconds
+     10 picoseconds asExactNanoseconds
     "
 !
 
 asNumber
-    "answer the duration as seconds"
+    <resource: #obsolete>
+    "answer the duration as seconds.
+     This method has a bad name (a historic leftover);
+     Please change any sender to use asTruncatedSeconds or
+     asExactSeconds, depending on what is wanted."
 
     ^ self asExactSeconds
 
@@ -1053,9 +1063,11 @@
 !
 
 asPicoseconds
-    "answer the duration as picoseconds (truncated)."
+    "answer the duration as picoseconds (truncated).
+     Because the smallest representable timeDuration is 1ps,
+     there is no distinction between truncated and exact picos."
 
-    ^ self getPicoseconds
+    ^ (timeEncoding * 1000000000) + ((additionalPicoseconds ? 0))
 
     "
      10 milliseconds asPicoeconds
@@ -1064,10 +1076,15 @@
 !
 
 asSeconds
-    "answer the duration as seconds (truncated, for now).
+    "answer the duration as seconds (truncated).
+     Values smaller than 1 s will be returned as 0.
+
      To get the exact number, use asExactSeconds.
-     Please change senders to use asTruncatedSeconds to make this truncation explicit;
-     for now, this returns the truncated value for backward compatibility"
+     Please change senders to use asTruncatedSeconds 
+     to make this truncation explicit (and obvious when reading code).
+     For compatibility (both backward and with other smalltalks),
+     asSeconds returns the TRUNCATED integer value
+     (many senders assume that an integer is returned)"
 
     ^ self asTruncatedSeconds
 
@@ -1087,49 +1104,166 @@
 !
 
 asTruncatedMicroseconds
-    "answer the duration as microseconds (truncated)."
+    "answer the duration as microseconds (truncated).
+     Values smaller than 1 us will be returned as 0.
+     This is the total number of microseconds - not just the fractional part"
 
-    ^ self getMicroseconds
+    ^ (timeEncoding * 1000) + ((additionalPicoseconds ? 0) // (1000 * 1000))
 
     "
-     10 milliseconds getMicroseconds
-     10 seconds getMicroseconds
+     100 nanoseconds asTruncatedMicroseconds
+     100 nanoseconds asExactMicroseconds
+
+     10 milliseconds asTruncatedMicroseconds
+     10 seconds asTruncatedMicroseconds
     "
 !
 
 asTruncatedMilliseconds
-    "answer the duration as milliseconds (truncated)."
+    "answer the duration as milliseconds (truncated).
+     Values smaller than 1 ms will be returned as 0.
+     This is the total number of milliseconds - not just the fractional part"
 
-    ^ self getMilliseconds
+    ^ timeEncoding
 
     "
+     0.1 milliseconds asTruncatedMilliseconds
+     0.1 milliseconds asExactMilliseconds
+
      10 milliseconds asMilliseconds
      10 seconds asMilliseconds
     "
 !
 
 asTruncatedNanoseconds
-    "answer the duration as nanoseconds (truncated)."
+    "answer the duration as nanoseconds (truncated).
+     Values smaller than 1 ns will be returned as 0.
+     This is the total number of nanoseconds - not just the fractional part"
 
-    ^ self getNanoseconds
+    ^ (timeEncoding * 1000000) + ((additionalPicoseconds ? 0) // (1000))
 
     "
-     10 milliseconds getMicroseconds
-     10 seconds getMicroseconds
+     10 picoseconds asTruncatedNanoseconds
+     10 picoseconds asExactNanoseconds
+
+     10 milliseconds asTruncatedNanoseconds
+     10 seconds asTruncatedNanoseconds
     "
 !
 
 asTruncatedSeconds
     "answer the duration as seconds (truncated).
+     Values smaller than 1 s will be returned as 0.
+     This is the total number of seconds - not just the fractional part.
      To get the exact number, use asExactSeconds."
 
-    ^ self getSeconds
+    ^ timeEncoding // 1000
 
     "
      10 milliseconds asTruncatedSeconds
      10 milliseconds asExactSeconds
      2 minutes asTruncatedSeconds
     "
+!
+
+secondsAsFixedPoint
+    "answer the duration in seconds as a fixedPoint number."
+
+    ^ self secondsAsFixedPoint:4 
+
+    "
+     (10 milliseconds) secondsAsFixedPoint
+     (10 milliseconds) secondsAsFixedPoint asFixedPoint:3 
+    "
+
+    "Modified (comment): / 14-09-2017 / 15:15:24 / stefan"
+!
+
+secondsAsFixedPoint:scale
+    "answer the duration in seconds as a fixedPoint number with given 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
+
+    "
+     (1000 milliseconds) secondsAsFixedPoint
+     (10 milliseconds) secondsAsFixedPoint
+     (10 microseconds) secondsAsFixedPoint scale:8
+     (10 nanoseconds) secondsAsFixedPoint scale:8   
+     (1000001 microseconds) secondsAsFixedPoint scale:8
+    "
+
+    "Modified (comment): / 14-09-2017 / 15:15:24 / stefan"
+!
+
+secondsAsFloat
+    "answer the duration in seconds as a float."
+
+    |t|
+
+    t := timeEncoding / 1000.0.
+    additionalPicoseconds notNil ifTrue:[
+        t := t + (additionalPicoseconds / ((1000.0*1000.0) * (1000.0*1000.0))).
+    ].
+    ^ t
+
+    "
+     (1000 milliseconds) secondsAsFloat
+     (10 milliseconds) secondsAsFloat
+     (10 microseconds) secondsAsFloat
+     (10 nanoseconds) secondsAsFloat
+     (1000001 microseconds) secondsAsFloat
+    "
+
+    "Modified (comment): / 14-09-2017 / 15:15:18 / stefan"
+!
+
+secondsAsFraction
+    "answer the duration in seconds as a fraction
+     (might return an integer, if the duration is an exact multiple
+      of millis)."
+
+    |t|
+
+    t := timeEncoding / 1000.
+    additionalPicoseconds notNil ifTrue:[
+        t := t + (additionalPicoseconds / ((1000*1000)*(1000*1000))).
+    ].
+    ^ t
+
+    "
+     (1000 milliseconds) secondsAsFraction
+     (10 milliseconds) secondsAsFraction
+     (10 microseconds) secondsAsFraction
+     (10 nanoseconds) secondsAsFraction
+     (1000001 microseconds) secondsAsFraction asFloat
+    "
+
+    "Modified (comment): / 19-01-2018 / 17:29:24 / stefan"
+!
+
+secondsAsLongFloat
+    "answer the duration as longfloat seconds."
+
+    |t|
+
+    t := timeEncoding / 1000 asLongFloat.
+    additionalPicoseconds notNil ifTrue:[
+        t := t + (additionalPicoseconds / (((1000*1000) asLongFloat) * (1000*1000) asLongFloat)).
+    ].
+    ^ t
+
+    "
+     (10 milliseconds) secondsAsLongFloat
+     (10 microseconds) secondsAsLongFloat
+     (10 nanoseconds) secondsAsLongFloat
+     (1000001 microseconds) secondsAsLongFloat
+    "
 ! !
 
 !TimeDuration methodsFor:'double dispatching'!
@@ -1718,13 +1852,6 @@
     ^ self getSeconds // 60
 !
 
-getNanoseconds
-    "return the number of nanoseconds (truncated).
-     This is the total number of nanoseconds - not just the fractional part"
-
-    ^ (timeEncoding * 1000000) + ((additionalPicoseconds ? 0) // (1000))
-!
-
 getPicoseconds
     "return the number of picoseconds (truncated).
      This is the total number of picoseconds - not just the fractional part"