Date.st
changeset 7649 1e907123a5c2
parent 7648 61b7826a9a0e
child 7650 11ef2ccbfb2e
--- a/Date.st	Thu Oct 09 18:09:39 2003 +0200
+++ b/Date.st	Thu Oct 09 21:27:56 2003 +0200
@@ -249,15 +249,18 @@
     "Modified: 8.10.1996 / 19:25:39 / cg"
 !
 
-readFrom:aStringOrStream printFormat:aFormatArray
+readFrom:aStringOrStream printFormat:aSqueakFormatArrayOrFormatString
     "return a new Date, reading a printed representation from aStream.
-     1   day position (1, 2 or 3)
-     2   month position (1..3)
-     3   year position (1..3)
+     aSqueakFormatArrayOrFormatString may either be a squeak formatArray
+         1   day position (1, 2 or 3)
+         2   month position (1..3)
+         3   year position (1..3)
+     or a formatString (see printing instance protocol).
     "
-    ^ self readFrom:aStringOrStream
-        printFormat:aFormatArray 
-            onError:[self error:'expected: ' , self name]
+    ^ self 
+        readFrom:aStringOrStream
+        printFormat:aSqueakFormatArrayOrFormatString 
+        onError:[self error:'expected: ' , self name]
 
     "
      Date readFrom:'19:11:1999' printFormat:#( 1 2 3 )
@@ -272,34 +275,80 @@
     "Modified: 8.10.1996 / 19:25:39 / cg"
 !
 
-readFrom:aStringOrStream printFormat:aFormatArray onError:exceptionBlock
+readFrom:aStringOrStream printFormat:aSqueakFormatArrayOrFormatString onError:exceptionBlock
     "return a new Date, reading a printed representation from aStream.
-     1   day position (1, 2 or 3)
-     2   month position (1..3)
-     3   year position (1..3)
+     aSqueakFormatArrayOrFormatString may either be a squeak formatArray
+         1   day position (1, 2 or 3)
+         2   month position (1..3)
+         3   year position (1..3)
+     or a formatString (see printing instance protocol).
+     TODO: make this a general feature of all DateAndTime classes.
     "
 
-    ^ [
-        |str arg year|
+    |str day month year|
+
+    str := aStringOrStream readStream.
+
+    aSqueakFormatArrayOrFormatString isArray 
+        ifTrue:[
+            [
+                |arg|
+
+                arg := Array new:3.
 
-        str := aStringOrStream readStream.
-        arg := Array new:3.
+                1 to:3 do:[:i||v|
+                    [str peek isLetterOrDigit] whileFalse:[str next].
 
-        1 to:3 do:[:i||v|
-            [str peek isLetterOrDigit] whileFalse:[str next].
+                    v := (str peek isDigit) ifTrue:[Integer readFrom:str]
+                                           ifFalse:[str nextAlphaNumericWord].
+                    arg at:i put:v
+                ].
+                year := (arg at:(aSqueakFormatArrayOrFormatString at:3)).
+                day := arg at:(aSqueakFormatArrayOrFormatString at:1).
+                month := arg at:(aSqueakFormatArrayOrFormatString at:2).
+            ] on:Error do:[^ exceptionBlock value].
+        ] ifFalse:[
+            [
+                |formatStream fc c sel|
 
-            v := (str peek isDigit) ifTrue:[Integer readFrom:str]
-                                   ifFalse:[str nextAlphaNumericWord].
-            arg at:i put:v
+                formatStream := aSqueakFormatArrayOrFormatString readStream.
+                [formatStream atEnd] whileFalse:[
+                    fc := formatStream next.
+                    fc == $% ifTrue:[
+                        sel := ''.
+                        [(fc := formatStream peek) notNil and:[ fc isLetter]] whileTrue:[ sel := sel , (formatStream next) ].
+                        sel = 'd' ifTrue:[
+                            day := Integer readFrom:str
+                        ] ifFalse:[
+                            sel = 'm' ifTrue:[
+                                month := Integer readFrom:str
+                            ] ifFalse:[
+                                sel = 'y' ifTrue:[
+                                    year := Integer readFrom:str
+                                ]
+                            ]
+                        ]
+                    ] ifFalse:[
+                        fc == Character space ifTrue:[
+                            [(c := str peek) isSeparator or:[ '.:,;/' includes:c ] ] whileTrue:[ str next ].    
+                        ] ifFalse:[
+                            str skipSeparators.
+                            str next == fc ifFalse:[ self error ].
+                            str skipSeparators.
+                        ]
+                    ]
+                ].
+            ] on:Error do:[^ exceptionBlock value].
         ].
-        year := (arg at:(aFormatArray at:3)).
-        (year between:0 and:99) ifTrue:[
-            year := UserPreferences current twoDigitDateHandler value:year.
-        ].
-        self newDay:(arg at:(aFormatArray at:1))
-              month:(arg at:(aFormatArray at:2))
-               year:year
-    ] on:Error do:exceptionBlock.
+
+    day isNil ifTrue:[ day := 1 ].
+    month isNil ifTrue:[ month := 1 ].
+    year isNil ifTrue:[ year := Date today year ].
+
+    (year between:0 and:99) ifTrue:[
+        year := UserPreferences current twoDigitDateHandler value:year.
+    ].
+    ^ self newDay:day month:month year:year.
 
     "
      Date readFrom:'31 December 1992' printFormat:#(1 2 3) onError:'fail' 
@@ -317,6 +366,11 @@
 
      Date readFrom:'31/12/92' printFormat:#(1 2 3) onError:'fail'       
      Date readFrom:'31/12/01' printFormat:#(1 2 3) onError:'fail'       
+
+     Date readFrom:'31/12/01' printFormat:'%d %m %y' onError:'fail'       
+     Date readFrom:'12/01' printFormat:'%m %y' onError:'fail'       
+     Date readFrom:'01' printFormat:'%y' onError:'fail'       
+     Date readFrom:'30.01' printFormat:'%d %m' onError:'fail'       
     "
 
     "Created: 16.11.1995 / 22:50:17 / cg"
@@ -848,7 +902,6 @@
     "Created: 15.6.1996 / 15:19:25 / cg"
 ! !
 
-
 !Date class methodsFor:'obsolete instance creation'!
 
 day:day month:month year:year
@@ -989,7 +1042,6 @@
     "
 ! !
 
-
 !Date methodsFor:'Compatibility-ANSI'!
 
 dayOfWeek
@@ -1715,7 +1767,6 @@
     "
 ! !
 
-
 !Date methodsFor:'printing & storing'!
 
 addPrintBindingsTo:aDictionary
@@ -1962,7 +2013,6 @@
     "
 ! !
 
-
 !Date methodsFor:'private-accessing'!
 
 dateEncoding
@@ -1997,11 +2047,10 @@
     "Modified: 1.7.1996 / 15:23:12 / cg"
 ! !
 
-
 !Date class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.76 2003-10-09 16:09:39 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.77 2003-10-09 19:27:56 cg Exp $'
 ! !
 
 Date initialize!