Date.st
changeset 22874 fbe961c2662c
parent 22835 157b20cb8547
child 23022 b50e8a99d04b
--- a/Date.st	Thu May 10 20:26:50 2018 +0200
+++ b/Date.st	Thu May 10 20:58:28 2018 +0200
@@ -432,16 +432,16 @@
 
 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
+     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).
+     or an ST/X formatString (see printing instance protocol).
      For now %d, %m, %monthName, %shortMonthName, %y, %Y, %y1900, %y2000, %y1950 and %y1980 are supported in the formatString.
      y1900 converts 2-digit year YY into 19YY, 
      y2000 into 20YY.
-     y1950, y1980 and Y are special; 
-     if the year is below 50/80/70, it is converted to 20YY, otherwise to 19YY. 
+     y1950, y1980, y1970 and Y are special; 
+     if the year is below 50/80/70/70, 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.
@@ -598,11 +598,11 @@
 
 readFrom:aStringOrStream format:aSqueakFormatArrayOrFormatString onError:exceptionBlock
     "return a new Date, reading a printed representation from aStream.
-     aSqueakFormatArrayOrFormatString may either be a squeak formatArray
+     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).
+     or an ST/X 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, %Y, %y1900, %y2000, %y1950 and %y1980 are supported:
@@ -1791,11 +1791,11 @@
 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
+     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).
+     or an ST/X 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. 
@@ -1854,11 +1854,11 @@
 
 readFrom:aStringOrStream printFormat:aSqueakFormatArrayOrFormatString onError:exceptionBlock
     "return a new Date, reading a printed representation from aStream.
-     aSqueakFormatArrayOrFormatString may either be a squeak formatArray
+     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).
+     or an ST/X 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:
@@ -2034,7 +2034,7 @@
         ^ #month -> month
     ].
 
