class: Date
authorClaus Gittinger <cg@exept.de>
Sat, 06 Jun 2015 12:26:43 +0200
changeset 18444 f70da4b43b10
parent 18443 314b1142aefa
child 18445 6184d4210c89
class: Date added: #readFrom:format: #readFrom:format:language:onError: #readFrom:format:onError: comment/format in: #fromDaysFrom0: #readFrom:onError: changed: #readFrom:printFormat: #readFrom:printFormat:language:onError: #readFrom:printFormat:onError: category of: #readFrom:printFormat: #readFrom:printFormat:language:onError: #readFrom:printFormat:onError:
Date.st
--- a/Date.st	Sat Jun 06 12:19:26 2015 +0200
+++ b/Date.st	Sat Jun 06 12:26:43 2015 +0200
@@ -298,7 +298,8 @@
     "return a new Date, given the day-number starting with 0 at 1.Jan 0;
      (i.e. 'Date fromDaysSince0:0' returns 1st Jan. 0).
      Date asDaysSince0 is the reverse operation.
-     Notice, that this is a private interface"
+     Notice, that this is a private interface.
+     Also notice: does not care for Gregorian/Julisn calendar change"
 
     |year rest d yearIncrement yearAsDaysFrom0|
 
@@ -446,81 +447,7 @@
     "
 !
 
-readFrom:aStringOrStream onError:exceptionBlock
-    "return a new Date, reading a printed representation from aStream.
-     Notice, that this is not the storeString format and 
-     is different from the format expected by readFrom:.
-     
-     BUG:
-       For smalltalk compatibility, this method handles american format (i.e. month/day/year),
-       the common format with letter month in the middle (10 December 2007)
-       and ISO format (yyyy-mm-dd) - as long as yyyy is > 12.
-
-       It does NOT handle the german/french and other dd-mm-yyyy formats.
-       use readFrom:printFormat:onError: for this."
-
-    ^ [
-        |str items first second month day year|
-        str := aStringOrStream readStream.
-
-        items := #(1 2 3) collect:[:idx|
-            [str peek isLetterOrDigit] whileFalse:[str next].
-            (str peek isDigit) ifTrue:[
-                Integer readFrom:str
-            ] ifFalse:[
-                str nextAlphaNumericWord
-            ].
-        ].
-
-        first := items at:1.
-        second := items at:2.
-
-        (first isInteger and:[second isInteger and:[first > 12]]) ifTrue:[
-            "ISO Date: yyyy-mm-dd"
-            year := first.
-            month := second.
-            day := items at:3.
-        ] ifFalse:[
-            second isInteger ifTrue:[
-                "must be an american date mm/dd/yy"
-                month := first.
-                day := second.
-            ] ifFalse:[
-                "3 Dec 2007"
-                day := first.
-                month := second.
-            ].
-            year := items at:3.
-        ].
-        (year between:0 and:99) ifTrue:[
-            year := UserPreferences current twoDigitDateHandler value:year.
-        ].
-        self newDay:day month:month year:year
-    ] on:Error do:exceptionBlock.
-
-    "
-     Date readFromString:'31 December 1992'  
-     Date readFrom:'19:11:1999'  
-     Date readFrom:'2007-12-31' onError:'wrong date'.  
-     Date readFromString:'December, 5 1992'  
-     Date readFromString:'12/31/1992'        
-     Date readFromString:'3-jan-95'           
-     Date readFromString:'3-jan-01'           
-     Date readFromString:'12/31/01'        
-     Date readFromString:'15.4.1992'         -> german; leads to an error
-     Date readFromString:'10.4.1992'         -> german; leads to a wrong date
-     Date readFromString:'10.4.1992' onError:['wrong date']
-     Date readFromString:'32.4.1992' onError:['wrong date']
-     Date readFromString:'fooBar' onError:['wrong date']
-     Date readFromString:'10.4' onError:['wrong date']
-     Date readFromString:'10041999' onError:['wrong date']  
-    "
-
-    "Created: 16.11.1995 / 22:50:17 / cg"
-    "Modified: 8.10.1996 / 19:25:39 / cg"
-!
-
-readFrom:aStringOrStream printFormat:aSqueakFormatArrayOrFormatString
+readFrom:aStringOrStream format:aSqueakFormatArrayOrFormatString
     "return a new Date, reading a printed representation from aStream.
      aSqueakFormatArrayOrFormatString may either be a squeak formatArray
          1   day position (1, 2 or 3)
@@ -530,33 +457,33 @@
 
     ^ self 
         readFrom:aStringOrStream
