AbsoluteTime.st
changeset 241 6f30be88e314
parent 213 3b56a17534fd
child 242 0190f298e56c
--- a/AbsoluteTime.st	Wed Feb 08 04:10:51 1995 +0100
+++ b/AbsoluteTime.st	Wed Feb 08 04:11:17 1995 +0100
@@ -10,18 +10,20 @@
  hereby transferred.
 "
 
-Magnitude subclass:#AbsoluteTime
-       instanceVariableNames:'secondsLow secondsHi'
-       classVariableNames:''
-       poolDictionaries:''
-       category:'Magnitude-General'
+'From Smalltalk/X, Version:2.10.4 on 8-feb-1995 at 12:46:45 pm'!
+
+AbstractTime subclass:#AbsoluteTime
+	 instanceVariableNames:'secondsLow secondsHi'
+	 classVariableNames:''
+	 poolDictionaries:''
+	 category:'Magnitude-General'
 !
 
 AbsoluteTime comment:'
 COPYRIGHT (c) 1989 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/Attic/AbsoluteTime.st,v 1.7 1994-11-28 20:32:07 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/AbsoluteTime.st,v 1.8 1995-02-08 03:10:47 claus Exp $
 '!
 
 !AbsoluteTime class methodsFor:'documentation'!
@@ -42,7 +44,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/Attic/AbsoluteTime.st,v 1.7 1994-11-28 20:32:07 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/AbsoluteTime.st,v 1.8 1995-02-08 03:10:47 claus Exp $
 "
 !
 
@@ -51,9 +53,10 @@
     This class represents time values in seconds from 1st. Jan 1970, as
     used in the Unix operating system. Its implementation is not the same
     as in ST-80 (which represents Time as seconds from 1. Jan 1901.
-    Actually, the implementation does not even know which time/date the
-    OperatingSystem bases its time upon - it is simply keeping the value(s)
-    as return from the OS when asked for the time.
+
+    Actually, the implementation does not depend or even know which time/date 
+    the OperatingSystem bases its time upon - it is simply keeping the value(s)
+    as return from the OS.
     For conversion, these values are given back to the OS, which will know
     how to convert these times.
     This has the advantage, that time-stamps on files (such as last-access-
@@ -64,76 +67,74 @@
     we keep low and hi 16bits of the time separately (it could have been 
     implemented using LargeIntegers though).
 
-    This class is typically abstract (it does not have to be, though).
+    This class should not be confused with Time (which only represents the
+    time within one day). Time instances cannot be used to compare times across
+    midnight; instances of AbsoluteTime can.
+
     See Time for more details.
 "
 ! !
 
 !AbsoluteTime class methodsFor:'instance creation'!
 
-secondClock
-    "return seconds of now - for GNU-ST compatibility"
+day:d month:m year:y hour:h minutes:min seconds:s
+    "return an instance of the receiver"
 
-    ^ OperatingSystem getTime
-!
+    ^ self fromOSTime:(OperatingSystem 
+                        computeTimePartsFromYear:y month:m day:d 
+                                            hour:h minute:min seconds:s)
 
-millisecondClockValue
-    "return the millisecond clock - since this one overruns
-     regularly, use only for short timing deltas."
+    "
+     AbsoluteTime day:2 month:1 year:1991 hour:12 minutes:30 seconds:0 
+     AbsoluteTime day:8 month:1 year:1995 hour:0 minutes:43 seconds:48 
+    "
+! !
 
-    ^ OperatingSystem getMillisecondTime.
-!
+!AbsoluteTime methodsFor:'private'!
 
-fromUnixTimeLow:low and:hi
-    "return an instance of Time, given the unix time.
-     Internal interface - not for public use."
-
-    ^ self basicNew setSecondsLow:low and:hi
+secondsLow
+    ^ secondsLow
 !
 
-dateAndTimeNow
-    "return an array filled with date and time"
-
-    ^ Array with:(Date today) with:(Time now)
-! !
-
-!AbsoluteTime class methodsFor:'timing evaluations'!
+setSecondsLow:secsLow and:secsHi
+    secondsHi := secsHi.
+    secondsLow := secsLow
+!
 
-secondsToRun:aBlock
-    "evaluate the argument, aBlock; return the number of seconds it took"
-
-    |startTime endTime|
-
-    startTime := self now.
-    aBlock value.
-    endTime := self now.
-    ^ endTime - startTime
+fromOSTimeLow:secsLow and:secsHi
+    secondsHi := secsHi.
+    secondsLow := secsLow
 !
 
-millisecondsToRun:aBlock
-    "evaluate the argument, aBlock; return the number of milliseconds it took"
-
-    |startTime endTime|
+secondsHi
+    ^ secondsHi
+!
 
-    startTime := self millisecondClockValue.
-    aBlock value.
-    endTime := self millisecondClockValue.
-    ^ endTime - startTime
+setSeconds:secs
+    secondsHi := secs // 16r10000.
+    secondsLow := secs \\ 16r10000
+!
+
+getSeconds
+    ^ (secondsHi * 16r10000) + secondsLow
+!
+
+fromOSTime:secs
+    secondsHi := secs // 16r10000.
+    secondsLow := secs \\ 16r10000
 ! !
 
 !AbsoluteTime methodsFor:'accessing'!
 
 hourInDay
-    "return the hour-part"
+    "return the hours (0..23)"
 
-    |hr|
+    ^ self hours
 
-    OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
-	:hours :minutes :secs |
+    "
+     AbsoluteTime now hours 
+    "
 
-	hr := hours
-    ].
-    ^ hr
 !
 
 minuteInDay
@@ -142,11 +143,16 @@
     |m|
 
     OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
-	:hours :minutes :secs |
+        :hours :minutes :secs |
 
-	m := minutes
+        m := minutes
     ].
     ^ m
