TimeDuration.st
changeset 22857 d3675e380987
parent 22856 e8a1e761cdcd
child 22858 7b475b237f26
equal deleted inserted replaced
22856:e8a1e761cdcd 22857:d3675e380987
   249      or the whole duration may be negative, as in '-(1h 10m)'
   249      or the whole duration may be negative, as in '-(1h 10m)'
   250     "
   250     "
   251 
   251 
   252     ^ [
   252     ^ [
   253         |seconds millis picos restMillis 
   253         |seconds millis picos restMillis 
   254          t1 str val fraction uIdx unit unitChar1 negative defaultUnitOrNil|
   254          t1 str val fraction mantissa uIdx unit unitChar1 negative defaultUnitOrNil|
   255 
   255 
   256         defaultUnitOrNil := defaultUnitOrNilArg.
   256         defaultUnitOrNil := defaultUnitOrNilArg.
   257         str := aStringOrStream readStream.
   257         str := aStringOrStream readStream.
   258         seconds := 0.
   258         seconds := 0.
   259         millis := 0.
   259         millis := 0.
   293                     "/ hour:minutes:seconds format
   293                     "/ hour:minutes:seconds format
   294                     str next.
   294                     str next.
   295                     val := Integer readFrom:str onError:nil.
   295                     val := Integer readFrom:str onError:nil.
   296                     val isNil ifTrue:[^ exceptionBlock value].
   296                     val isNil ifTrue:[^ exceptionBlock value].
   297                     seconds := seconds + val.
   297                     seconds := seconds + val.
   298                     str peek == $. ifTrue:[
   298                     (str peek == $. or:[str peek == $,]) ifTrue:[
   299                         "/ hour:minutes:seconds.millis format
   299                         "/ hour:minutes:seconds.millis format
   300                         str next.
   300                         str next.
   301                         "/ the old code here was wrong in assuming that exactly 3 digits
   301                         "/ the old code here was wrong in assuming that exactly 3 digits
   302                         "/ are coming; thus hh:mm:ss.1 was interpreted as 1ms (instead of 100)
   302                         "/ are coming; thus hh:mm:ss.1 was interpreted as 1ms (instead of 100)
   303                         "/ thus: count the zeros...
   303                         "/ thus: count the zeros...
   304                         str peek isDigit ifTrue:[
   304                         str peek isDigit ifTrue:[
   305                             fraction := Number readMantissaFrom:str radix:10.
   305                             "/ fraction := Number readMantissaFrom:str radix:10.
   306                             fraction isNil ifTrue:[^ exceptionBlock value].
   306                             "/ fraction isNil ifTrue:[^ exceptionBlock value].
       
   307                             "/ ignore the float value; take the fraction
       
   308 
       
   309                             mantissa := Number readMantissaAndScaleFrom:str radix:10.
       
   310                             fraction := (mantissa at:2) / (10 raisedTo:(mantissa at:3)).
   307                             millis := (fraction * 1000) rounded.
   311                             millis := (fraction * 1000) rounded.
   308                         ] ifFalse:[
   312                         ] ifFalse:[
   309                             millis := 0
   313                             millis := 0
   310                         ].
   314                         ].
   311                     ]
   315                     ]
   312                 ].
   316                 ].
   313                 ^ self fromMilliseconds:(seconds*1000+millis) rounded asInteger.
   317                 ^ self fromMilliseconds:(seconds*1000+millis) rounded asInteger.
   314             ].
   318             ].
   315             ((str peek == $.) or:[(str peek == $,)]) ifTrue:[
   319             ((str peek == $.) or:[(str peek == $,)]) ifTrue:[
   316                 str next.
   320                 str next.
   317                 fraction := Number readMantissaFrom:str radix:10.
   321                 "/ fraction := Number readMantissaFrom:str radix:10.
       
   322                 "/ ignore the float value; take the fraction
       
   323                 mantissa := Number readMantissaAndScaleFrom:str radix:10.
       
   324                 fraction := (mantissa at:2) / (10 raisedTo:(mantissa at:3)).
   318                 val := val + fraction.
   325                 val := val + fraction.
   319             ].
   326             ].
   320             str skipSeparators.
   327             str skipSeparators.
   321             str atEnd ifTrue:[
   328             str atEnd ifTrue:[
   322                 defaultUnitOrNil isNil ifTrue:[
   329                 defaultUnitOrNil isNil ifTrue:[
   880 
   887 
   881 asFixedPoint
   888 asFixedPoint
   882     "answer the duration as seconds.
   889     "answer the duration as seconds.
   883      Better use the explicit getSeconds"
   890      Better use the explicit getSeconds"
   884 
   891 
   885     ^ FixedPoint numerator:timeEncoding denominator:1000 scale:4 
   892     ^ self asFixedPoint:4 
   886 
   893 
   887     "
   894     "
   888      (10 milliseconds) asFixedPoint
   895      (10 milliseconds) asFixedPoint
   889      (10 milliseconds) asFixedPoint asFixedPoint:3 
   896      (10 milliseconds) asFixedPoint asFixedPoint:3 
   890     "
   897     "
   894 
   901 
   895 asFixedPoint:scale
   902 asFixedPoint:scale
   896     "answer the duration as seconds.
   903     "answer the duration as seconds.
   897      Better use the explicit getSeconds"
   904      Better use the explicit getSeconds"
   898 
   905 
   899     ^ FixedPoint numerator:timeEncoding denominator:1000 scale:scale 
   906     |t|
   900 
   907 
   901     "
   908     t := FixedPoint numerator:timeEncoding denominator:1000 scale:scale.
   902      (10 milliseconds) asFixedPoint  
   909     additionalPicoseconds notNil ifTrue:[
   903      (10 milliseconds) asFixedPoint:3 
   910         t := t + (FixedPoint numerator:additionalPicoseconds denominator:((1000*1000)*(1000*1000)) scale:scale)
       
   911     ].
       
   912     ^ t
       
   913 
       
   914     "
       
   915      (10 milliseconds) asFixedPoint
       
   916      (10 microseconds) asFixedPoint scale:8
       
   917      (10 nanoseconds) asFixedPoint scale:8   
       
   918      (1000001 microseconds) asFixedPoint scale:8
   904     "
   919     "
   905 
   920 
   906     "Modified (comment): / 14-09-2017 / 15:15:24 / stefan"
   921     "Modified (comment): / 14-09-2017 / 15:15:24 / stefan"
   907 !
   922 !
   908 
   923 
   909 asFloat
   924 asFloat
   910     "answer the duration as seconds.
   925     "answer the duration as seconds.
   911      Better use the explicit getSeconds"
   926      Better use the explicit getSeconds"
   912 
   927 
   913     ^ timeEncoding / 1000.0
   928     |t|
       
   929 
       
   930     t := timeEncoding / 1000.0.
       
   931     additionalPicoseconds notNil ifTrue:[
       
   932         t := t + (additionalPicoseconds / ((1000.0*1000.0) * (1000.0*1000.0))).
       
   933     ].
       
   934     ^ t
   914 
   935 
   915     "
   936     "
   916      (10 milliseconds) asFloat
   937      (10 milliseconds) asFloat
       
   938      (10 microseconds) asFloat
       
   939      (10 nanoseconds) asFloat
       
   940      (1000001 microseconds) asFloat
   917     "
   941     "
   918 
   942 
   919     "Modified (comment): / 14-09-2017 / 15:15:18 / stefan"
   943     "Modified (comment): / 14-09-2017 / 15:15:18 / stefan"
   920 !
   944 !
   921 
   945 
   922 asFraction
   946 asFraction
   923     "answer the duration as seconds.
   947     "answer the duration as seconds.
   924      Better use the explicit getSeconds"
   948      Better use the explicit getSeconds"
   925 
   949 
   926     ^ timeEncoding / 1000
   950     |t|
       
   951 
       
   952     t := timeEncoding / 1000.
       
   953     additionalPicoseconds notNil ifTrue:[
       
   954         t := t + (additionalPicoseconds / ((1000*1000)*(1000*1000))).
       
   955     ].
       
   956     ^ t
   927 
   957 
   928     "
   958     "
   929      (10 milliseconds) asFraction
   959      (10 milliseconds) asFraction
   930      (1 seconds) asFraction
   960      (10 microseconds) asFraction
       
   961      (10 nanoseconds) asFraction
       
   962      (1000001 microseconds) asFraction asFloat
   931     "
   963     "
   932 
   964 
   933     "Modified (comment): / 19-01-2018 / 17:29:24 / stefan"
   965     "Modified (comment): / 19-01-2018 / 17:29:24 / stefan"
   934 !
   966 !
   935 
   967 
   949 
   981 
   950 asLongFloat
   982 asLongFloat
   951     "answer the duration as longfloat seconds.
   983     "answer the duration as longfloat seconds.
   952      Better use the explicit getSeconds"
   984      Better use the explicit getSeconds"
   953 
   985 
   954     ^ timeEncoding / 1000 asLongFloat
   986     |t|
   955 
   987 
   956     "
   988     t := timeEncoding / 1000 asLongFloat.
   957      (10 milliseconds)  asLongFloat
   989     additionalPicoseconds notNil ifTrue:[
   958     "
   990         t := t + (additionalPicoseconds / (((1000*1000) asLongFloat) * (1000*1000) asLongFloat)).
   959 
   991     ].
   960     "Modified (comment): / 21-06-2017 / 13:59:54 / cg"
   992     ^ t
       
   993 
       
   994     "
       
   995      (10 milliseconds) asLongFloat
       
   996      (10 microseconds) asLongFloat
       
   997      (10 nanoseconds) asLongFloat
       
   998      (1000001 microseconds) asLongFloat
       
   999     "
   961 !
  1000 !
   962 
  1001 
   963 asMicroseconds
  1002 asMicroseconds
   964     "answer the duration as microseconds (truncated)."
  1003     "answer the duration as microseconds (truncated)."
   965 
  1004 
   966     ^ self getMicroseconds
  1005     ^ self getMicroseconds
   967 
  1006 
   968     "
  1007     "
   969      10 milliseconds getMicroseconds
  1008      10 milliseconds asMicroseconds
   970      10 seconds getMicroseconds
  1009      1.5 milliseconds asMicroseconds
       
  1010      10 seconds asMicroseconds
   971     "
  1011     "
   972 !
  1012 !
   973 
  1013 
   974 asMilliseconds
  1014 asMilliseconds
   975     "answer the duration as milliseconds (truncated)."
  1015     "answer the duration as milliseconds (truncated)."
   986     "answer the duration as nanoseconds (truncated)."
  1026     "answer the duration as nanoseconds (truncated)."
   987 
  1027 
   988     ^ self getNanoseconds
  1028     ^ self getNanoseconds
   989 
  1029 
   990     "
  1030     "
   991      10 milliseconds getMicroseconds
  1031      10 milliseconds asNanoseconds
   992      10 seconds getMicroseconds
  1032      10 seconds asNanoseconds
   993     "
  1033     "
   994 !
  1034 !
   995 
  1035 
   996 asNumber
  1036 asNumber
   997     "answer the duration as seconds"
  1037     "answer the duration as seconds"
   998 
  1038 
   999     ^ self asExactSeconds
  1039     ^ self asExactSeconds
  1000 
  1040 
  1001     "Modified (comment): / 14-09-2017 / 15:15:00 / stefan"
  1041     "Modified (comment): / 14-09-2017 / 15:15:00 / stefan"
  1002     "Modified: / 21-09-2017 / 18:57:57 / cg"
  1042     "Modified: / 21-09-2017 / 18:57:57 / cg"
       
  1043 !
       
  1044 
       
  1045 asPicoeconds
       
  1046     "answer the duration as picoseconds (truncated)."
       
  1047 
       
  1048     ^ self getPicoseconds
       
  1049 
       
  1050     "
       
  1051      10 milliseconds asPicoeconds
       
  1052      10 seconds asPicoeconds
       
  1053     "
  1003 !
  1054 !
  1004 
  1055 
  1005 asSeconds
  1056 asSeconds
  1006     "answer the duration as seconds (truncated, for now).
  1057     "answer the duration as seconds (truncated, for now).
  1007      To get the exact number, use asExactSeconds.
  1058      To get the exact number, use asExactSeconds.
  1530                 shortFlag ifFalse:[
  1581                 shortFlag ifFalse:[
  1531                     "/ show millis
  1582                     "/ show millis
  1532                     (millis ~= 0) ifTrue:[
  1583                     (millis ~= 0) ifTrue:[
  1533                         fmt := fmt , '%S.%is'
  1584                         fmt := fmt , '%S.%is'
  1534                     ] ifFalse:[
  1585                     ] ifFalse:[
  1535                         overAllMicros := self asMicroseconds.
  1586                         overAllMicros := self microseconds.
  1536                         overAllMicros > 2 ifTrue:[
  1587                         overAllMicros > 2 ifTrue:[
  1537                             fmt := fmt , '%(micro)us'.
  1588                             fmt := fmt , '%(micro)us'.
  1538                         ] ifFalse:[
  1589                         ] ifFalse:[
  1539                             overAllNanos := self asNanoseconds.
  1590                             overAllNanos := self nanoseconds.
  1540                             overAllNanos > 2 ifTrue:[
  1591                             overAllNanos > 2 ifTrue:[
  1541                                 fmt := fmt , '%(nano)ns'.
  1592                                 fmt := fmt , '%(nano)ns'.
  1542                             ] ifFalse:[
  1593                             ] ifFalse:[
  1543                                 fmt := fmt , '%(pico)ps'.
  1594                                 fmt := fmt , '%(pico)ps'.
  1544                             ].
  1595                             ].