-        printFormat:aSqueakFormatArrayOrFormatString 
+        format:aSqueakFormatArrayOrFormatString 
         onError:[ self conversionErrorSignal raise ]
 
     "
-     Date readFrom:'19:11:1999' printFormat:#( 1 2 3 )
-     Date readFrom:'19-nov-1999' printFormat:#( 1 2 3 )
-     Date readFrom:'19:11:1999' printFormat:#( 2 1 3 )  -> exception: wrong month
-     Date readFrom:'5:12:1999' printFormat:#( 2 1 3 )  
-     Date readFrom:'may-12-1999' printFormat:#( 2 1 3 )  
-     Date readFrom:'1999 may 12' printFormat:#( 3 2 1 )  
-     Date readFrom:'12/31/2001' printFormat:#( 2 1 3 ) 
-     Date readFrom:' 31.08.2001' printFormat:#( 1 2 3 )    
-     Date readFrom:' 31.dec.2001' printFormat:#( 1 2 3 )    
-     Date readFrom:' 31.Dec.2001' printFormat:#( 1 2 3 )    
-     Date readFrom:' 31.dez.2001' printFormat:#( 1 2 3 )    
-
-     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'       
+     Date readFrom:'19:11:1999' format:#( 1 2 3 )
+     Date readFrom:'19-nov-1999' format:#( 1 2 3 )
+     Date readFrom:'19:11:1999' format:#( 2 1 3 )  -> exception: wrong month
+     Date readFrom:'5:12:1999' format:#( 2 1 3 )  
+     Date readFrom:'may-12-1999' format:#( 2 1 3 )  
+     Date readFrom:'1999 may 12' format:#( 3 2 1 )  
+     Date readFrom:'12/31/2001' format:#( 2 1 3 ) 
+     Date readFrom:' 31.08.2001' format:#( 1 2 3 )    
+     Date readFrom:' 31.dec.2001' format:#( 1 2 3 )    
+     Date readFrom:' 31.Dec.2001' format:#( 1 2 3 )    
+     Date readFrom:' 31.dez.2001' format:#( 1 2 3 )    
+
+     Date readFrom:'31/12/01' format:'%d %m %y' onError:'fail'       
+     Date readFrom:'12/01' format:'%m %y' onError:'fail'       
+     Date readFrom:'01' format:'%y' onError:'fail'        
+     Date readFrom:'30.01' format:'%d %m' onError:'fail'       
     "
 
     "Created: 16.11.1995 / 22:50:17 / cg"
     "Modified: 8.10.1996 / 19:25:39 / cg"
 !
 
-readFrom:aStringOrStream printFormat:aFormatStringOrSqueakFormatArray language:languageOrNil onError:exceptionBlock
+readFrom:aStringOrStream format:aFormatStringOrSqueakFormatArray language:languageOrNil onError:exceptionBlock
     "return a new Date, reading a printed representation from aStream.
      aFormatStringOrSqueakFormatArray may either be a squeak formatArray
          1   day position (1, 2 or 3)
@@ -704,7 +631,7 @@
     "Modified: 8.10.1996 / 19:25:39 / cg"
 !
 
-readFrom:aStringOrStream printFormat:aSqueakFormatArrayOrFormatString onError:exceptionBlock
+readFrom:aStringOrStream format:aSqueakFormatArrayOrFormatString onError:exceptionBlock
     "return a new Date, reading a printed representation from aStream.
      aSqueakFormatArrayOrFormatString may either be a squeak formatArray
          1   day position (1, 2 or 3)
@@ -721,32 +648,106 @@
 
     ^ self
         readFrom:aStringOrStream 
-        printFormat:aSqueakFormatArrayOrFormatString 
+        format:aSqueakFormatArrayOrFormatString 
         language:nil 
         onError:exceptionBlock
 
     "
