AbstractTime.st
changeset 22945 c6f530d70280
parent 22925 9748ba089af4
child 22988 46b7e5b5dc48
--- a/AbstractTime.st	Wed May 16 11:38:00 2018 +0200
+++ b/AbstractTime.st	Wed May 16 12:47:21 2018 +0200
@@ -848,6 +848,37 @@
     "
 !
 
+addMicroseconds:numberOfMicroSeconds
+    "return a new instance of myself, numberOfMicroSeconds afterwards."
+
+    ^ self speciesNew 
+        setMilliseconds:(self getMilliseconds + (numberOfMicroSeconds // 1000))
+        additionalPicoseconds:(self additionalPicoseconds 
+                                + ((numberOfMicroSeconds \\ 1000)*1000*1000) rounded)
+
+    "
+     |t1 t2|
+
+     t1 := Timestamp now.
+     Transcript showCR:t1.
+     t2 := t1 addMicroseconds:1000.
+     Transcript showCR:t2.
+     t2 := t1 addMicroseconds:1010.
+     Transcript showCR:t2.
+     Transcript showCR:(t2 - t1).
+     self halt.
+     Transcript showCR:(t2 - t1) asMicroseconds.
+    "
+
+    "
+     |t|
+
+     t := Time now.
+     Transcript showCR:t.
+     Transcript showCR:(t addMilliseconds:1000).
+    "
+!
+
 addMilliseconds:numberOfMilliSeconds
     "return a new instance of myself, numberOfMilliSeconds afterwards."
 
@@ -1326,18 +1357,17 @@
 !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).
+    "return the time difference as a timeDuration instance"
 
-    "/ 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:
+    "/ notice: old code returned the number of seconds here; 
+    "/ so check your code and change to
+    "/      ^ t1 getSeconds - t2 getSeconds
+    "/ for the old behavior
 
-    "/ ^ aTimestamp getSeconds - self getSeconds
+    ((aTimestamp additionalPicoseconds == 0) and:[ self additionalPicoseconds == 0 ]) ifTrue:[
+        ^ TimeDuration fromMilliseconds:(aTimestamp getMilliseconds - self getMilliseconds).
+    ].    
+    ^ TimeDuration fromMicroseconds:(aTimestamp getMicroseconds - self getMicroseconds).
 ! !
 
 
@@ -1848,6 +1878,18 @@
     self subclassResponsibility
 !
 
+getMicroseconds
+    "get the milliseconds since some point of time in the past.    
+     Since I am abstract (not knowing how the time is actually
+     represented), this must be done by a concrete class.
+     Also be aware that the returned value is concrete-class specific;
+     Time returns the micros since midnight, Timestamp since the epoch.
+     Use this only to compute relative time deltas.
+     Here is a fallback, which only returns milliseocnd precision values"
+
+    ^ self getMilliseconds * 1000
+!
+
 getMilliseconds
     "get the milliseconds since some point of time in the past.
      Since I am abstract (not knowing how the time is actually