#REFACTORING by stefan
authorStefan Vogel <sv@exept.de>
Tue, 21 Feb 2017 15:41:32 +0100
changeset 21503 d8d05099c9e5
parent 21502 cd414d26eacf
child 21504 2f6f531a264d
#REFACTORING by stefan class: Delay changed: #for: #forSeconds: #until: #wait #waitFor: better TimeDuration handling Fix until: for longer time periods
Delay.st
--- a/Delay.st	Tue Feb 21 15:23:39 2017 +0100
+++ b/Delay.st	Tue Feb 21 15:41:32 2017 +0100
@@ -153,11 +153,14 @@
 for:aTimeDuration
     "return a new Delay object for delaying aTimeDuration"
 
-    ^ self new delay:aTimeDuration getMilliseconds
+    ^ self new delay:aTimeDuration asTimeDuration getMilliseconds
 
     "
       Delay for:10 seconds
+      Delay for:2.5
     "
+
+    "Modified: / 21-02-2017 / 15:06:45 / stefan"
 !
 
 forMilliseconds:aNumber
@@ -166,22 +169,36 @@
     ^ self new delay:aNumber
 !
 
-forSeconds:aNumber
-    "return a new Delay object for delaying aNumber seconds"
+forSeconds:secondsOrTimeDuration
+    "return a new Delay object for delaying secondsOrTimeDuration seconds"
+
+    |seconds|
 
-    ^ self new delay:(aNumber * 1000) rounded
+    secondsOrTimeDuration isNumber ifTrue:[
+        seconds := secondsOrTimeDuration.
+    ] ifFalse:[
+        "converts a TimeDuration to a Fraction"
+        seconds := secondsOrTimeDuration asFraction.
+    ].
+
+    ^ self new delay:(seconds * 1000) asInteger
+
+    "Modified: / 21-02-2017 / 14:54:53 / stefan"
 !
 
 until:aTimeStamp
     "return a new Delay object, that will delay the active process
      until the system has reached the time represented by the argument."
 
-    ^ self new delay:(aTimeStamp millisecondDeltaFrom:Timestamp now)
+    ^ self new delay:(aTimeStamp - Timestamp now) getMilliseconds.
 
     "
         (self until:(Timestamp now + 1 seconds)) wait
         (self until:(Timestamp now - 30 seconds)) wait
+        (self until:Date tomorrow asTimestamp) wait
     "
+
+    "Modified (comment): / 21-02-2017 / 15:17:49 / stefan"
 !
 
 untilMilliseconds:aMillisecondTime
@@ -207,12 +224,15 @@
     "wait for the given timeDuration.
      This is a combined instance creation & wait."
 
-    ^ (self forMilliseconds:aTimeDuration getMilliseconds) wait
+    ^ (self for:aTimeDuration) wait
 
 
     "
      Delay waitFor:(TimeDuration seconds:5).
+     Delay waitFor:2.5.
     "
+
+    "Modified: / 21-02-2017 / 15:06:01 / stefan"
 !
 
 waitForMilliseconds:aNumber
@@ -307,7 +327,7 @@
      has passed (if millisecondDelta is non-nil), or the absolute millisecondTime
      has been reached (if resumptionTime non-nil)."
 
-    |wasBlocked currentDelta dueTime now then|
+    |wasBlocked currentDelta dueTime then|
 
     isInterrupted := false.
 
@@ -316,7 +336,6 @@
         currentDelta <= 0 ifTrue:[
             ^ self.
         ].
-        now := OperatingSystem getMillisecondTime.
         currentDelta > 16r0fffffff ifTrue:[
             "NOTE: the microsecondTime is increasing monotonically,
                    while millisecondTime is wrapping at 16r1fffffff.
@@ -324,7 +343,9 @@
             dueTime := OperatingSystem getMicrosecondTime + (currentDelta * 1000).
             currentDelta := 16r0fffffff.
         ].
-        then := OperatingSystem millisecondTimeAdd:now and:currentDelta .
+        then := OperatingSystem 
+                    millisecondTimeAdd:OperatingSystem getMillisecondTime 
+                    and:currentDelta .
     ] ifFalse:[
         then := resumptionTime.
     ].
@@ -334,6 +355,7 @@
         [
             Processor signal:delaySemaphore atMilliseconds:then.
             Processor activeProcess setStateTo:#timeWait if:#active.
+
             delaySemaphore wait.
 
             dueTime notNil
@@ -341,8 +363,7 @@
               and:[(currentDelta := dueTime - OperatingSystem getMicrosecondTime) > 0
               and:[
                 currentDelta := (currentDelta // 1000) min:16r0fffffff.
-                now := OperatingSystem getMillisecondTime.
-                then := OperatingSystem millisecondTimeAdd:now and:currentDelta.
+                then := OperatingSystem millisecondTimeAdd:OperatingSystem getMillisecondTime and:currentDelta.
                 true.]]]
         ] whileTrue.
     ] ensure:[
@@ -355,8 +376,8 @@
      Transcript showCR:'2'.
     "
 
-    "Modified: 26.2.1997 / 15:21:35 / cg"
-    "Modified: 18.4.1997 / 11:56:46 / stefan"
+    "Modified: / 26-02-1997 / 15:21:35 / cg"
+    "Modified (format): / 21-02-2017 / 15:13:57 / stefan"
 ! !
 
 !Delay methodsFor:'early signalling'!