-     Date readFrom:'31 December 1992' printFormat:#(1 2 3) onError:'fail' 
-     Date readFrom:'19:11:1999' printFormat:#(1 2 3) onError:'fail'  
-     Date readFrom:'December, 5 1992' printFormat:#(1 2 3) onError:'fail' 
-     Date readFrom:'3-jan-95' printFormat:#(1 2 3) onError:'fail'          
-     Date readFrom:'12/31/1992' printFormat:#(1 2 3) onError:'fail'       
-     Date readFrom:'15.4.1992' printFormat:#(1 2 3) onError:'wrong date'  -> german
-     Date readFrom:'10.4.1992' printFormat:#(1 2 3) onError:'fail'        -> german
-     Date readFrom:'10.4.1992' printFormat:#(1 2 3) onError:['wrong date']
-     Date readFrom:'32.4.1992' printFormat:#(1 2 3) onError:['wrong date']
-     Date readFrom:'fooBar' printFormat:#(1 2 3) onError:['wrong date']
-     Date readFrom:'10.4' printFormat:#(1 2 3) onError:['wrong date']
-     Date readFrom:'10041999' printFormat:#(1 2 3) onError:['wrong date']  
-
-     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'       
-     Date readFrom:'311201' printFormat:'%2d%2m%2y' onError:'fail'       
+     Date readFrom:'31 December 1992' format:#(1 2 3) onError:'fail' 
+     Date readFrom:'19:11:1999' format:#(1 2 3) onError:'fail'  
+     Date readFrom:'December, 5 1992' format:#(1 2 3) onError:'fail' 
+     Date readFrom:'3-jan-95' format:#(1 2 3) onError:'fail'          
+     Date readFrom:'12/31/1992' format:#(1 2 3) onError:'fail'       
+     Date readFrom:'15.4.1992' format:#(1 2 3) onError:'wrong date'  -> german
+     Date readFrom:'10.4.1992' format:#(1 2 3) onError:'fail'        -> german
+     Date readFrom:'10.4.1992' format:#(1 2 3) onError:['wrong date']
+     Date readFrom:'32.4.1992' format:#(1 2 3) onError:['wrong date']
+     Date readFrom:'fooBar' format:#(1 2 3) onError:['wrong date']
+     Date readFrom:'10.4' format:#(1 2 3) onError:['wrong date']
+     Date readFrom:'10041999' format:#(1 2 3) onError:['wrong date']  
+
+     Date readFrom:'31/12/92' format:#(1 2 3) onError:'fail'       
+     Date readFrom:'31/12/01' format:#(1 2 3) onError:'fail'       
+
+     Date readFrom:'31/12/01' format:'%d %m %y' onError:'fail'       
+     Date readFrom:'12/01' format:'%m %y' onError:'fail'       
+     Date readFrom:'01' format:'%y' onError:'fail'       
+     Date readFrom:'30.01' format:'%d %m' onError:'fail'       
+     Date readFrom:'311201' format:'%2d%2m%2y' onError:'fail'       
+    "
+
+    "Created: 16.11.1995 / 22:50:17 / cg"
+    "Modified: 8.10.1996 / 19:25:39 / cg"
+!
+
+readFrom:aStringOrStream onError:exceptionBlock
+    "return a new Date, reading a printed representation from aStream.
+     Notice, that this is not the storeString format and 
+     is different from the format expected by readFrom:.
+     
+     BUG:
+       For Smalltalk compatibility, this method handles american format (i.e. month/day/year),
+       the common format with letter month in the middle (10 December 2007)
+       and ISO format (yyyy-mm-dd) - as long as yyyy is > 12.
+
+       It does NOT handle the german/french and other dd-mm-yyyy formats.
+       use readFrom:printFormat:onError: for this."
+
+    ^ [
+        |str items first second month day year|
+        str := aStringOrStream readStream.
+
+        items := #(1 2 3) collect:[:idx|
+            [str peek isLetterOrDigit] whileFalse:[str next].
+            (str peek isDigit) ifTrue:[
+                Integer readFrom:str
+            ] ifFalse:[
+                str nextAlphaNumericWord
+            ].
+        ].
+
+        first := items at:1.
+        second := items at:2.
+
+        (first isInteger and:[second isInteger and:[first > 12]]) ifTrue:[
+            "ISO Date: yyyy-mm-dd"
+            year := first.
+            month := second.
+            day := items at:3.
+        ] ifFalse:[
+            second isInteger ifTrue:[
+                "must be an american date mm/dd/yy"
+                month := first.
+                day := second.
+            ] ifFalse:[
+                "3 Dec 2007"
+                day := first.
+                month := second.
+            ].
+            year := items at:3.
+        ].
+        (year between:0 and:99) ifTrue:[
+            year := UserPreferences current twoDigitDateHandler value:year.
+        ].
+        self newDay:day month:month year:year
+    ] on:Error do:exceptionBlock.
+
+    "
+     Date readFromString:'31 December 1992'  
+     Date readFrom:'19:11:1999'  
+     Date readFrom:'2007-12-31' onError:'wrong date'.  
+     Date readFromString:'December, 5 1992'  
+     Date readFromString:'12/31/1992'        
+     Date readFromString:'3-jan-95'           
+     Date readFromString:'3-jan-01'           
+     Date readFromString:'12/31/01'        
+     Date readFromString:'15.4.1992'         -> german; leads to an error
+     Date readFromString:'10.4.1992'         -> german; leads to a wrong date
+     Date readFromString:'10.4.1992' onError:['wrong date']
+     Date readFromString:'32.4.1992' onError:['wrong date']
+     Date readFromString:'fooBar' onError:['wrong date']
+     Date readFromString:'10.4' onError:['wrong date']
+     Date readFromString:'10041999' onError:['wrong date']  
     "
 
     "Created: 16.11.1995 / 22:50:17 / cg"
