class: Timestamp
authorClaus Gittinger <cg@exept.de>
Sat, 08 Nov 2014 01:20:46 +0100
changeset 16996 28bdfa0a3f2f
parent 16995 f228434f6e34
child 16997 f0867a7f248b
class: Timestamp added: #UTCyear:month:day:hour:minute:second:millisecond: #asTZTimestamp #setOSTimeFromUTCYear:month:day:hour:minute:second:millisecond: changed: #UTCYear:month:day:hour:minute:second:millisecond: #setOSTimeFromYear:month:day:hour:minute:second:millisecond: #year:month:day:hour:minute:second:millisecond: support timestamps after 2039
Timestamp.st
--- a/Timestamp.st	Sat Nov 08 01:08:23 2014 +0100
+++ b/Timestamp.st	Sat Nov 08 01:20:46 2014 +0100
@@ -116,10 +116,7 @@
      interpreted in the UTC timezone."
 
     ^ self basicNew
-	fromOSTime:(OperatingSystem
-			computeOSTimeFromUTCYear:y month:m day:d
-					 hour:h minute:min second:s
-					 millisecond:millis)
+            UTCyear:y month:m day:d hour:h minute:min second:s millisecond:millis
 
     "
      Timestamp UTCYear:1970 month:1 day:1 hour:0 minute:0 second:0 millisecond:0
@@ -2150,6 +2147,20 @@
     "
 !
 
+asTZTimestamp
+    "return a timestamp which represents the very same time,
+     but will represent itself as a timestamp with the local utcOffset.
+     Use this to make sure that a local timestamp can be read back in another timezone"
+
+    ^ self asTZTimestamp:self utcOffset
+
+    "see the different printStrings of:
+         Timestamp now              
+     and
+         Timestamp now asTZTimestamp
+    "
+!
+
 asTZTimestamp:utcOffset
     "return a timestamp which represents the very same time,
      but will represent itself as a timestamp with the given utcOffset"
@@ -2263,21 +2274,43 @@
 
 !Timestamp methodsFor:'initialization'!
 
+UTCyear:y month:m day:d hour:h minute:min second:s millisecond:millis
+    "private: ask the operating system to compute the internal osTime (based on the epoch),
+     given y,m,d and h,m,s in my time.
+     If that fails (because the timestamp is outside the epoch, compute it manually"
+
+    self setOSTimeFromUTCYear:y month:m day:d hour:h minute:min second:s millisecond:millis
+!
+
+setOSTimeFromUTCYear:y month:m day:d hour:h minute:min second:s millisecond:millis
+    "private: ask the operating system to compute the internal osTime (based on the epoch),
+     given y,m,d and h,m,s in local time"
+
+    Error handle:[:ex |
+        "handler for timestamps before the epoch or after the OS representable time (2038 on current Unices).
+         Then, an out-of-os-range osTime is generated here manually."
+
+        |deltaDays|
+
+        deltaDays := self class epoch asDate subtractDate:(Date newDay:d month:m year:y).
+        "/ deltadays will be negative for dates before the epoch and positive if after.
+
+        osTime := (h * 3600) + (min * 60) + s.
+        osTime := osTime - (deltaDays * 24 * 3600).
+        osTime := osTime * 1000.
+        osTime := osTime + millis.
+    ] do:[
+        osTime := OperatingSystem
+                computeOSTimeFromUTCYear:y month:m day:d
+                hour:h minute:min second:s
+                millisecond:millis
+    ]
+!
+
 setOSTimeFromYear:y month:m day:d hour:h minute:min second:s millisecond:millis
     "private: ask the operating system to compute the internal osTime (based on the epoch),
      given y,m,d and h,m,s in local time"
 
-    osTime := OperatingSystem
-                computeOSTimeFromYear:y month:m day:d
-                hour:h minute:min second:s
-                millisecond:millis
-!
-
-year:y month:m day:d hour:h minute:min second:s millisecond:millis
-    "private: ask the operating system to compute the internal osTime (based on the epoch),
-     given y,m,d and h,m,s in my time.
-     If that fails (because the timestamp is outside the epoch, compute it manually"
-
     Error handle:[:ex |
         "handler for timestamps before the epoch or after the OS representable time (2038 on current Unices).
          Then, an out-of-os-range osTime is generated here manually."
@@ -2293,8 +2326,19 @@
         osTime := osTime * 1000.
         osTime := osTime + millis.
     ] do:[
-        self setOSTimeFromYear:y month:m day:d hour:h minute:min second:s millisecond:millis
-    ].
+        osTime := OperatingSystem
+                computeOSTimeFromYear:y month:m day:d
+                hour:h minute:min second:s
+                millisecond:millis
+    ]
+!
+
+year:y month:m day:d hour:h minute:min second:s millisecond:millis
+    "private: ask the operating system to compute the internal osTime (based on the epoch),
+     given y,m,d and h,m,s in my time.
+     If that fails (because the timestamp is outside the epoch, compute it manually"
+
+    self setOSTimeFromYear:y month:m day:d hour:h minute:min second:s millisecond:millis
 ! !
 
 
@@ -3772,11 +3816,11 @@
 !Timestamp class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.191 2014-11-08 00:08:23 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.192 2014-11-08 00:20:46 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.191 2014-11-08 00:08:23 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.192 2014-11-08 00:20:46 cg Exp $'
 ! !