AbstractTime.st
changeset 22626 89f954dcc609
parent 22618 96964b12a929
child 22632 df853ecc851b
equal deleted inserted replaced
22625:c329c0b27248 22626:89f954dcc609
   252     "
   252     "
   253 
   253 
   254     "Modified: 1.7.1996 / 15:20:10 / cg"
   254     "Modified: 1.7.1996 / 15:20:10 / cg"
   255 !
   255 !
   256 
   256 
   257 nowWithMilliseconds
   257 nowWithMicroseconds
   258     "return an instance of myself representing this moment with millisecond precision."
   258     "return an instance of myself representing this moment with at least microsecond precision."
   259 
   259 
   260     ^ self basicNew fromOSTimeWithMilliseconds:(OperatingSystem getOSTime)
   260     |osTime millis micros|
       
   261 
       
   262     osTime := OperatingSystem getOSTimeWithMicros.
       
   263     millis := osTime at:1.
       
   264     micros := osTime at:2.
       
   265     ^ self basicNew fromOSTimeWithMilliseconds:millis picoseconds:(micros*(1000*1000)).
   261 
   266 
   262     "
   267     "
   263      Timestamp now   
   268      Timestamp now   
   264      Time now
   269      Time now
   265      Time nowWithMilliseconds
   270      Timestamp nowWithMilliseconds
       
   271     "
       
   272 
       
   273     "Modified: 1.7.1996 / 15:20:10 / cg"
       
   274 !
       
   275 
       
   276 nowWithMilliseconds
       
   277     "return an instance of myself representing this moment with at least millisecond precision."
       
   278 
       
   279     ^ self basicNew fromOSTimeWithMilliseconds:(OperatingSystem getOSTime)
       
   280 
       
   281     "
       
   282      Time now
       
   283      Time nowWithMilliseconds 
       
   284 
       
   285      Timestamp now   
       
   286      Timestamp nowWithMilliseconds
       
   287      Timestamp nowWithMicroseconds
   266     "
   288     "
   267 
   289 
   268     "Modified: 1.7.1996 / 15:20:10 / cg"
   290     "Modified: 1.7.1996 / 15:20:10 / cg"
   269 !
   291 !
   270 
   292 
   643      Timestamp now hourInDay 
   665      Timestamp now hourInDay 
   644      Time now hourInDay 
   666      Time now hourInDay 
   645     "
   667     "
   646 !
   668 !
   647 
   669 
       
   670 microseconds
       
   671     "return the microseconds within the current second (0..999999)"
       
   672 
       
   673     ^ (self milliseconds * 1000) + (self picoseconds // (1000*1000)).
       
   674 
       
   675     "
       
   676      Timestamp now microseconds   
       
   677      Timestamp nowWithMicroseconds microseconds   
       
   678     "
       
   679 !
       
   680 
   648 minuteInDay
   681 minuteInDay
   649     "return the minutes (0..59)"
   682     "return the minutes (0..59)"
   650 
   683 
   651     ^ self minutes.
   684     ^ self minutes.
   652 
   685 
   653     "
   686     "
   654      Timestamp now minuteInDay 
   687      Timestamp now minuteInDay 
   655      Time now minuteInDay 
   688      Time now minuteInDay 
       
   689     "
       
   690 !
       
   691 
       
   692 picoseconds
       
   693     "return the additional picoseconds within the current second (0..999999999).
       
   694      Here, assume that we have none"
       
   695 
       
   696     ^ 0.
       
   697 
       
   698     "
       
   699      Timestamp now picoseconds
   656     "
   700     "
   657 !
   701 !
   658 
   702 
   659 secondInDay
   703 secondInDay
   660     "return the seconds (0..59)"
   704     "return the seconds (0..59)"
  1108     "represent myself as a timestamp in the local timezone"
  1152     "represent myself as a timestamp in the local timezone"
  1109 
  1153 
  1110     ^ self subclassResponsibility
  1154     ^ self subclassResponsibility
  1111 ! !
  1155 ! !
  1112 
  1156 
  1113 !AbstractTime methodsFor:'double dispatching'!
       
  1114 
       
  1115 differenceFromTimestamp:aTimestamp
       
  1116     "/ the correct thing to do (and I will, in the future) is to
       
  1117     "/ return a TimeDuration:
       
  1118     "/
       
  1119     ^ TimeDuration fromMilliseconds:(aTimestamp getMilliseconds - self getMilliseconds).
       
  1120 
       
  1121     "/ which is the same as: deltaFrom:aTimestamp
       
  1122     "/
       
  1123     "/ however, there might be old code around, which is not be prepared for
       
  1124     "/ getting a non-number (the seconds). Therefore, for the meantime,
       
  1125     "/ we return:
       
  1126 
       
  1127     "/ ^ aTimestamp getSeconds - self getSeconds
       
  1128 ! !
       
  1129 
  1157 
  1130 !AbstractTime methodsFor:'printing & storing'!
  1158 !AbstractTime methodsFor:'printing & storing'!
  1131 
  1159 
  1132 addBasicPrintBindingsTo:aDictionary language:languageOrNil
  1160 addBasicPrintBindingsTo:aDictionary language:languageOrNil
  1133     "private print support: add bindings for printing to aDictionary.
  1161     "private print support: add bindings for printing to aDictionary.
  1138      bindings:
  1166      bindings:
  1139         %h      hours, 00..23 (i.e. european)  0-padded to length 2
  1167         %h      hours, 00..23 (i.e. european)  0-padded to length 2
  1140         %m      minutes, 00..59                0-padded to length 2
  1168         %m      minutes, 00..59                0-padded to length 2
  1141         %s      seconds, 00..59                0-padded to length 2
  1169         %s      seconds, 00..59                0-padded to length 2
  1142         %i      milliseconds, 000..999         0-padded to length 3
  1170         %i      milliseconds, 000..999         0-padded to length 3
       
  1171         %j      microseconds, 000000..999999   0-padded to length 6
  1143 
  1172 
  1144      Timestamp only:
  1173      Timestamp only:
  1145         %(day)   day, 00..31                    0-padded to length 2
  1174         %(day)   day, 00..31                    0-padded to length 2
  1146         %(month) month, 00..12                  0-padded to length 2
  1175         %(month) month, 00..12                  0-padded to length 2
  1147         %(year)  year, 4 digits                 0-padded to length 4
  1176         %(year)  year, 4 digits                 0-padded to length 4
  1149      special:
  1178      special:
  1150         %H      24-hours - unpadded
  1179         %H      24-hours - unpadded
  1151         %M      minutes - unpadded
  1180         %M      minutes - unpadded
  1152         %S      seconds - unpadded
  1181         %S      seconds - unpadded
  1153         %I      milliseconds, unpadded
  1182         %I      milliseconds, unpadded
       
  1183         %J      microseconds, unpadded
       
  1184         %F      subsecond fraction, unpadded with as many post-digits as appropriate
       
  1185                 (i.e. for .1 , .01 , .001 etc.)
  1154 
  1186 
  1155         %t      seconds within hour  (unpadded)
  1187         %t      seconds within hour  (unpadded)
  1156         %T      seconds from midNight  (unpadded)
  1188         %T      seconds from midNight  (unpadded)
  1157 
  1189 
       
  1190         %(milli) milliseconds unpadded - alias for %I for convenience
  1158         %(milli1) milliseconds, truncated to 1/10th of a second 0..9
  1191         %(milli1) milliseconds, truncated to 1/10th of a second 0..9
  1159         %(milli2) milliseconds, truncated to 1/100th of a second 00..99 0-padded to length 2
  1192         %(milli2) milliseconds, truncated to 1/100th of a second 00..99 0-padded to length 2
  1160         %(milli3) milliseconds, same as %i for convenience
  1193         %(milli3) milliseconds, same as %i for convenience
       
  1194 
       
  1195         %(micro) microseconds unpadded - alias for %J for convenience
       
  1196         %(micro6) microseconds, same as %j for convenience
       
  1197 
       
  1198         %(fract) fraction part - alias for %F for convenience
  1161 
  1199 
  1162      Timestamp only:
  1200      Timestamp only:
  1163         %(Day)         - day - unpadded
  1201         %(Day)         - day - unpadded
  1164         %(Month)       - month - unpadded
  1202         %(Month)       - month - unpadded
  1165         %(yearOrTime)  - year or time 5 digits    as in unix-ls:
  1203         %(yearOrTime)  - year or time 5 digits    as in unix-ls:
  1217           YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
  1255           YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
  1218                 Timestamp now printStringFormat:'%(year)-%(month)-%(day)T%h:%m:%s.%(milli2)%(TZD)'
  1256                 Timestamp now printStringFormat:'%(year)-%(month)-%(day)T%h:%m:%s.%(milli2)%(TZD)'
  1219 
  1257 
  1220     "
  1258     "
  1221 
  1259 
  1222     |time hours minutes seconds millis s t|
  1260     |time hours minutes seconds millis micros picos 
       
  1261      millisZ3 picosZ9 fract012 fract s t|
  1223 
  1262 
  1224     time := self asTime.
  1263     time := self asTime.
  1225     hours := time hours.
  1264     hours := time hours.
  1226     minutes := time minutes.
  1265     minutes := time minutes.
  1227     seconds := time seconds.
  1266     seconds := time seconds.
  1228     millis := self milliseconds.
  1267     millis := self milliseconds.
       
  1268     micros := self microseconds.
       
  1269     picos := self picoseconds.
  1229 
  1270 
  1230     aDictionary at:$H put:(s := hours printString).
  1271     aDictionary at:$H put:(s := hours printString).
  1231     aDictionary at:$h put:(s leftPaddedTo:2 with:$0).
  1272     aDictionary at:$h put:(s leftPaddedTo:2 with:$0).
  1232 
  1273 
  1233     aDictionary at:$M put:(s := minutes printString).
  1274     aDictionary at:$M put:(s := minutes printString).
  1235 
  1276 
  1236     aDictionary at:$S put:(s := seconds printString).
  1277     aDictionary at:$S put:(s := seconds printString).
  1237     aDictionary at:$s put:(s leftPaddedTo:2 with:$0).
  1278     aDictionary at:$s put:(s leftPaddedTo:2 with:$0).
  1238 
  1279 
  1239     aDictionary at:$I put:(s := millis printString).
  1280     aDictionary at:$I put:(s := millis printString).
  1240     aDictionary at:$i put:(t := s leftPaddedTo:3 with:$0).
  1281     aDictionary at:#milli put:s.
  1241     aDictionary at:#milli3 put:t.
  1282     aDictionary at:$i put:(millisZ3 := s leftPaddedTo:3 with:$0).
       
  1283     aDictionary at:#milli3 put:millisZ3.
  1242 
  1284 
  1243     aDictionary at:#milli1 put:((millis // 100) printString).
  1285     aDictionary at:#milli1 put:((millis // 100) printString).
  1244     aDictionary at:#milli2 put:((millis // 10) printStringLeftPaddedTo:2 with:$0).
  1286     aDictionary at:#milli2 put:((millis // 10) printStringLeftPaddedTo:2 with:$0).
  1245 
  1287 
       
  1288     aDictionary at:$J put:(s := micros printString).
       
  1289     aDictionary at:#micro put:s.
       
  1290     aDictionary at:$j put:(t := s leftPaddedTo:6 with:$0).
       
  1291     aDictionary at:#micro6 put:t.
       
  1292 
       
  1293     picosZ9 := self picoseconds printString leftPaddedTo:9 with:$0.
       
  1294     fract012 := millisZ3,picosZ9.
       
  1295     fract := fract012 copyTo:(fract012 findLast:[:ch | ch ~~ $0] ifNone:12).
       
  1296     aDictionary at:$F put:fract.
       
  1297     aDictionary at:#fract put:fract.
       
  1298 
  1246     aDictionary at:$t put:(seconds * minutes) printString.
  1299     aDictionary at:$t put:(seconds * minutes) printString.
  1247     aDictionary at:$T put:(seconds * minutes * hours) printString.
  1300     aDictionary at:$T put:(seconds * minutes * hours) printString.
  1248 
       
  1249 
  1301 
  1250     "
  1302     "
  1251       |dict|
  1303       |dict|
  1252       dict := Dictionary new.
  1304       dict := Dictionary new.
  1253       Timestamp now addBasicPrintBindingsTo:dict language:#en.
  1305       Timestamp now addBasicPrintBindingsTo:dict language:#en.
  1254       dict inspect
  1306       dict inspect
       
  1307     "
       
  1308     "
       
  1309       Timestamp now printStringFormat:'%(milli)'        -- millis only  
       
  1310       Timestamp now printStringFormat:'%(milli3)'       -- millis padded  
       
  1311       Timestamp now printStringFormat:'%(micro6)'       -- micros padded  
       
  1312       Timestamp now printStringFormat:'%(fract)'        -- fraction part - as needed
       
  1313       Timestamp nowWithMicroseconds printStringFormat:'%(fract)'        -- fraction part - as needed
  1255     "
  1314     "
  1256 !
  1315 !
  1257 
  1316 
  1258 addPrintBindingsTo:aDictionary
  1317 addPrintBindingsTo:aDictionary
  1259     <resource: #obsolete>
  1318     <resource: #obsolete>
  1520     "strictly private: set the milliseconds from an OS time (since the epoch)"
  1579     "strictly private: set the milliseconds from an OS time (since the epoch)"
  1521 
  1580 
  1522     self subclassResponsibility
  1581     self subclassResponsibility
  1523 !
  1582 !
  1524 
  1583 
       
  1584 fromOSTimeWithMilliseconds:anUninterpretedOSTime picoseconds:picos
       
  1585     "strictly private: set the milliseconds from an OS time (since the epoch) plus picoSeconds"
       
  1586 
       
  1587     self subclassResponsibility
       
  1588 !
       
  1589 
  1525 getMilliseconds
  1590 getMilliseconds
  1526     "get the milliseconds since some point of time in the past.
  1591     "get the milliseconds since some point of time in the past.
  1527      Since I am abstract (not knowing how the time is actually
  1592      Since I am abstract (not knowing how the time is actually
  1528      represented), this must be done by a concrete class."
  1593      represented), this must be done by a concrete class."
  1529 
  1594