@@ -1726,6 +1727,151 @@
 
     self obsoleteMethodWarning:'use #leapYear: (2014-09)'.
     ^ self leapYear:yearInteger
+!
+
+readFrom:aStringOrStream printFormat:aSqueakFormatArrayOrFormatString
+    "OBSOLETE: kept for backward compatibility.
+     return a new Date, reading a printed representation from aStream.
+     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
+        format:aSqueakFormatArrayOrFormatString 
+
+    "
+     Date readFrom:'19:11:1999' printFormat:#( 1 2 3 )
+     Date readFrom:'19-nov-1999' printFormat:#( 1 2 3 )
+     Date readFrom:'19:11:1999' printFormat:#( 2 1 3 )  -> exception: wrong month
+     Date readFrom:'5:12:1999' printFormat:#( 2 1 3 )  
+     Date readFrom:'may-12-1999' printFormat:#( 2 1 3 )  
+     Date readFrom:'1999 may 12' printFormat:#( 3 2 1 )  
+     Date readFrom:'12/31/2001' printFormat:#( 2 1 3 ) 
+     Date readFrom:' 31.08.2001' printFormat:#( 1 2 3 )    
+     Date readFrom:' 31.dec.2001' printFormat:#( 1 2 3 )    
+     Date readFrom:' 31.Dec.2001' printFormat:#( 1 2 3 )    
+     Date readFrom:' 31.dez.2001' printFormat:#( 1 2 3 )    
+
+     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"
+    "Modified: 8.10.1996 / 19:25:39 / cg"
+!
+
+readFrom:aStringOrStream printFormat:aFormatStringOrSqueakFormatArray language:languageOrNil onError:exceptionBlock
+    "OBSOLETE: kept for backward compatibility
+     return a new Date, reading a printed representation from aStream.
+     aFormatStringOrSqueakFormatArray 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).
+     For now %d, %m, %monthName, %shortMonthName, %y, %y1900, %y2000, %y1950 and %y1980 are supported in the formatString.
+     y1900 converts 2-digit year YY into 19YY, y2000 into 20YY.
+     y1950 and y1980 are special; if the year is below 50/80, it is converted to 20YY, otherwise to 19YY. 
+     The formatString can have any of these characters '-.:,;/' as separator.
+     The format may be preceeded by a single numeric length (as in %2d) to specify how many
+     characters to read.
+     The formatString can also use a space as separator (for  ex. '%d %m %y') and any separator will be allowed.
+     However, when a character separator is defined, only that separator will be expected.
+     TODO: make this a general feature of all DateAndTime classes.
+    "
+
+    ^ self readFrom:aStringOrStream format:aFormatStringOrSqueakFormatArray language:languageOrNil onError:exceptionBlock
+
+
+    "
+     Date readFrom:'31 December 1992' format:#(1 2 3) onError:'fail' 
+     Date readFrom:'19:11:1999' format:#(1 2 3) onError:'fail'  
+     Date readFrom:'December, 5 1992' format:#(1 2 3) onError:'fail' 
+     Date readFrom:'3-jan-95' format:#(1 2 3) onError:'fail'          
+     Date readFrom:'12/31/1992' format:#(1 2 3) onError:'fail'       
+     Date readFrom:'15.4.1992' format:#(1 2 3) onError:'wrong date'  -> german
+     Date readFrom:'10.4.1992' format:#(1 2 3) onError:'fail'        -> german
+     Date readFrom:'10.4.1992' format:#(1 2 3) onError:['wrong date']
+     Date readFrom:'32.4.1992' format:#(1 2 3) onError:['wrong date']
+     Date readFrom:'fooBar' format:#(1 2 3) onError:['wrong date']
+     Date readFrom:'10.4' format:#(1 2 3) onError:['wrong date']
+     Date readFrom:'10041999' format:#(1 2 3) onError:['wrong date']  
+
+     Date readFrom:'31/12/92' format:#(1 2 3) onError:'fail'       
+     Date readFrom:'31/12/01' format:#(1 2 3) onError:'fail'       
+
+     Date readFrom:'31/12/01' format:'%d %m %y' onError:'fail'       
+     Date readFrom:'12/01' format:'%m %y' onError:'fail'       
+     Date readFrom:'01' format:'%y' onError:'fail'       
+     Date readFrom:'30.01' format:'%d %m' onError:'fail'     
+     Date readFrom:'300180' format:'%2d%2m%2y' onError:'fail' 
+     
+     Date readFrom:'300170' format:'%2d%2m%2y' onError:'fail'      - gives 2070 as year
+     Date readFrom:'300170' format:'%2d%2m%2(y1950)' onError:'fail'  - gives 1970 as year   
+     Date readFrom:'300170' format:'%2d%2m%2(y1980)' onError:'fail'  - gives 2070 as year   
+     Date readFrom:'300181' format:'%2d%2m%2(y1980)' onError:'fail'  - gives 1981 as year   
+
+     Date readFrom:'3-3-1995' format:'%d %m %y' language: #de onError:'fail'          
+     Date readFrom:'3-März-1995' format:'%d %monthName %y' language: #de onError:'fail'          
+     Date readFrom:'3-mär-1995' format:'%d %shortMonthName %y' language: #de onError:'fail'   
+     Date readFrom:'3/mär/1995' format:'%d %shortMonthName %y' language: #de onError:'fail'  
+     Date readFrom:'3/mär/1995' format:'%d-%shortMonthName-%y' language: #de onError:'fail'          
+     Date readFrom:'3-dez-1995' format:'%d %shortMonthName %y' language: #de onError:'fail'          
+     Date readFrom:'3-Dez-1995' format:'%d %shortMonthName %y' language: #de onError:'fail'          
+     Date readFrom:'3-Dezember-1995' format:'%d %monthName %y' language: #de onError:'fail'          
+    "
+
+    "Created: 16.11.1995 / 22:50:17 / cg"
+    "Modified: 8.10.1996 / 19:25:39 / cg"
+!
+
+readFrom:aStringOrStream printFormat:aSqueakFormatArrayOrFormatString onError:exceptionBlock
+    "return a new Date, reading a printed representation from aStream.
+     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).
+     All of the %-formats as in the printString are supported here.
+     (i.e. %d, %m and %y, %shortMonthName and %monthName)
+     In addition, %y1900, %y2000, %y1950 and %y1980 are supported:
+     y1900 converts 2-digit year YY into 19YY, y2000 into 20YY.
+     y1950 and y1980 are special; if the year is below 50/80, it is converted to 20YY, otherwise to 19YY. 
+     TODO: make this a general feature of all DateAndTime classes.
+    "
+
+    ^ self readFrom:aStringOrStream format:aSqueakFormatArrayOrFormatString onError:exceptionBlock
+
+    "
+     Date readFrom:'31 December 1992' format:#(1 2 3) onError:'fail' 
+     Date readFrom:'19:11:1999' format:#(1 2 3) onError:'fail'  
+     Date readFrom:'December, 5 1992' format:#(1 2 3) onError:'fail' 
+     Date readFrom:'3-jan-95' format:#(1 2 3) onError:'fail'          
+     Date readFrom:'12/31/1992' format:#(1 2 3) onError:'fail'       
+     Date readFrom:'15.4.1992' format:#(1 2 3) onError:'wrong date'  -> german
+     Date readFrom:'10.4.1992' format:#(1 2 3) onError:'fail'        -> german
+     Date readFrom:'10.4.1992' format:#(1 2 3) onError:['wrong date']
+     Date readFrom:'32.4.1992' format:#(1 2 3) onError:['wrong date']
+     Date readFrom:'fooBar' format:#(1 2 3) onError:['wrong date']
+     Date readFrom:'10.4' format:#(1 2 3) onError:['wrong date']
+     Date readFrom:'10041999' format:#(1 2 3) onError:['wrong date']  
+
+     Date readFrom:'31/12/92' format:#(1 2 3) onError:'fail'       
+     Date readFrom:'31/12/01' format:#(1 2 3) onError:'fail'       
+
+     Date readFrom:'31/12/01' format:'%d %m %y' onError:'fail'       
+     Date readFrom:'12/01' format:'%m %y' onError:'fail'       
+     Date readFrom:'01' format:'%y' onError:'fail'       
+     Date readFrom:'30.01' format:'%d %m' onError:'fail'       
+     Date readFrom:'311201' format:'%2d%2m%2y' onError:'fail'       
+    "
+
+    "Created: 16.11.1995 / 22:50:17 / cg"
+    "Modified: 8.10.1996 / 19:25:39 / cg"
 ! !
 
 !Date class methodsFor:'private'!
@@ -3564,11 +3710,11 @@
 !Date class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.171 2015-06-06 10:19:26 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.172 2015-06-06 10:26:43 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.171 2015-06-06 10:19:26 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.172 2015-06-06 10:26:43 cg Exp $'
 ! !