+
+    "
+     AbsoluteTime now minuteInDay 
+    "
+
 !
 
 secondInDay
@@ -155,24 +161,32 @@
     |s|
 
     OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
-	:hours :minutes :secs |
+        :hours :minutes :secs |
 
-	s := secs
+        s := secs
     ].
     ^ s
+
+    "
+     AbsoluteTime now secondInDay 
+    "
+
 !
 
 day
     "return the day-in-month of the receiver (1..31).
      Obsolete; use instances of Date for this."
 
+    |d|
+
     OperatingSystem computeDatePartsOf:secondsLow and:secondsHi
-				   for:[:year :month :day |
-	^ day
-    ]
+                                   for:[:year :month :day |
+        d := day
+    ].
+    ^ d
 
     "
-     Time now day
+     AbsoluteTime now day 
     "
 !
 
@@ -180,13 +194,16 @@
     "return the month of the receiver (1..12).
      Obsolete; use instances of Date for this."
 
+    |m|
+
     OperatingSystem computeDatePartsOf:secondsLow and:secondsHi
-				   for:[:year :month :day |
-	^ month
-    ]
+                                   for:[:year :month :day |
+        m := month
+    ].
+    ^ m
 
     "
-     Time now month
+     AbsoluteTime now month
     "
 !
 
@@ -194,14 +211,71 @@
     "return the year of the receiver i.e. 1992.
      Obsolete; use instances of Date for this."
 
+    |y|
+
     OperatingSystem computeDatePartsOf:secondsLow and:secondsHi
-				   for:[:year :month :day |
-	^ year
-    ]
+                                   for:[:year :month :day |
+        y := year
+    ].
+    ^ y
+
+    "
+     AbsoluteTime now year
+    "
+!
+
+hours
+    "return the hours (0..23)"
+
+    |hr|
+
+    OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
+        :hours :minutes :secs |
+
+        hr := hours
+    ].
+    ^ hr
 
     "
-     Time now year
+     AbsoluteTime now hours 
+    "
+
+!
+
+minutes
+    "return the minutes (0..59)"
+
+    |m|
+
+    OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
+        :hours :minutes :secs |
+
+        m := minutes
+    ].
+    ^ m
+
+    "
+     AbsoluteTime now minutes 
     "
+
+!
+
+seconds
+    "return the seconds (0..59)"
+
+    |s|
+
+    OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
+        :hours :minutes :secs |
+
+        s := secs
+    ].
+    ^ s
+
+    "
+     AbsoluteTime now seconds 
+    "
+
 ! !
 
 !AbsoluteTime methodsFor:'comparing'!
@@ -229,6 +303,51 @@
     ^ (secondsLow bitShift:16) bitOr:secondsLow
 ! !
 
