TimeDuration.st
changeset 22625 c329c0b27248
parent 22621 ea38224ef388
child 22633 168193740976
equal deleted inserted replaced
22624:a99e7cd55024 22625:c329c0b27248
    14 "{ Package: 'stx:libbasic' }"
    14 "{ Package: 'stx:libbasic' }"
    15 
    15 
    16 "{ NameSpace: Smalltalk }"
    16 "{ NameSpace: Smalltalk }"
    17 
    17 
    18 Time subclass:#TimeDuration
    18 Time subclass:#TimeDuration
    19 	instanceVariableNames:'picos'
    19 	instanceVariableNames:'picoseconds'
    20 	classVariableNames:'DefaultFormatForPrinting TimeDurationZero'
    20 	classVariableNames:'DefaultFormatForPrinting TimeDurationZero'
    21 	poolDictionaries:''
    21 	poolDictionaries:''
    22 	category:'Magnitude-Time'
    22 	category:'Magnitude-Time'
    23 !
    23 !
    24 
    24 
   473 ! !
   473 ! !
   474 
   474 
   475 !TimeDuration methodsFor:'accessing'!
   475 !TimeDuration methodsFor:'accessing'!
   476 
   476 
   477 days
   477 days
   478     "get the number of days"
   478     "get the (truncated) number of days.
       
   479      Use this only for printing"
   479 
   480 
   480     ^ self 
   481     ^ self 
   481         possiblyNegatedValueFromTimeEncodingInto:[:t |
   482         possiblyNegatedValueFromTimeEncodingInto:[:t |
   482             t // 1000 // 3600 // 24
   483             t // 1000 // 3600 // 24
   483         ].    
   484         ].    
   484 
   485 
   485     "
   486     "
   486      (Duration days: 9 hours: 1 minutes: 2 seconds: 3) days
   487      (Duration days:9 hours:1 minutes:2 seconds:3) days   
   487      (Duration days: -9 hours: -1 minutes: -2 seconds: -3) days
   488      (Duration days:-9 hours:-1 minutes:-2 seconds:-3) days 
   488     "
   489     "
   489 !
   490 !
   490 
   491 
   491 getExactSeconds
   492 getExactSeconds
   492     "return the exact number of seconds.
   493     "return the exact number of seconds.
   496 
   497 
   497     "Created: / 21-09-2017 / 18:52:26 / cg"
   498     "Created: / 21-09-2017 / 18:52:26 / cg"
   498 !
   499 !
   499 
   500 
   500 hours
   501 hours
   501     "get the number of hours.
   502     "get the (truncated) number of hours.
   502      (notice: that is NOT the total number of hours,
   503      notice: that is NOT the total number of hours,
   503      but the fractional part only. 
   504      but the fractional part only. 
   504      Use this only for printing"
   505      Use this only for printing"
   505 
   506 
   506     ^ self 
   507     ^ self 
   507         possiblyNegatedValueFromTimeEncodingInto:[:t |
   508         possiblyNegatedValueFromTimeEncodingInto:[:t |
   515     "Modified (comment): / 21-09-2017 / 18:53:32 / cg"
   516     "Modified (comment): / 21-09-2017 / 18:53:32 / cg"
   516 !
   517 !
   517 
   518 
   518 milliseconds
   519 milliseconds
   519     "get the milliseconds part 
   520     "get the milliseconds part 
   520      (notice: that is NOT the total number of millis,
   521      notice: that is NOT the total number of millis,
   521      but the fractional part only. 
   522      but the fractional part only. 
   522      Use this only for printing.
   523      Use this only for printing.
   523      asMilliseconds or getMilliseconds is probably what you want"
   524      asMilliseconds or getMilliseconds is probably what you want"
   524 
   525 
   525     ^ self 
   526     ^ self 
   535     "Modified (comment): / 21-09-2017 / 18:53:26 / cg"
   536     "Modified (comment): / 21-09-2017 / 18:53:26 / cg"
   536 !
   537 !
   537 
   538 
   538 minutes
   539 minutes
   539     "get the number of minutes.
   540     "get the number of minutes.
   540      (notice: that is NOT the total number of minutes,
   541      notice: that is NOT the total number of minutes,
   541      but the fractional part only. 
   542      but the fractional part only. 
   542      Use this only for printing"
   543      Use this only for printing"
   543 
   544 
   544     ^ self 
   545     ^ self 
   545         possiblyNegatedValueFromTimeEncodingInto:[:t |
   546         possiblyNegatedValueFromTimeEncodingInto:[:t |
   552     "
   553     "
   553 
   554 
   554     "Modified (comment): / 21-09-2017 / 18:53:22 / cg"
   555     "Modified (comment): / 21-09-2017 / 18:53:22 / cg"
   555 !
   556 !
   556 
   557 
   557 picos
   558 picoseconds
   558     "get the optional picoseconds part (0..1000*1000*1000)"
   559     "get the optional additional picoseconds (0..999999999)
   559 
   560      notice: that is NOT the total number of picoseconds,
   560     ^ picos ? 0
   561      but the fractional part only. 
       
   562      Use this only for printing."
       
   563 
       
   564     ^ picoseconds ? 0
   561 !
   565 !
   562 
   566 
   563 seconds
   567 seconds
   564     "get the number of seconds.
   568     "get the number of seconds.
   565      (notice: that is NOT the total number of seconds,
   569      notice: that is NOT the total number of seconds,
   566      but the fractional part only. 
   570      but the fractional part only. 
   567      Use this only for printing.
   571      Use this only for printing.
   568      asSeconds or getSeconds is probably what you want"
   572      asSeconds or getSeconds is probably what you want"
   569 
   573 
   570     ^ self 
   574     ^ self 
   675 ! !
   679 ! !
   676 
   680 
   677 !TimeDuration methodsFor:'converting'!
   681 !TimeDuration methodsFor:'converting'!
   678 
   682 
   679 asFixedPoint
   683 asFixedPoint
   680     "answer the duration as seconds"
   684     "answer the duration as seconds.
       
   685      Better use the explicit getSeconds"
   681 
   686 
   682     ^ FixedPoint numerator:timeEncoding denominator:1000 scale:4 
   687     ^ FixedPoint numerator:timeEncoding denominator:1000 scale:4 
   683 
   688 
   684     "
   689     "
   685         (10 milliseconds)  asFixedPoint
   690      (10 milliseconds) asFixedPoint
       
   691      (10 milliseconds) asFixedPoint asFixedPoint:3 
   686     "
   692     "
   687 
   693 
   688     "Modified (comment): / 14-09-2017 / 15:15:24 / stefan"
   694     "Modified (comment): / 14-09-2017 / 15:15:24 / stefan"
   689 !
   695 !
   690 
   696 
       
   697 asFixedPoint:scale
       
   698     "answer the duration as seconds.
       
   699      Better use the explicit getSeconds"
       
   700 
       
   701     ^ FixedPoint numerator:timeEncoding denominator:1000 scale:scale 
       
   702 
       
   703     "
       
   704      (10 milliseconds) asFixedPoint  
       
   705      (10 milliseconds) asFixedPoint:3 
       
   706     "
       
   707 
       
   708     "Modified (comment): / 14-09-2017 / 15:15:24 / stefan"
       
   709 !
       
   710 
   691 asFloat
   711 asFloat
   692     "answer the duration as seconds"
   712     "answer the duration as seconds.
       
   713      Better use the explicit getSeconds"
   693 
   714 
   694     ^ timeEncoding / 1000.0
   715     ^ timeEncoding / 1000.0
   695 
   716 
   696     "
   717     "
   697         (10 milliseconds)  asFloat
   718      (10 milliseconds) asFloat
   698     "
   719     "
   699 
   720 
   700     "Modified (comment): / 14-09-2017 / 15:15:18 / stefan"
   721     "Modified (comment): / 14-09-2017 / 15:15:18 / stefan"
   701 !
   722 !
   702 
   723 
   703 asFraction
   724 asFraction
   704     "answer the duration as seconds"
   725     "answer the duration as seconds.
       
   726      Better use the explicit getSeconds"
   705 
   727 
   706     ^ timeEncoding / 1000
   728     ^ timeEncoding / 1000
   707 
   729 
   708     "
   730     "
   709         (10 milliseconds) asFraction
   731      (10 milliseconds) asFraction
   710         (1 seconds) asFraction
   732      (1 seconds) asFraction
   711     "
   733     "
   712 
   734 
   713     "Modified (comment): / 19-01-2018 / 17:29:24 / stefan"
   735     "Modified (comment): / 19-01-2018 / 17:29:24 / stefan"
   714 !
   736 !
   715 
   737 
   716 asInteger
   738 asInteger
   717     "answer the duration as (truncated) seconds"
   739     "answer the duration as (truncated) seconds.
       
   740      Better use the explicit getSeconds"
   718 
   741 
   719     ^ self getSeconds
   742     ^ self getSeconds
   720 
   743 
   721     "
   744     "
   722         10 milliseconds asInteger
   745      10 milliseconds asInteger
   723     "
   746     "
   724 
   747 
   725     "Modified (comment): / 14-09-2017 / 15:14:49 / stefan"
   748     "Modified (comment): / 14-09-2017 / 15:14:49 / stefan"
   726     "Modified (comment): / 21-09-2017 / 18:57:43 / cg"
   749     "Modified (comment): / 21-09-2017 / 18:57:43 / cg"
   727 !
   750 !
   728 
   751 
   729 asLongFloat
   752 asLongFloat
   730     "answer the duration as longfloat seconds"
   753     "answer the duration as longfloat seconds.
       
   754      Better use the explicit getSeconds"
   731 
   755 
   732     ^ timeEncoding / 1000 asLongFloat
   756     ^ timeEncoding / 1000 asLongFloat
   733 
   757 
   734     "
   758     "
   735         (10 milliseconds)  asLongFloat
   759      (10 milliseconds)  asLongFloat
   736     "
   760     "
   737 
   761 
   738     "Modified (comment): / 21-06-2017 / 13:59:54 / cg"
   762     "Modified (comment): / 21-06-2017 / 13:59:54 / cg"
   739 !
   763 !
   740 
   764 
  1226     "Modified: / 18-07-2007 / 14:06:17 / cg"
  1250     "Modified: / 18-07-2007 / 14:06:17 / cg"
  1227     "Modified (comment): / 17-05-2017 / 16:35:56 / mawalch"
  1251     "Modified (comment): / 17-05-2017 / 16:35:56 / mawalch"
  1228 !
  1252 !
  1229 
  1253 
  1230 getMilliseconds
  1254 getMilliseconds
  1231     "return the number of milliseconds"
  1255     "return the number of milliseconds.
       
  1256      This is the total number of milliseconds - not just the fractional part"
  1232 
  1257 
  1233     ^ timeEncoding
  1258     ^ timeEncoding
  1234 
  1259 
  1235     "Modified: / 18-07-2007 / 13:44:33 / cg"
  1260     "Modified: / 18-07-2007 / 13:44:33 / cg"
  1236 !
  1261 !
  1237 
  1262 
  1238 getSeconds
  1263 getSeconds
  1239     "return the number of seconds (truncated)"
  1264     "return the number of seconds (truncated).
       
  1265      This is the total number of seconds - not just the fractional part"
  1240 
  1266 
  1241     ^ timeEncoding // 1000
  1267     ^ timeEncoding // 1000
  1242 
  1268 
  1243     "Modified: / 18-07-2007 / 13:44:37 / cg"
  1269     "Modified: / 18-07-2007 / 13:44:37 / cg"
  1244     "Modified (comment): / 21-09-2017 / 18:50:03 / cg"
  1270     "Modified (comment): / 21-09-2017 / 18:50:03 / cg"
  1245 !
  1271 !
  1246 
  1272 
  1247 picos:picosecondPart
  1273 picoseconds:picosecondPart
  1248     "set the optional picoseconds part (0..1000*1000*1000)"
  1274     "set the optional additional picoseconds (0..999999999)"
  1249 
  1275 
  1250     picos := picosecondPart rounded
  1276     picoseconds := picosecondPart rounded
  1251 !
  1277 !
  1252 
  1278 
  1253 possiblyNegatedValueFromTimeEncodingInto:aBlock
  1279 possiblyNegatedValueFromTimeEncodingInto:aBlock
  1254     timeEncoding < 0 ifTrue:[
  1280     timeEncoding < 0 ifTrue:[
  1255         ^ (aBlock value:(timeEncoding negated)) negated
  1281         ^ (aBlock value:(timeEncoding negated)) negated
  1271     self setMilliseconds:((h * 60 * 60 ) + (m * 60) + s) * 1000 + millis.
  1297     self setMilliseconds:((h * 60 * 60 ) + (m * 60) + s) * 1000 + millis.
  1272 !
  1298 !
  1273 
  1299 
  1274 setMilliseconds:millis
  1300 setMilliseconds:millis
  1275     "set my duration given milliseconds.
  1301     "set my duration given milliseconds.
  1276      Notice that (in contrast to Time), there is no modulo operation here.
       
  1277      Duration can be longer than a day"
  1302      Duration can be longer than a day"
  1278 
  1303 
  1279     "/ self assert:(millis isInteger).
       
  1280     timeEncoding := millis rounded
  1304     timeEncoding := millis rounded
  1281 
  1305 
  1282     "Modified: / 18-07-2007 / 13:44:16 / cg"
  1306     "Modified: / 18-07-2007 / 13:44:16 / cg"
  1283 !
  1307 !
  1284 
  1308