-    format = 'y1900' ifTrue:[
+    (format sameAs: 'y1900') ifTrue:[
         year := Integer readFrom:string.
         year < 100 ifTrue:[
             ^ #year -> (year + 1900)
@@ -2042,7 +2042,7 @@
         ^ #year -> year
     ].
     
-    format = 'y1950' ifTrue:[
+    (format sameAs: 'y1950') ifTrue:[
         "shift YY into 1950..2049; for 2k support of old date strings" 
         year := Integer readFrom:string.
         year < 100 ifTrue:[
@@ -2054,7 +2054,7 @@
         ^ #year -> year
     ].
 
-    format = 'y1980' ifTrue:[
+    (format sameAs: 'y1980') ifTrue:[
         "shift YY into 1980..2079; for 2k support of old date strings" 
         year := Integer readFrom:string.
         year < 100 ifTrue:[
@@ -2066,6 +2066,18 @@
         ^ #year -> year
     ].
 
+    (format sameAs: 'y1970') ifTrue:[
+        "shift YY into 1970..2069; for 2k support of old date strings" 
+        year := Integer readFrom:string.
+        year < 100 ifTrue:[
+            year < 70 ifTrue:[
+                ^ #year -> (year + 2000)
+            ].
+            ^ #year -> (year + 1900)
+        ].
+        ^ #year -> year
+    ].
+
     (format = 'Y') ifTrue:[
         "shift YY into 1970..2069; for 2k support of old date strings" 
         year := Integer readFrom:string.
@@ -3366,22 +3378,40 @@
         %d              - day, 01..31                    0-padded to length 2
         %m              - month, 01..12                  0-padded to length 2
         %w              - week in year, 00..53           0-padded to length 2
-        %y              - year, full                     i.e. 1999, 2004
+        %y              - year, full                     i.e. '1999', '2004'
 
      special:
         %D              - day - unpadded
         %M              - month - unpadded
         %W              - week in year - unpadded
-        %Y              - year, last 2 digits only i.e. 99, 04 (danger: year 2k bug)                     
-
-        %Y1900          - year, last 2 digits of 19YY only i.e. 99, 04 (danger: year 2k bug)
-                          raises an error, if the year is not in 1900..1999    
-        %Y2000          - year, last 2 digits of 20YY only i.e. 01, 04 or 15
+        %Y              - year, last 2 digits only i.e. 1999->'99', 1904->'04', 2004->'04'
+                          (danger: year 2k bug)
+                            makes any date a 2-digit value. 
+                            Only use to process/generate old (backward compatible) datasets
+                            which were generated way before y2k, and the old (bad) values need to
+                            be regenerated.
+
+        %Y1900          - year, last 2 digits of 19YY i.e. 1999->'99', 1904->'04', 2004->error
+                         (danger: year 2k bug)
+                            raises an error, if the year is not in 1900..1999 
+                            Only use to process/generate old (backward compatible) datasets
+                            which were generated way before y2k, and the old (bad) values need to
+                            be regenerated. Same as above, but with error reporting
+
+        %Y2000          - year, last 2 digits of 20YY only i.e. 2001->'01', 2004->'04' 2099->'99'
                           raises an error, if the year is not in 2000..2099    
-        %Y1950          - year, last 2 digits of 19YY or 20YY only i.e. 01, 04 or 80
-                          raises an error, if the year is not in 1950..2049    
-        %Y1980          - year, last 2 digits of 19YY or 20YY only i.e. 01, 04 or 80
+
+        %Y1950          - year, last 2 digits of 19YY or 20YY only i.e. 2001->'01', 2049->'49', 1950->'50', 1999->'99'
+                          raises an error, if the year is not in 1950..2049.
+                            this is occasionally used to get old (pre y2k) data converted.
+
+        %Y1980          - year, last 2 digits of 19YY or 20YY only i.e. 2001->'01', 2079->'79, 1999->'99' 
+                          same as above, with boundary year at 1980
                           raises an error, if the year is not in 1980..2079    
+                            this is occasionally used to get old (pre y2k) data converted.
+
+        %Y1970          - same as above, with boundary year at 1970.
+                          raises an error, if the year is not in 1970..2069    
 
         %(weekDay)      - day in week (1->monday, 2->tuesday, ... ,7->sunday)
         %(dayOfYear)    - day in year (1..365/366)
@@ -3411,7 +3441,8 @@
 
     |day ds dsPadded0 dsPaddedB month ms msPadded0 msPaddedB 
      year weekInYear monthName shortMonthName 
-     dayInWeek dayOfWeek dayName dayOfYear shortDayName ws wsPadded0|
+     dayInWeek dayOfWeek dayName dayOfYear shortDayName ws wsPadded0
+     conv|
 
     day := self day.
     ds := day printString.
@@ -3460,27 +3491,70 @@
     aDictionary at:$m put:msPadded0.
     aDictionary at:$M put:ms.
     aDictionary at:$y put:year.
-    aDictionary at:$Y put:((year \\ 100) printStringLeftPaddedTo:2 with:$0).
-    aDictionary at:#Y1950 put:[ 
-                                (year between:1950 and:2049) ifFalse:[ 
-                                    self error:'year cannot be represented'
-                                ].
-                                (year between:1950 and:1999) ifTrue:[
-                                    (year - 1900) printStringLeftPaddedTo:2 with:$0
-                                ] ifFalse:[    
-                                    (year - 2000) printStringLeftPaddedTo:2 with:$0
-                                ]
-                            ].        
-    aDictionary at:#Y1980 put:[ 
-                                (year between:1980 and:2079) ifFalse:[ 
-                                    self error:'year cannot be represented'
-                                ].
-                                (year between:1980 and:1999) ifTrue:[
-                                    (year - 1900) printStringLeftPaddedTo:2 with:$0
-                                ] ifFalse:[    
-                                    (year - 2000) printStringLeftPaddedTo:2 with:$0
-                                ]
-                            ].        
+    aDictionary at:$Y put:[ (year \\ 100) printStringLeftPaddedTo:2 with:$0 ].
+
+    conv := 
+        [ 
+            (year between:1950 and:2049) ifFalse:[ 
+                self error:'year cannot be represented'
+            ].
+            (year between:1950 and:1999) ifTrue:[
+                (year - 1900) printStringLeftPaddedTo:2 with:$0
+            ] ifFalse:[    
+                (year - 2000) printStringLeftPaddedTo:2 with:$0
+            ]
+        ].        
+    aDictionary at:#Y1950 put:conv. 
+    aDictionary at:#y1950 put:conv. 
+
+    conv := 
+        [ 
+            (year between:1980 and:2079) ifFalse:[ 
+                self error:'year cannot be represented'
+            ].
+            (year between:1980 and:1999) ifTrue:[
+                (year - 1900) printStringLeftPaddedTo:2 with:$0
+            ] ifFalse:[    
+                (year - 2000) printStringLeftPaddedTo:2 with:$0
+            ]
+        ].        
+    aDictionary at:#Y1980 put:conv. 
+    aDictionary at:#y1980 put:conv. 
+
+    conv := 
+        [ 
+            (year between:1970 and:2069) ifFalse:[ 
+                self error:'year cannot be represented'
+            ].
+            (year between:1970 and:1999) ifTrue:[
+                (year - 1900) printStringLeftPaddedTo:2 with:$0
+            ] ifFalse:[    
+                (year - 2000) printStringLeftPaddedTo:2 with:$0
+            ]
+        ].        
+    aDictionary at:#Y1970 put:conv. 
+    aDictionary at:#y1970 put:conv. 
+
+    conv := 
+        [ 
+            (year between:2000 and:2099) ifFalse:[ 
+                self error:'year cannot be represented'
+            ].
+            (year - 2000) printStringLeftPaddedTo:2 with:$0
+        ].        
+    aDictionary at:#Y2000 put:conv. 
+    aDictionary at:#y2000 put:conv. 
+
+    conv := 
+        [ 
+            (year between:1900 and:1999) ifFalse:[ 
+                self error:'year cannot be represented'
+            ].
+            (year - 1900) printStringLeftPaddedTo:2 with:$0
+        ].        
+    aDictionary at:#Y1900 put:conv. 
+    aDictionary at:#y1900 put:conv. 
+
     aDictionary at:$w put:wsPadded0.
     aDictionary at:$W put:ws.