+!AbsoluteTime methodsFor:'converting'!
+
+asSeconds
+    "return the number of seconds elapsed since whatever time the
+     OperatingSystem bases its time upon. Since this is totally
+     OS-dependent, do not interpret the value returned by this method.
+     You can use it to add/subtract seconds or get time deltas, though."
+
+    ^ (secondsHi * 16r10000) + secondsLow
+
+    "
+     AbsoluteTime now asSeconds
+     AbsoluteTime fromSeconds:(AbsoluteTime now asSeconds + 3600) 
+     Time hour:23 minutes:33 seconds:0         
+     Time fromSeconds:((Time hour:23 minutes:33 seconds:0) asSeconds + 3600) 
+    "
+!
+
+asDate
+    "return a Date object from the receiver"
+
+    ^ Date fromOSTime:(Array with:secondsLow with:secondsHi) 
+
+    "
+     AbsoluteTime now  
+     AbsoluteTime now asDate
+     (AbsoluteTime now addTime:3600) asDate 
+     (AbsoluteTime now addTime:3600) asTime 
+     AbsoluteTime fromSeconds:(AbsoluteTime now asSeconds + 3600) 
+     (AbsoluteTime fromSeconds:(AbsoluteTime now asSeconds + 3600)) asDate  
+    "
+
+!
+
+asTime
+    ^ Time fromOSTime:(Array with:secondsLow with:secondsHi)
+
+    "
+     AbsoluteTime now  
+     AbsoluteTime now asTime
+     (AbsoluteTime now addTime:3600) asTime 
+    "
+
+! !
+
 !AbsoluteTime methodsFor:'arithmetic'!
 
 - aTime
@@ -238,73 +357,61 @@
 !
 
 addTime:timeAmount
-    "return a new Time/Date timeAmount seconds from myself"
+    "return a new instance of myself, timeAmount seconds afterwards"
 
     ^ self class new setSeconds:(self getSeconds + timeAmount)
 !
 
 subtractTime:timeAmount
-    "return a new Time/Date timeAmount seconds before myself"
+    "return a new instance opf myself, timeAmount seconds before myself"
 
     ^ self class new setSeconds:(self getSeconds - timeAmount)
 ! !
 
 !AbsoluteTime methodsFor:'printing & storing'!
 
-storeString
-    |string|
+printOn:aStream
+    |h min s d m y|
 
-    string := '(' , self class name , ' new setSecondsLow:'.
-    string := string , secondsLow storeString.
-    string := string , ' and:' , secondsHi storeString.
-    string := string , ')'.
-    ^ string
-! !
-
-!AbsoluteTime methodsFor:'converting'!
-
-asSeconds
-    "return the number of seconds elapsed since whatever time the
-     OperatingSystem bases its time upon. Since this is totally
-     OS-dependent, do not use this method. (see Time>>asSeconds)"
-
-    ^ (secondsHi * 16r10000) + secondsLow
+    OperatingSystem computeDatePartsOf:secondsLow and:secondsHi for:[
+        :year :month :day | d := day. m := month. y := year.
+    ].
+    OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
+        :hours :minutes :secs | h := hours. min := minutes. s := secs.
+    ].
+    d printOn:aStream.
+    aStream nextPut:$-.
+    m printOn:aStream.
+    aStream nextPut:$-.
+    y printOn:aStream.
+    aStream space.
+    h printOn:aStream leftPaddedTo:2 with:$0. 
+    aStream nextPut:$:.
+    min printOn:aStream leftPaddedTo:2 with:$0.
+    aStream nextPut:$:.
+    s printOn:aStream leftPaddedTo:2 with:$0.
 
     "
-     AbsoluteTime asSeconds
+     AbsoluteTime now 
+     AbsoluteTime fromSeconds:0 
+     Time now            
+     Date today         
     "
 !
 
-asDate
-    "return a Date object from the receiver"
+storeOn:aStream
+    aStream nextPut:$(; 
+            nextPutAll:self class name; 
+            nextPutAll:' new setSecondsLow:'.
+    secondsLow storeOn:aStream.
+    aStream nextPutAll:' and:'.
+    secondsHi storeOn:aStream.
+    aStream nextPut:$).
 
-    ^ Date fromOSTime:(Array with:secondsLow with:secondsHi) 
-!
+    "
+     AbsoluteTime now storeString '(AbsoluteTime new setSecondsLow:39757 and:12087)'
 
-asTime
-    ^ Time fromOSTime:(Array with:secondsLow with:secondsHi)
+     AbsoluteTime readFromString:(AbsoluteTime now storeString) 
+    "
 ! !
 
-!AbsoluteTime methodsFor:'private'!
-
-secondsLow
-    ^ secondsLow
-!
-
-secondsHi
-    ^ secondsHi
-!
-
-getSeconds
-    ^ (secondsHi * 16r10000) + secondsLow
-!
-
-setSeconds:secs
-    secondsHi := secs // 16r10000.
-    secondsLow := secs \\ 16r10000
-!
-
-setSecondsLow:secsLow and:secsHi
-    secondsHi := secsHi.
-    secondsLow := secsLow
-! !