class: Time
authorClaus Gittinger <cg@exept.de>
Thu, 06 Nov 2014 17:50:41 +0100
changeset 16940 38fa652b86a8
parent 16939 1c51cd6660e5
child 16941 78358165ed6b
class: Time added: #defaultFormatStringWithMilliseconds #formatStringWithMilliseconds12us #formatStringWithMilliseconds24 #fromOSTimeWithMilliseconds: comment/format in: #documentation changed: #milliseconds #printOn: #readFrom:onError: support milliseconds. old "Time now" still returns a seconds only time (for backward compatibility); if required, use "Time nowWithMilliseconds" to get a more precise time.
Time.st
--- a/Time.st	Thu Nov 06 17:47:03 2014 +0100
+++ b/Time.st	Thu Nov 06 17:50:41 2014 +0100
@@ -46,15 +46,20 @@
     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 actuallycount the number of milliseconds since midnight.
-          however, all instance creators so far only create time instances with 0-millis.
+    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.
           This may change in the future.
+
           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).
           A change of the default printformat on the other side is not done now, as it may
           affect many existing applications.
 
+          Any application which needs the millisecond precision time should call the new
+            Time nowWithMilliseconds.
+
     Examples:
         |t|
 
@@ -185,7 +190,7 @@
      either 24 hour format or being am."
 
     ^ [
-        |str hour min sec peekC|
+        |str hour min sec peekC millis|
 
         str := aStringOrStream readStream.
 
@@ -194,6 +199,8 @@
 
         min := 0.
         sec := 0.
+        millis := 0.
+
         str atEnd ifFalse:[
             peekC := str peek.
             (peekC == $:) ifTrue:[
@@ -205,6 +212,10 @@
                     str next.
                     sec := Integer readFrom:str.
                     (sec between:0 and:59) ifFalse:[^ exceptionBlock value].
+                    (str peek == $.) ifTrue:[
+                        str next.
+                        millis := (Fraction readDecimalFractionFrom:str onError:[^ exceptionBlock value]) * 1000 asInteger.
+                    ].
                 ].
                 peekC := str peek.
             ].
@@ -242,7 +253,7 @@
                 ].
             ]
         ].
-        self basicNew setHours:hour minutes:min seconds:sec
+        self basicNew setHours:hour minutes:min seconds:sec milliseconds:millis
     ] on:Error do:exceptionBlock.
 
     "
@@ -276,6 +287,10 @@
      Time readFrom:'12:00:00 pm'  
      Time readFrom:'0:00:00 pm' onError:'invalid'
      Time readFrom:'24:00:00 pm'  
+
+     Time readFrom:'13:00:00.5'    
+     Time readFrom:'13:00:00.123'    
+     Time readFrom:'1:00:00.123 pm'  
     "
 
     "Modified: 8.10.1996 / 19:32:11 / cg"
@@ -309,6 +324,14 @@
     "Created: / 16-01-2011 / 11:23:36 / cg"
 !
 
+defaultFormatStringWithMilliseconds
+    LanguageTerritory == #us ifTrue:[
+        ^ self formatStringWithMilliseconds12us
+    ] ifFalse:[
+        ^ self formatStringWithMilliseconds24
+    ]
+!
+
 formatString12us
     "return the format string used to format US times (and other areas)"
 
@@ -319,6 +342,18 @@
     "return the format string used to format non US times"
 
     ^ '%h:%m:%s'
+!
+
+formatStringWithMilliseconds12us
+    "return the format string used to format US times (and other areas)"
+
+    ^ '%u:%m:%s.i %A'
+!
+
+formatStringWithMilliseconds24
+    "return the format string used to format non US times"
+
+    ^ '%h:%m:%s.%i'
 ! !
 
 !Time methodsFor:'Compatibility-Squeak'!
@@ -447,10 +482,11 @@
 !
 
 milliseconds
-    "time does not keep milliseconds 
-     - for compatibility with Timestamp"
+    "get the milliseconds part 
+     (notice: that is NOT the total number of millis,
+     but the fractional part only. Use this only for printing"
 
-    ^ 0
+    ^ timeEncoding \\ 1000
 !
 
 minutes
@@ -703,9 +739,14 @@
      depending on the setting of LanguageTerritory.
      I dont know what ST-80 does here (12-hour format ?)"
 
+    |format|
+
+    format := (self milliseconds = 0) 
+                    ifTrue:[self class defaultFormatString]
+                    ifFalse:[self class defaultFormatStringWithMilliseconds].
     ^ self
         printOn:aStream 
-        format:(self class defaultFormatString)               
+        format:format               
 
     "
      Time now printOn:Transcript. Transcript cr
@@ -756,6 +797,15 @@
     "Modified: 1.7.1996 / 15:21:06 / cg"
 !
 
+fromOSTimeWithMilliseconds:osTime
+    "set my time in the local timezone, given an osTime"
+
+    |i|
+
+    i := OperatingSystem computeTimeAndDateFrom:osTime.
+    self setHours:(i hours) minutes:(i minutes) seconds:(i seconds) milliseconds:(i milliseconds)
+!
+
 fromUtcOSTime:osTime
     "set my time in the local timezone, given an osTime"
 
@@ -849,10 +899,10 @@
 !Time class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.94 2014-11-06 16:23:25 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.95 2014-11-06 16:50:41 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.94 2014-11-06 16:23:25 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.95 2014-11-06 16:50:41 cg Exp $'
 ! !