TimeDuration.st
changeset 22990 4efa84e493a9
parent 22930 8a5a2399ea28
child 22997 1c2629bf2cac
equal deleted inserted replaced
22989:9bd6b9704250 22990:4efa84e493a9
   191     "
   191     "
   192 !
   192 !
   193 
   193 
   194 microseconds:microseconds
   194 microseconds:microseconds
   195     "return a new TimeDuration representing a duration of microseconds microseconds.
   195     "return a new TimeDuration representing a duration of microseconds microseconds.
   196      Currently we do not support this (we round to millis), but maybe later..."
   196      Now we support microseconds (even picoseconds) but we still round to milliseconds for backward
       
   197      compatibility with the historic interface."
   197 
   198 
   198     microseconds == 0 ifTrue:[^ TimeDurationZero].
   199     microseconds == 0 ifTrue:[^ TimeDurationZero].
   199     ^ self basicNew setMilliseconds:((microseconds / 1000) rounded).
   200     ^ self basicNew setMilliseconds:((microseconds / 1000) rounded).
   200 
   201 
   201     "
   202     "
   202      TimeDuration microseconds:2499 
   203      TimeDuration microseconds:2499 
   203      TimeDuration microseconds:2500 
   204      TimeDuration microseconds:2500 
   204      TimeDuration microseconds:12345678900 
   205      TimeDuration microseconds:12345678900 
   205     "
   206     "
       
   207 
       
   208     "Modified (comment): / 22-05-2018 / 16:54:17 / Stefan Vogel"
   206 !
   209 !
   207 
   210 
   208 milliseconds:m
   211 milliseconds:m
   209     "return a new TimeDuration representing a duration of m millis.
   212     "return a new TimeDuration representing a duration of m millis.
   210      See also Time now / Date today / Timestamp now."
   213      See also Time now / Date today / Timestamp now."
  2060 
  2063 
  2061     |restMicros|
  2064     |restMicros|
  2062 
  2065 
  2063     micros isInteger ifTrue:[
  2066     micros isInteger ifTrue:[
  2064         timeEncoding := micros // 1000.
  2067         timeEncoding := micros // 1000.
  2065         additionalPicoseconds := (micros \\ 1000) * 1000 * 1000
  2068         additionalPicoseconds := (micros \\ 1000) * 1000000
  2066     ] ifFalse:[
  2069     ] ifFalse:[
  2067         timeEncoding := micros // 1000.
  2070         timeEncoding := micros // 1000.
  2068         restMicros := micros - (timeEncoding * 1000).
  2071         restMicros := micros - (timeEncoding * 1000).
  2069         additionalPicoseconds := (restMicros * 1000 * 1000) rounded asInteger.
  2072         additionalPicoseconds := (restMicros * 1000000) truncated.
  2070     ].
  2073     ].
  2071 
  2074 
  2072     "
  2075     "
  2073      self new setMicroseconds:100
  2076      self new setMicroseconds:100
  2074      self new setMicroseconds:2
  2077      self new setMicroseconds:2
  2075      self new setMicroseconds:1.5
  2078      self new setMicroseconds:1.5
  2076      self new setMicroseconds:0.1
  2079      self new setMicroseconds:0.1
  2077     "
  2080     "
  2078 
  2081 
  2079     "Modified: / 18-07-2007 / 13:44:16 / cg"
  2082     "Modified: / 18-07-2007 / 13:44:16 / cg"
       
  2083     "Modified: / 22-05-2018 / 16:52:53 / Stefan Vogel"
  2080 !
  2084 !
  2081 
  2085 
  2082 setMilliseconds:millis
  2086 setMilliseconds:millis
  2083     "set my duration given milliseconds.
  2087     "set my duration given milliseconds.
  2084      Duration can be longer than a day"
  2088      Duration can be longer than a day"
  2085 
  2089 
  2086     millis isInteger ifTrue:[
  2090     millis isInteger ifTrue:[
  2087         timeEncoding := millis.
  2091         timeEncoding := millis.
  2088     ] ifFalse:[
  2092     ] ifFalse:[
  2089         timeEncoding := millis // 1.
  2093         timeEncoding := millis // 1.
  2090         additionalPicoseconds := ((millis \\ 1) * 1000 * 1000 * 1000) rounded asInteger.
  2094         additionalPicoseconds := ((millis \\ 1) * 1000000000) truncated.
  2091     ]
  2095     ]
  2092 
  2096 
  2093     "Modified: / 18-07-2007 / 13:44:16 / cg"
  2097     "Modified: / 18-07-2007 / 13:44:16 / cg"
       
  2098     "Modified: / 22-05-2018 / 16:51:30 / Stefan Vogel"
  2094 !
  2099 !
  2095 
  2100 
  2096 setMilliseconds:millis additionalPicoseconds:picos
  2101 setMilliseconds:millis additionalPicoseconds:picos
  2097     "set my duration given milliseconds and addon picos.
  2102     "set my duration given milliseconds and addon picos.
  2098      Duration can be longer than a day; 
  2103      Duration can be longer than a day; 
  2104         newMillis := millis.
  2109         newMillis := millis.
  2105         newPicos := 0.
  2110         newPicos := 0.
  2106     ] ifFalse:[
  2111     ] ifFalse:[
  2107         newMillis := millis truncated.
  2112         newMillis := millis truncated.
  2108         rest := millis - newMillis.
  2113         rest := millis - newMillis.
  2109         newPicos := (rest * 1000 * 1000 * 1000) rounded asInteger.
  2114         newPicos := (rest * 1000000000) truncated.
  2110     ].
  2115     ].
  2111 
  2116 
  2112     picos ~~ 0 ifTrue:[
  2117     picos ~= 0 ifTrue:[
  2113         newPicos := newPicos + picos.
  2118         newPicos := (newPicos + picos) truncated.
  2114         newMillis := newMillis + (newPicos // (1000*1000*1000)).
  2119         newMillis := newMillis + (newPicos // 1000000000).
  2115         newPicos := newPicos \\ (1000*1000*1000).
  2120         newPicos := newPicos \\ 1000000000.
  2116     ].
  2121     ].
  2117     timeEncoding := newMillis.
  2122     timeEncoding := newMillis.
  2118     additionalPicoseconds := newPicos.
  2123     additionalPicoseconds := newPicos.
  2119 
  2124 
  2120     "Modified: / 18-07-2007 / 13:44:16 / cg"
  2125     "Modified: / 18-07-2007 / 13:44:16 / cg"
       
  2126     "Modified: / 22-05-2018 / 16:55:53 / Stefan Vogel"
  2121 !
  2127 !
  2122 
  2128 
  2123 setNanoseconds:nanos
  2129 setNanoseconds:nanos
  2124     "set my duration given nanoseconds."
  2130     "set my duration given nanoseconds."
  2125 
  2131 
  2130         timeEncoding := millis.
  2136         timeEncoding := millis.
  2131         restNanos := nanos \\ (1000*1000).
  2137         restNanos := nanos \\ (1000*1000).
  2132         additionalPicoseconds := restNanos * 1000 
  2138         additionalPicoseconds := restNanos * 1000 
  2133     ] ifFalse:[
  2139     ] ifFalse:[
  2134         timeEncoding := millis.
  2140         timeEncoding := millis.
  2135         restNanos := nanos - (millis * (1000*1000)).
  2141         restNanos := nanos - (millis * 1000000).
  2136         additionalPicoseconds := (restNanos * 1000) rounded asInteger.
  2142         additionalPicoseconds := (restNanos * 1000) truncated.
  2137     ].
  2143     ].
  2138 
  2144 
  2139     "
  2145     "
  2140      self new setMicroseconds:4
  2146      self new setMicroseconds:4
  2141      self new setNanoseconds:4
  2147      self new setNanoseconds:4
  2144      self new setNanoseconds:40000000
  2150      self new setNanoseconds:40000000
  2145      self new setNanoseconds:0.1
  2151      self new setNanoseconds:0.1
  2146     "
  2152     "
  2147 
  2153 
  2148     "Modified: / 18-07-2007 / 13:44:16 / cg"
  2154     "Modified: / 18-07-2007 / 13:44:16 / cg"
       
  2155     "Modified (format): / 22-05-2018 / 16:53:09 / Stefan Vogel"
  2149 !
  2156 !
  2150 
  2157 
  2151 setPicoseconds:picos
  2158 setPicoseconds:picos
  2152     "set my duration given picoseconds."
  2159     "set my duration given picoseconds."
  2153 
  2160 
  2154     timeEncoding := picos // (1000 * 1000 * 1000).
  2161     timeEncoding := picos // 1000000000.
  2155     additionalPicoseconds := (picos \\ (1000 * 1000 * 1000)) rounded asInteger.
  2162     additionalPicoseconds := (picos \\ 1000000000) truncated.
  2156 
  2163 
  2157     "
  2164     "
  2158      self new setMicroseconds:4
  2165      self new setMicroseconds:4
  2159      self new setNanoseconds:4
  2166      self new setNanoseconds:4
  2160      self new setPicoseconds:4
  2167      self new setPicoseconds:4
  2171      self assert: (self new setPicoseconds:4000000000000) = (self new setNanoseconds:4000000000) .
  2178      self assert: (self new setPicoseconds:4000000000000) = (self new setNanoseconds:4000000000) .
  2172      self assert: (self new setPicoseconds:4000000000000) = (self new setSeconds:4) .
  2179      self assert: (self new setPicoseconds:4000000000000) = (self new setSeconds:4) .
  2173     "
  2180     "
  2174 
  2181 
  2175     "Modified: / 18-07-2007 / 13:44:16 / cg"
  2182     "Modified: / 18-07-2007 / 13:44:16 / cg"
       
  2183     "Modified: / 22-05-2018 / 16:51:45 / Stefan Vogel"
  2176 !
  2184 !
  2177 
  2185 
  2178 setSeconds:secs
  2186 setSeconds:secs
  2179     "set my timeduration given seconds.
  2187     "set my timeduration given seconds.
  2180      Notice that (in contrast to Time), there is no modulu operation here.
  2188      Notice that (in contrast to Time), there is no modulu operation here.