Time.st
changeset 22839 ff149d1b48fb
parent 22809 89897abfad56
child 22886 a303a0c8a8c9
--- a/Time.st	Wed May 09 16:27:31 2018 +0200
+++ b/Time.st	Wed May 09 16:30:20 2018 +0200
@@ -48,20 +48,25 @@
     Use instances of Timestamp (and read the comment there) to do this.
 
     Time now returns the time in the local timezone.
-    Use Time utcNow to get the time in the UTC zone.
-
-    Note: time was changed recently to keep the number of milliseconds since midnight.
-          However, all existing instance creators so far only create time instances with 0-millis.
-          I.e. Time now still returns a time with second precision.
+    Use Time utcNow to get the time in the UTC zone; 
+    bare in mind that, Time instances are not prepared and made for time zone aware
+    time handling: always use Timestamp instances.
+    
+    Note: 
+      Time was changed recently to keep the number of milliseconds since midnight.
+      However, all existing instance creators so far only create time instances with 0-millis.
+      I.e. 'Time now' still returns a time with second precision.
 
-          It is not done currently, to remain backward compatible, as users may get confused
-          to see t1 > t2 although they print the same (as long as the printed representation does not
-          include the milli seconds).
-          On the other side: a change of the default printformat is not done now, 
-          as it may affect many existing applications.
+      It is not done currently, to remain backward compatible, as users may get confused
+      to see t1 > t2 although they print the same 
+      (as long as the printed representation does not include the milliseconds).
 
-          Any application which needs the millisecond precision time should call the new
-            Time nowWithMilliseconds.
+      On the other hand: 
+            a change of the default printformat is not done, 
+            as it may affect many existing applications.
+
+      Any application which needs the millisecond precision time should call the new
+        Time nowWithMilliseconds.
 
     Examples:
         |t|
@@ -476,6 +481,22 @@
     "
 
     "Modified: 1.7.1996 / 15:20:10 / cg"
+!
+
+utcNowWithMilliseconds
+    "return an instance of myself representing this moment with at least millisecond precision."
+
+    ^ self basicNew fromUtcOSTimeWithMilliseconds:(OperatingSystem getOSTime)
+
+    "
+     Time now                       -> 01:05:10 PM
+     Time nowWithMilliseconds       -> 01:05:14.359 PM
+
+     Time utcNow                    -> 11:05:16 AM
+     Time utcNowWithMilliseconds    -> 11:05:18.753 AM
+    "
+
+    "Modified: 1.7.1996 / 15:20:10 / cg"
 ! !
 
 !Time class methodsFor:'format strings'!
@@ -976,6 +997,43 @@
     "
 !
 
+printAsUTCIso8601FormatOn:aStream
+    "prints the receiver as UTC time"
+
+    Timestamp timestampISO8601Builder
+        print:self compact:false
+        asLocal:false asUTC:true withMilliseconds:true
+        timeSeparator:$T timeOnly:true
+        on:aStream
+
+    "
+     Time now printString           -> '04:23:33 PM'
+     Time now printStringIso8601    -> 'T16:23:39'  (notice: this is local time)
+
+     Time now printAsUTCIso8601FormatOn:Transcript. Transcript cr.
+     Time now printIso8601FormatOn:Transcript. Transcript cr. 
+
+    "
+!
+
+printIso8601FormatOn:aStream
+    "prints the receiver as local time"
+
+    Timestamp timestampISO8601Builder
+        print:self compact:false
+        asLocal:true asUTC:false withMilliseconds:true
+        timeSeparator:$T timeOnly:true
+        on:aStream
+
+    "
+     Time now printString           -> '04:23:33 PM'
+     Time now printStringIso8601    -> 'T16:23:39'  (notice: this is local time)
+
+     Time now printAsUTCIso8601FormatOn:Transcript. Transcript cr.
+     Time now printIso8601FormatOn:Transcript. Transcript cr. 
+    "
+!
+
 printOn:aStream
     "append a human readable printed representation of the receiver to aStream.
      The format is suitable for a human - not meant to be read back.
@@ -995,6 +1053,8 @@
 
     "
      Time now printOn:Transcript. Transcript cr
+     Time now printString
+     Time now printStringIso8601
     "
 
     "Modified: / 16-01-2011 / 11:27:24 / cg"
@@ -1062,10 +1122,33 @@
     "Modified: 1.7.1996 / 15:21:06 / cg"
 !
 
+fromUtcOSTimeWithMilliseconds:osTime
+    "set my time in the local timezone, given an osTime"
+
+    |i|
+
+    i := OperatingSystem computeUTCTimeAndDateFrom:osTime.
+    self setHours:(i hours) minutes:(i minutes) seconds:(i seconds) milliseconds:(i milliseconds)
+
+    "Modified: 1.7.1996 / 15:21:06 / cg"
+!
+
 getMilliseconds
-    "return the number of milliseconds since midnight"
+    "return the number of milliseconds since midnight.
+     Notice that the receiver may or may not have millisecond resolution,
+     depending on whether it was created by Time now or by Time nowWithMilliseconds.
+     Notice: 
+        does not include any timezone info (in contrast to Timestamp),
+        so this always represent a time depending on the context (usually: local)
+     Notice:
+        beacuse of that, always use Timestamps for comparisons."
 
     ^ timeEncoding
+
+    "
+     Time now getMilliseconds
+     Time nowWithMilliseconds getMilliseconds
+    "
 !
 
 getSeconds