Time.st
changeset 6165 0ac589c3d92a
parent 6164 8335251d4cf9
child 6467 f200ac48f2a8
--- a/Time.st	Thu Nov 08 17:04:46 2001 +0100
+++ b/Time.st	Thu Nov 08 17:07:36 2001 +0100
@@ -1,6 +1,6 @@
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
-              All Rights Reserved
+	      All Rights Reserved
 
  This software is furnished under a license and may be used
  only in accordance with the terms of that license and with the
@@ -10,21 +10,21 @@
  hereby transferred.
 "
 
-"{ Package: '__NoProject__' }"
+"{ Package: 'stx:libbasic' }"
 
-Time subclass:#TimeDuration
-        instanceVariableNames:''
-        classVariableNames:''
-        poolDictionaries:''
-        category:'Magnitude-General'
+AbstractTime subclass:#Time
+	instanceVariableNames:'timeEncoding'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Magnitude-General'
 !
 
-!TimeDuration class methodsFor:'documentation'!
+!Time class methodsFor:'documentation'!
 
 copyright
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
-              All Rights Reserved
+	      All Rights Reserved
 
  This software is furnished under a license and may be used
  only in accordance with the terms of that license and with the
@@ -67,25 +67,173 @@
 "
 ! !
 
-!TimeDuration class methodsFor:'instance creation'!
+!Time class methodsFor:'instance creation'!
+
+fromSeconds:secondsSinceMidnight
+    "return a new Time, which represents the number of seconds since midNight.
+     Be careful, time wraps - if the number of seconds exceeds the number of seconds
+     in a day, a time relative to another days midnight is returned.
+     (i.e. (Time fromSeconds:0) = (Time fromSeconds:24*3600)"
+
+    |s|
+
+    s := secondsSinceMidnight \\ (3600 * 24).
+    ^ self
+        hours:(s // 3600)
+        minutes:((s \\ 3600) // 60)
+        seconds:((s \\ 3600) \\ 60)
+
+    "
+     Time fromSeconds:0     
+     Time fromSeconds:3675     
+     Time fromSeconds:36000     
+     Time fromSeconds:72000     
+     Time fromSeconds:96000      
+    "
+
+    "Modified: 8.10.1996 / 19:32:11 / cg"
 
-hours:h minutes:m seconds:s millis:millis
+
+
+!
+
+hour:h minutes:m seconds:s
+    "return an instance of Time representing the given time.
+     See also Time now / Date today / AbsoluteTime now.
+     Obsolete: please use #hours:minutes:seconds:"
+
+    <resource:#obsolete>
+
+    self obsoleteMethodWarning:'use hours:minutes:seconds:'.
+
+    ^ self hours:h minutes:m seconds:s
+
+    "
+     Time hours:2 minutes:33 seconds:0 
+     Time hours:0 minutes:0 seconds:0 
+     Time hours:24 minutes:0 seconds:0 
+     Time hours23 minutes:59 seconds:59 
+    "
+
+    "Modified: 19.4.1996 / 15:32:40 / cg"
+!
+
+hours:h minutes:m seconds:s
     "return an instance of Time representing the given time.
      See also Time now / Date today / AbsoluteTime now."
 
-    ^ self basicNew setHours:h minutes:m seconds:s millis:millis
+    ^ self basicNew setHours:h minutes:m seconds:s
 
     "
-     TimeDuration hours:2 minutes:33 seconds:0 millis:123
+     Time hours:2 minutes:33 seconds:0 
+     Time hours:0 minutes:0 seconds:0 
+     Time hours:24 minutes:0 seconds:0 
+     Time hours:23 minutes:59 seconds:59 
     "
+
+    "Modified: 19.4.1996 / 15:32:40 / cg"
+!
+
+readFrom:aStringOrStream onError:exceptionBlock
+    "return a new Time, reading a printed representation from aStream.
+     If no am/pm follows the time, the string is interpreted as 
+     either 24 hour format or being am."
+
+    |newTime|
+
+    ErrorSignal handle:[:ex |
+        ^ exceptionBlock value
+    ] do:[
+        |str hour min sec|
+
+        str := aStringOrStream readStream.
+
+        hour := Integer readFrom:str.
+        (hour between:0 and:24) ifFalse:[^ exceptionBlock value].
+
+        min := 0.
+        sec := 0.
+        str atEnd ifFalse:[
+            (str peek == $:) ifTrue:[
+                str next.
+                min := Integer readFrom:str.
+                (min between:0 and:59) ifFalse:[^ exceptionBlock value].
+
+                (str peek == $:) ifTrue:[
+                    str next.
+                    sec := Integer readFrom:str.
+                    (sec between:0 and:59) ifFalse:[^ exceptionBlock value].
+                ].
+            ].
+            [str peek == Character space] whileTrue:[str next].
+            (str peek == $p or:[str peek == $P]) ifTrue:[
+                str next.
+                (str peek == $m or:[str peek == $M]) ifTrue:[
+                    str next
+                ].
+                (hour <= 0 or:[hour > 12]) ifTrue:[^ exceptionBlock value].
+
+                "pm"
+                hour ~~ 12 ifTrue:[
+                    hour := hour + 12
+                ]
+            ] ifFalse:[
+                (str peek == $a or:[str peek == $A]) ifTrue:[
+                    str next.
+                    (str peek == $m or:[str peek == $M]) ifTrue:[
+                        str next
+                    ].
+                    hour >= 12 ifTrue:[^ exceptionBlock value].
+                    hour == 24 ifTrue:[
+                        hour := 0.
+                    ]
+                ]
+            ]
+        ].
+        newTime := self basicNew setHours:hour minutes:min seconds:sec
+    ].
+    ^ newTime
+
+    "
+     Time readFrom:'0:00'     
+     Time readFrom:'2:00'     
+     Time readFrom:'12:00'    
+     Time readFrom:'14:00'    
+     Time readFrom:'23:00'    
+     Time readFrom:'24:00'    
+     Time readFrom:'2:30 am'    
+     Time readFrom:'2:30 pm'    
+     Time readFrom:'14'    
+     Time readFrom:'2 am'    
+     Time readFrom:'2 pm'    
+
+     Time readFrom:'18:22:00'    
+     Time readFrom:'14:00:11'    
+     Time readFrom:'7:00:11'     
+     Time readFrom:'24:00:00'     
+     Time readFrom:'0:00:00'     
+     Time readFrom:'12:00:00'     
+     Time readFrom:'0:00:00'     
+     Time readFrom:'6:22:00 pm'   
+     Time readFrom:'2:00:11 pm'  
+     Time readFrom:'7:00:11 am'  
+     Time readFrom:'12:00:00 am'  
+     Time readFrom:'0:00:00 am'  
+     Time readFrom:'24:00:00 am'  
+     Time readFrom:'12:00:00 pm'  
+     Time readFrom:'0:00:00 pm'   - invalid
+     Time readFrom:'24:00:00 pm'  
+    "
+
+    "Modified: 8.10.1996 / 19:32:11 / cg"
 ! !
 
-!TimeDuration class methodsFor:'format strings'!
+!Time class methodsFor:'format strings'!
 
 formatString12us
     "return the format string used to format US times (and other areas)"
 
-    ^ '%u:%m:%s.%i %a'
+    ^ '%u:%m:%s %a'
 !
 
 formatString24
@@ -213,7 +361,7 @@
 < aTime
     "return true if the receiver is before the argument"
 
-    ^ timeEncoding < aTime timeEncoding
+    ^ self getSeconds < aTime getSeconds
 !
 
 = aTime
@@ -223,19 +371,19 @@
         ^ timeEncoding == aTime timeEncoding
     ].
     (aTime species == self species) ifFalse:[^ false].
-    ^ self asSeconds == aTime asSeconds
+    ^ self getSeconds == aTime getSeconds
 !
 
 > aTime
     "return true if the receiver is before the argument"
 
-    ^ timeEncoding > aTime timeEncoding
+    ^ self getSeconds > aTime getSeconds
 !
 
 hash
     "return an integer useful for hashing on times"
 
-    ^ timeEncoding
+    ^ self getSeconds
 ! !
 
 !Time methodsFor:'converting'!
@@ -422,35 +570,63 @@
 getMilliseconds
     "return the number of milliseconds since midnight"
 
-    ^ timeEncoding
+    ^ self getSeconds * 1000
 !
 
 getSeconds
     "return the number of seconds since midnight"
 
-    ^ timeEncoding // 1000
+    ^ timeEncoding
 !
 
-setHours:h minutes:m seconds:s millis:millis
+setHours:h minutes:m seconds:s
     "set my time given individual values"
 
-    self setMilliseconds:(((h\\24) * 60 * 60 ) + (m * 60) + s) * 1000 + millis.
+    self setSeconds:(((h\\24) * 60 * 60 ) + (m * 60) + s).
 !
 
 setMilliseconds:millis
     "set my time given milliseconds since midnight"
 
-    timeEncoding := millis
+    self setSeconds:(millis // 1000)
 !
 
 setSeconds:secs
     "set my time given seconds since midnight"
 
-    self setMilliseconds:(secs * 1000).
+    secs < 0 ifTrue:[
+        timeEncoding := (24 * 3600) - (secs negated \\ (24 * 3600))
+    ] ifFalse:[
+        timeEncoding := secs
+    ].
+    timeEncoding > (24 * 3600) ifTrue:[
+        timeEncoding := timeEncoding \\ (24 * 3600).
+    ]
+
+    "
+     Time now seconds
+     Time now timeEncoding
+     (Time now addDays:5) seconds     
+     (Time now addDays:5) timeEncoding
+    "
+!
+
+timeEncoding
+    "the internal encoding is stricktly private, 
+     and should not be used outside."
+
+    ^ timeEncoding
+!
+
+timeEncoding:encoding
+    "the internal encoding is stricktly private, 
+     and should not be used outside."
+
+    timeEncoding := encoding
 ! !
 
-!TimeDuration class methodsFor:'documentation'!
+!Time class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.51 2001-11-08 16:04:46 md Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.52 2001-11-08 16:07:36 md Exp $'
 ! !