AbstractTime.st
changeset 22626 89f954dcc609
parent 22618 96964b12a929
child 22632 df853ecc851b
--- a/AbstractTime.st	Fri Mar 16 15:20:19 2018 +0100
+++ b/AbstractTime.st	Fri Mar 16 15:20:30 2018 +0100
@@ -254,15 +254,37 @@
     "Modified: 1.7.1996 / 15:20:10 / cg"
 !
 
+nowWithMicroseconds
+    "return an instance of myself representing this moment with at least microsecond precision."
+
+    |osTime millis micros|
+
+    osTime := OperatingSystem getOSTimeWithMicros.
+    millis := osTime at:1.
+    micros := osTime at:2.
+    ^ self basicNew fromOSTimeWithMilliseconds:millis picoseconds:(micros*(1000*1000)).
+
+    "
+     Timestamp now   
+     Time now
+     Timestamp nowWithMilliseconds
+    "
+
+    "Modified: 1.7.1996 / 15:20:10 / cg"
+!
+
 nowWithMilliseconds
-    "return an instance of myself representing this moment with millisecond precision."
+    "return an instance of myself representing this moment with at least millisecond precision."
 
     ^ self basicNew fromOSTimeWithMilliseconds:(OperatingSystem getOSTime)
 
     "
+     Time now
+     Time nowWithMilliseconds 
+
      Timestamp now   
-     Time now
-     Time nowWithMilliseconds
+     Timestamp nowWithMilliseconds
+     Timestamp nowWithMicroseconds
     "
 
     "Modified: 1.7.1996 / 15:20:10 / cg"
@@ -645,6 +667,17 @@
     "
 !
 
+microseconds
+    "return the microseconds within the current second (0..999999)"
+
+    ^ (self milliseconds * 1000) + (self picoseconds // (1000*1000)).
+
+    "
+     Timestamp now microseconds   
+     Timestamp nowWithMicroseconds microseconds   
+    "
+!
+
 minuteInDay
     "return the minutes (0..59)"
 
@@ -656,6 +689,17 @@
     "
 !
 
+picoseconds
+    "return the additional picoseconds within the current second (0..999999999).
+     Here, assume that we have none"
+
+    ^ 0.
+
+    "
+     Timestamp now picoseconds
+    "
+!
+
 secondInDay
     "return the seconds (0..59)"
 
@@ -1110,22 +1154,6 @@
     ^ self subclassResponsibility
 ! !
 
-!AbstractTime methodsFor:'double dispatching'!
-
-differenceFromTimestamp:aTimestamp
-    "/ the correct thing to do (and I will, in the future) is to
-    "/ return a TimeDuration:
-    "/
-    ^ TimeDuration fromMilliseconds:(aTimestamp getMilliseconds - self getMilliseconds).
-
-    "/ which is the same as: deltaFrom:aTimestamp
-    "/
-    "/ however, there might be old code around, which is not be prepared for
-    "/ getting a non-number (the seconds). Therefore, for the meantime,
-    "/ we return:
-
-    "/ ^ aTimestamp getSeconds - self getSeconds
-! !
 
 !AbstractTime methodsFor:'printing & storing'!
 
@@ -1140,6 +1168,7 @@
         %m      minutes, 00..59                0-padded to length 2
         %s      seconds, 00..59                0-padded to length 2
         %i      milliseconds, 000..999         0-padded to length 3
+        %j      microseconds, 000000..999999   0-padded to length 6
 
      Timestamp only:
         %(day)   day, 00..31                    0-padded to length 2
@@ -1151,14 +1180,23 @@
         %M      minutes - unpadded
         %S      seconds - unpadded
         %I      milliseconds, unpadded
+        %J      microseconds, unpadded
+        %F      subsecond fraction, unpadded with as many post-digits as appropriate
+                (i.e. for .1 , .01 , .001 etc.)
 
         %t      seconds within hour  (unpadded)
         %T      seconds from midNight  (unpadded)
 
+        %(milli) milliseconds unpadded - alias for %I for convenience
         %(milli1) milliseconds, truncated to 1/10th of a second 0..9
         %(milli2) milliseconds, truncated to 1/100th of a second 00..99 0-padded to length 2
         %(milli3) milliseconds, same as %i for convenience
 
+        %(micro) microseconds unpadded - alias for %J for convenience
+        %(micro6) microseconds, same as %j for convenience
+
+        %(fract) fraction part - alias for %F for convenience
+
      Timestamp only:
         %(Day)         - day - unpadded
         %(Month)       - month - unpadded
@@ -1219,13 +1257,16 @@
 
     "
 
-    |time hours minutes seconds millis s t|
+    |time hours minutes seconds millis micros picos 
+     millisZ3 picosZ9 fract012 fract s t|
 
     time := self asTime.
     hours := time hours.
     minutes := time minutes.
     seconds := time seconds.
     millis := self milliseconds.
+    micros := self microseconds.
+    picos := self picoseconds.
 
     aDictionary at:$H put:(s := hours printString).
     aDictionary at:$h put:(s leftPaddedTo:2 with:$0).
@@ -1237,22 +1278,40 @@
     aDictionary at:$s put:(s leftPaddedTo:2 with:$0).
 
     aDictionary at:$I put:(s := millis printString).
-    aDictionary at:$i put:(t := s leftPaddedTo:3 with:$0).
-    aDictionary at:#milli3 put:t.
+    aDictionary at:#milli put:s.
+    aDictionary at:$i put:(millisZ3 := s leftPaddedTo:3 with:$0).
+    aDictionary at:#milli3 put:millisZ3.
 
     aDictionary at:#milli1 put:((millis // 100) printString).
     aDictionary at:#milli2 put:((millis // 10) printStringLeftPaddedTo:2 with:$0).
 
+    aDictionary at:$J put:(s := micros printString).
+    aDictionary at:#micro put:s.
+    aDictionary at:$j put:(t := s leftPaddedTo:6 with:$0).
+    aDictionary at:#micro6 put:t.
+
+    picosZ9 := self picoseconds printString leftPaddedTo:9 with:$0.
+    fract012 := millisZ3,picosZ9.
+    fract := fract012 copyTo:(fract012 findLast:[:ch | ch ~~ $0] ifNone:12).
+    aDictionary at:$F put:fract.
+    aDictionary at:#fract put:fract.
+
     aDictionary at:$t put:(seconds * minutes) printString.
     aDictionary at:$T put:(seconds * minutes * hours) printString.
 
-
     "
       |dict|
       dict := Dictionary new.
       Timestamp now addBasicPrintBindingsTo:dict language:#en.
       dict inspect
     "
+    "
+      Timestamp now printStringFormat:'%(milli)'        -- millis only  
+      Timestamp now printStringFormat:'%(milli3)'       -- millis padded  
+      Timestamp now printStringFormat:'%(micro6)'       -- micros padded  
+      Timestamp now printStringFormat:'%(fract)'        -- fraction part - as needed
+      Timestamp nowWithMicroseconds printStringFormat:'%(fract)'        -- fraction part - as needed
+    "
 !
 
 addPrintBindingsTo:aDictionary
@@ -1522,6 +1581,12 @@
     self subclassResponsibility
 !
 
+fromOSTimeWithMilliseconds:anUninterpretedOSTime picoseconds:picos
+    "strictly private: set the milliseconds from an OS time (since the epoch) plus picoSeconds"
+
+    self subclassResponsibility
+!
+
 getMilliseconds
     "get the milliseconds since some point of time in the past.
      Since I am abstract (not knowing how the time is actually