allow reading of times without secs/minutes.
authorClaus Gittinger <cg@exept.de>
Fri, 22 May 1998 13:21:19 +0200
changeset 3481 1da093bda862
parent 3480 aa538a7438ba
child 3482 6ac69c06ca0a
allow reading of times without secs/minutes. Leave the stream correctly positioned in case of am/pm following.
Time.st
--- a/Time.st	Fri May 22 13:13:02 1998 +0200
+++ b/Time.st	Fri May 22 13:21:19 1998 +0200
@@ -85,8 +85,8 @@
 
 readFrom:aStringOrStream onError:exceptionBlock
     "return a new Time, reading a printed representation from aStream.
-     If no pm follows the time, the string is interpreted as either 24 hour format
-     or being am."
+     If no am/pm follows the time, the string is interpreted as 
+     either 24 hour format or being am."
 
     ErrorSignal handle:[:ex |
         ^ exceptionBlock value
@@ -100,17 +100,26 @@
         hour := Integer readFrom:str onError:ex.
         (hour between:0 and:24) ifFalse:[ex value].
 
-        [str peek isDigit] whileFalse:[str next].
-        min := Integer readFrom:str onError:ex.
-        (min between:0 and:59) ifFalse:[ex value].
-
+        min := 0.
+        sec := 0.
         str atEnd ifFalse:[
-            [str peek isDigit] whileFalse:[str next].
-            sec := Integer readFrom:str onError:ex.
-            (sec between:0 and:59) ifFalse:[ex value].
+            (str peek == $:) ifTrue:[
+                str next.
+                min := Integer readFrom:str onError:ex.
+                (min between:0 and:59) ifFalse:[ex value].
 
+                (str peek == $:) ifTrue:[
+                    str next.
+                    sec := Integer readFrom:str onError:ex.
+                    (sec between:0 and:59) ifFalse:[ex value].
+                ].
+            ].
             [str peek == Character space] whileTrue:[str next].
-            (str peek == $p) ifTrue:[
+            (str peek == $p or:[str peek == $P]) ifTrue:[
+                str next.
+                (str peek == $m or:[str peek == $M]) ifTrue:[
+                    str next
+                ].
                 hour > 12 ifTrue:[ex value].
                 hour == 0 ifTrue:[ex value].
 
@@ -119,7 +128,11 @@
                     hour := hour + 12
                 ]
             ] ifFalse:[
-                (str peek == $a) ifTrue:[
+                (str peek == $a or:[str peek == $A]) ifTrue:[
+                    str next.
+                    (str peek == $m or:[str peek == $M]) ifTrue:[
+                        str next
+                    ].
                     hour >= 12 ifTrue:[ex value].
                     hour == 24 ifTrue:[
                         hour := 0.
@@ -131,6 +144,18 @@
     ]
 
     "
+     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'     
@@ -426,5 +451,5 @@
 !Time class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.33 1998-05-22 11:12:09 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.34 1998-05-22 11:21:19 cg Exp $'
 ! !