Date.st
changeset 22874 fbe961c2662c
parent 22835 157b20cb8547
child 23022 b50e8a99d04b
equal deleted inserted replaced
22873:cc5d5daffdfa 22874:fbe961c2662c
   430     "Modified: 8.10.1996 / 19:25:39 / cg"
   430     "Modified: 8.10.1996 / 19:25:39 / cg"
   431 !
   431 !
   432 
   432 
   433 readFrom:aStringOrStream format:aFormatStringOrSqueakFormatArray language:languageOrNil onError:exceptionBlock
   433 readFrom:aStringOrStream format:aFormatStringOrSqueakFormatArray language:languageOrNil onError:exceptionBlock
   434     "return a new Date, reading a printed representation from aStream.
   434     "return a new Date, reading a printed representation from aStream.
   435      aFormatStringOrSqueakFormatArray may either be a squeak formatArray
   435      aFormatStringOrSqueakFormatArray may either be a Squeak formatArray:
   436          1   day position (1, 2 or 3)
   436          1   day position (1, 2 or 3)
   437          2   month position (1..3)
   437          2   month position (1..3)
   438          3   year position (1..3)
   438          3   year position (1..3)
   439      or a formatString (see printing instance protocol).
   439      or an ST/X formatString (see printing instance protocol).
   440      For now %d, %m, %monthName, %shortMonthName, %y, %Y, %y1900, %y2000, %y1950 and %y1980 are supported in the formatString.
   440      For now %d, %m, %monthName, %shortMonthName, %y, %Y, %y1900, %y2000, %y1950 and %y1980 are supported in the formatString.
   441      y1900 converts 2-digit year YY into 19YY, 
   441      y1900 converts 2-digit year YY into 19YY, 
   442      y2000 into 20YY.
   442      y2000 into 20YY.
   443      y1950, y1980 and Y are special; 
   443      y1950, y1980, y1970 and Y are special; 
   444      if the year is below 50/80/70, it is converted to 20YY, otherwise to 19YY. 
   444      if the year is below 50/80/70/70, it is converted to 20YY, otherwise to 19YY. 
   445      The formatString can have any of these characters '-.:,;/' as separator.
   445      The formatString can have any of these characters '-.:,;/' as separator.
   446      The format may be preceeded by a single numeric length (as in %2d) to specify how many
   446      The format may be preceeded by a single numeric length (as in %2d) to specify how many
   447      characters to read.
   447      characters to read.
   448      The formatString can also use a space as separator (for  ex. '%d %m %y') and any separator will be allowed.
   448      The formatString can also use a space as separator (for  ex. '%d %m %y') and any separator will be allowed.
   449      However, when a character separator is defined, only that separator will be expected.
   449      However, when a character separator is defined, only that separator will be expected.
   596     "Modified: 8.10.1996 / 19:25:39 / cg"
   596     "Modified: 8.10.1996 / 19:25:39 / cg"
   597 !
   597 !
   598 
   598 
   599 readFrom:aStringOrStream format:aSqueakFormatArrayOrFormatString onError:exceptionBlock
   599 readFrom:aStringOrStream format:aSqueakFormatArrayOrFormatString onError:exceptionBlock
   600     "return a new Date, reading a printed representation from aStream.
   600     "return a new Date, reading a printed representation from aStream.
   601      aSqueakFormatArrayOrFormatString may either be a squeak formatArray
   601      aSqueakFormatArrayOrFormatString may either be a Squeak formatArray:
   602          1   day position (1, 2 or 3)
   602          1   day position (1, 2 or 3)
   603          2   month position (1..3)
   603          2   month position (1..3)
   604          3   year position (1..3)
   604          3   year position (1..3)
   605      or a formatString (see printing instance protocol).
   605      or an ST/X formatString (see printing instance protocol).
   606      All of the %-formats as in the printString are supported here.
   606      All of the %-formats as in the printString are supported here.
   607      (i.e. %d, %m and %y, %shortMonthName and %monthName)
   607      (i.e. %d, %m and %y, %shortMonthName and %monthName)
   608      In addition, %Y, %y1900, %y2000, %y1950 and %y1980 are supported:
   608      In addition, %Y, %y1900, %y2000, %y1950 and %y1980 are supported:
   609      y1900 converts 2-digit year YY into 19YY, y2000 into 20YY.
   609      y1900 converts 2-digit year YY into 19YY, y2000 into 20YY.
   610      y1950, y1980 and Y are special; if the year is below 50/80/70, it is converted to 20YY, otherwise to 19YY. 
   610      y1950, y1980 and Y are special; if the year is below 50/80/70, it is converted to 20YY, otherwise to 19YY. 
  1789 !
  1789 !
  1790 
  1790 
  1791 readFrom:aStringOrStream printFormat:aFormatStringOrSqueakFormatArray language:languageOrNil onError:exceptionBlock
  1791 readFrom:aStringOrStream printFormat:aFormatStringOrSqueakFormatArray language:languageOrNil onError:exceptionBlock
  1792     "OBSOLETE: kept for backward compatibility
  1792     "OBSOLETE: kept for backward compatibility
  1793      return a new Date, reading a printed representation from aStream.
  1793      return a new Date, reading a printed representation from aStream.
  1794      aFormatStringOrSqueakFormatArray may either be a squeak formatArray
  1794      aFormatStringOrSqueakFormatArray may either be a Squeak formatArray
  1795          1   day position (1, 2 or 3)
  1795          1   day position (1, 2 or 3)
  1796          2   month position (1..3)
  1796          2   month position (1..3)
  1797          3   year position (1..3)
  1797          3   year position (1..3)
  1798      or a formatString (see printing instance protocol).
  1798      or an ST/X formatString (see printing instance protocol).
  1799      For now %d, %m, %monthName, %shortMonthName, %y, %y1900, %y2000, %y1950 and %y1980 are supported in the formatString.
  1799      For now %d, %m, %monthName, %shortMonthName, %y, %y1900, %y2000, %y1950 and %y1980 are supported in the formatString.
  1800      y1900 converts 2-digit year YY into 19YY, y2000 into 20YY.
  1800      y1900 converts 2-digit year YY into 19YY, y2000 into 20YY.
  1801      y1950 and y1980 are special; if the year is below 50/80, it is converted to 20YY, otherwise to 19YY. 
  1801      y1950 and y1980 are special; if the year is below 50/80, it is converted to 20YY, otherwise to 19YY. 
  1802      The formatString can have any of these characters '-.:,;/' as separator.
  1802      The formatString can have any of these characters '-.:,;/' as separator.
  1803      The format may be preceeded by a single numeric length (as in %2d) to specify how many
  1803      The format may be preceeded by a single numeric length (as in %2d) to specify how many
  1852     "Modified: 8.10.1996 / 19:25:39 / cg"
  1852     "Modified: 8.10.1996 / 19:25:39 / cg"
  1853 !
  1853 !
  1854 
  1854 
  1855 readFrom:aStringOrStream printFormat:aSqueakFormatArrayOrFormatString onError:exceptionBlock
  1855 readFrom:aStringOrStream printFormat:aSqueakFormatArrayOrFormatString onError:exceptionBlock
  1856     "return a new Date, reading a printed representation from aStream.
  1856     "return a new Date, reading a printed representation from aStream.
  1857      aSqueakFormatArrayOrFormatString may either be a squeak formatArray
  1857      aSqueakFormatArrayOrFormatString may either be a Squeak formatArray
  1858          1   day position (1, 2 or 3)
  1858          1   day position (1, 2 or 3)
  1859          2   month position (1..3)
  1859          2   month position (1..3)
  1860          3   year position (1..3)
  1860          3   year position (1..3)
  1861      or a formatString (see printing instance protocol).
  1861      or an ST/X formatString (see printing instance protocol).
  1862      All of the %-formats as in the printString are supported here.
  1862      All of the %-formats as in the printString are supported here.
  1863      (i.e. %d, %m and %y, %shortMonthName and %monthName)
  1863      (i.e. %d, %m and %y, %shortMonthName and %monthName)
  1864      In addition, %y1900, %y2000, %y1950 and %y1980 are supported:
  1864      In addition, %y1900, %y2000, %y1950 and %y1980 are supported:
  1865      y1900 converts 2-digit year YY into 19YY, y2000 into 20YY.
  1865      y1900 converts 2-digit year YY into 19YY, y2000 into 20YY.
  1866      y1950 and y1980 are special; if the year is below 50/80, it is converted to 20YY, otherwise to 19YY. 
  1866      y1950 and y1980 are special; if the year is below 50/80, it is converted to 20YY, otherwise to 19YY. 
  2032         month := (self abbreviatedMonthNamesForLanguage:languageOrNil) findFirst:[:m | monthName sameAs:m].
  2032         month := (self abbreviatedMonthNamesForLanguage:languageOrNil) findFirst:[:m | monthName sameAs:m].
  2033 "/        month == 0 ifTrue:[self error:'invalid month name'].
  2033 "/        month == 0 ifTrue:[self error:'invalid month name'].
  2034         ^ #month -> month
  2034         ^ #month -> month
  2035     ].
  2035     ].
  2036 
  2036 
  2037     format = 'y1900' ifTrue:[
  2037     (format sameAs: 'y1900') ifTrue:[
  2038         year := Integer readFrom:string.
  2038         year := Integer readFrom:string.
  2039         year < 100 ifTrue:[
  2039         year < 100 ifTrue:[
  2040             ^ #year -> (year + 1900)
  2040             ^ #year -> (year + 1900)
  2041         ].
  2041         ].
  2042         ^ #year -> year
  2042         ^ #year -> year
  2043     ].
  2043     ].
  2044     
  2044     
  2045     format = 'y1950' ifTrue:[
  2045     (format sameAs: 'y1950') ifTrue:[
  2046         "shift YY into 1950..2049; for 2k support of old date strings" 
  2046         "shift YY into 1950..2049; for 2k support of old date strings" 
  2047         year := Integer readFrom:string.
  2047         year := Integer readFrom:string.
  2048         year < 100 ifTrue:[
  2048         year < 100 ifTrue:[
  2049             year < 50 ifTrue:[
  2049             year < 50 ifTrue:[
  2050                 ^ #year -> (year + 2000)
  2050                 ^ #year -> (year + 2000)
  2052             ^ #year -> (year + 1900)
  2052             ^ #year -> (year + 1900)
  2053         ].
  2053         ].
  2054         ^ #year -> year
  2054         ^ #year -> year
  2055     ].
  2055     ].
  2056 
  2056 
  2057     format = 'y1980' ifTrue:[
  2057     (format sameAs: 'y1980') ifTrue:[
  2058         "shift YY into 1980..2079; for 2k support of old date strings" 
  2058         "shift YY into 1980..2079; for 2k support of old date strings" 
  2059         year := Integer readFrom:string.
  2059         year := Integer readFrom:string.
  2060         year < 100 ifTrue:[
  2060         year < 100 ifTrue:[
  2061             year < 80 ifTrue:[
  2061             year < 80 ifTrue:[
       
  2062                 ^ #year -> (year + 2000)
       
  2063             ].
       
  2064             ^ #year -> (year + 1900)
       
  2065         ].
       
  2066         ^ #year -> year
       
  2067     ].
       
  2068 
       
  2069     (format sameAs: 'y1970') ifTrue:[
       
  2070         "shift YY into 1970..2069; for 2k support of old date strings" 
       
  2071         year := Integer readFrom:string.
       
  2072         year < 100 ifTrue:[
       
  2073             year < 70 ifTrue:[
  2062                 ^ #year -> (year + 2000)
  2074                 ^ #year -> (year + 2000)
  2063             ].
  2075             ].
  2064             ^ #year -> (year + 1900)
  2076             ^ #year -> (year + 1900)
  2065         ].
  2077         ].
  2066         ^ #year -> year
  2078         ^ #year -> year
  3364      
  3376      
  3365      valid format items are:
  3377      valid format items are:
  3366         %d              - day, 01..31                    0-padded to length 2
  3378         %d              - day, 01..31                    0-padded to length 2
  3367         %m              - month, 01..12                  0-padded to length 2
  3379         %m              - month, 01..12                  0-padded to length 2
  3368         %w              - week in year, 00..53           0-padded to length 2
  3380         %w              - week in year, 00..53           0-padded to length 2
  3369         %y              - year, full                     i.e. 1999, 2004
  3381         %y              - year, full                     i.e. '1999', '2004'
  3370 
  3382 
  3371      special:
  3383      special:
  3372         %D              - day - unpadded
  3384         %D              - day - unpadded
  3373         %M              - month - unpadded
  3385         %M              - month - unpadded
  3374         %W              - week in year - unpadded
  3386         %W              - week in year - unpadded
  3375         %Y              - year, last 2 digits only i.e. 99, 04 (danger: year 2k bug)                     
  3387         %Y              - year, last 2 digits only i.e. 1999->'99', 1904->'04', 2004->'04'
  3376 
  3388                           (danger: year 2k bug)
  3377         %Y1900          - year, last 2 digits of 19YY only i.e. 99, 04 (danger: year 2k bug)
  3389                             makes any date a 2-digit value. 
  3378                           raises an error, if the year is not in 1900..1999    
  3390                             Only use to process/generate old (backward compatible) datasets
  3379         %Y2000          - year, last 2 digits of 20YY only i.e. 01, 04 or 15
  3391                             which were generated way before y2k, and the old (bad) values need to
       
  3392                             be regenerated.
       
  3393 
       
  3394         %Y1900          - year, last 2 digits of 19YY i.e. 1999->'99', 1904->'04', 2004->error
       
  3395                          (danger: year 2k bug)
       
  3396                             raises an error, if the year is not in 1900..1999 
       
  3397                             Only use to process/generate old (backward compatible) datasets
       
  3398                             which were generated way before y2k, and the old (bad) values need to
       
  3399                             be regenerated. Same as above, but with error reporting
       
  3400 
       
  3401         %Y2000          - year, last 2 digits of 20YY only i.e. 2001->'01', 2004->'04' 2099->'99'
  3380                           raises an error, if the year is not in 2000..2099    
  3402                           raises an error, if the year is not in 2000..2099    
  3381         %Y1950          - year, last 2 digits of 19YY or 20YY only i.e. 01, 04 or 80
  3403 
  3382                           raises an error, if the year is not in 1950..2049    
  3404         %Y1950          - year, last 2 digits of 19YY or 20YY only i.e. 2001->'01', 2049->'49', 1950->'50', 1999->'99'
  3383         %Y1980          - year, last 2 digits of 19YY or 20YY only i.e. 01, 04 or 80
  3405                           raises an error, if the year is not in 1950..2049.
       
  3406                             this is occasionally used to get old (pre y2k) data converted.
       
  3407 
       
  3408         %Y1980          - year, last 2 digits of 19YY or 20YY only i.e. 2001->'01', 2079->'79, 1999->'99' 
       
  3409                           same as above, with boundary year at 1980
  3384                           raises an error, if the year is not in 1980..2079    
  3410                           raises an error, if the year is not in 1980..2079    
       
  3411                             this is occasionally used to get old (pre y2k) data converted.
       
  3412 
       
  3413         %Y1970          - same as above, with boundary year at 1970.
       
  3414                           raises an error, if the year is not in 1970..2069    
  3385 
  3415 
  3386         %(weekDay)      - day in week (1->monday, 2->tuesday, ... ,7->sunday)
  3416         %(weekDay)      - day in week (1->monday, 2->tuesday, ... ,7->sunday)
  3387         %(dayOfYear)    - day in year (1..365/366)
  3417         %(dayOfYear)    - day in year (1..365/366)
  3388 
  3418 
  3389         %(dayName)      - full day name     
  3419         %(dayName)      - full day name     
  3409         %(yearRoman)    - year, in roman letters                    
  3439         %(yearRoman)    - year, in roman letters                    
  3410         %(monthRoman)   - month, in roman letters"                    
  3440         %(monthRoman)   - month, in roman letters"                    
  3411 
  3441 
  3412     |day ds dsPadded0 dsPaddedB month ms msPadded0 msPaddedB 
  3442     |day ds dsPadded0 dsPaddedB month ms msPadded0 msPaddedB 
  3413      year weekInYear monthName shortMonthName 
  3443      year weekInYear monthName shortMonthName 
  3414      dayInWeek dayOfWeek dayName dayOfYear shortDayName ws wsPadded0|
  3444      dayInWeek dayOfWeek dayName dayOfYear shortDayName ws wsPadded0
       
  3445      conv|
  3415 
  3446 
  3416     day := self day.
  3447     day := self day.
  3417     ds := day printString.
  3448     ds := day printString.
  3418     dsPaddedB := day printStringLeftPaddedTo:2.
  3449     dsPaddedB := day printStringLeftPaddedTo:2.
  3419     dsPadded0 := day printStringLeftPaddedTo:2 with:$0.
  3450     dsPadded0 := day printStringLeftPaddedTo:2 with:$0.
  3458     aDictionary at:$d put:dsPadded0.
  3489     aDictionary at:$d put:dsPadded0.
  3459     aDictionary at:$D put:ds.
  3490     aDictionary at:$D put:ds.
  3460     aDictionary at:$m put:msPadded0.
  3491     aDictionary at:$m put:msPadded0.
  3461     aDictionary at:$M put:ms.
  3492     aDictionary at:$M put:ms.
  3462     aDictionary at:$y put:year.
  3493     aDictionary at:$y put:year.
  3463     aDictionary at:$Y put:((year \\ 100) printStringLeftPaddedTo:2 with:$0).
  3494     aDictionary at:$Y put:[ (year \\ 100) printStringLeftPaddedTo:2 with:$0 ].
  3464     aDictionary at:#Y1950 put:[ 
  3495 
  3465                                 (year between:1950 and:2049) ifFalse:[ 
  3496     conv := 
  3466                                     self error:'year cannot be represented'
  3497         [ 
  3467                                 ].
  3498             (year between:1950 and:2049) ifFalse:[ 
  3468                                 (year between:1950 and:1999) ifTrue:[
  3499                 self error:'year cannot be represented'
  3469                                     (year - 1900) printStringLeftPaddedTo:2 with:$0
  3500             ].
  3470                                 ] ifFalse:[    
  3501             (year between:1950 and:1999) ifTrue:[
  3471                                     (year - 2000) printStringLeftPaddedTo:2 with:$0
  3502                 (year - 1900) printStringLeftPaddedTo:2 with:$0
  3472                                 ]
  3503             ] ifFalse:[    
  3473                             ].        
  3504                 (year - 2000) printStringLeftPaddedTo:2 with:$0
  3474     aDictionary at:#Y1980 put:[ 
  3505             ]
  3475                                 (year between:1980 and:2079) ifFalse:[ 
  3506         ].        
  3476                                     self error:'year cannot be represented'
  3507     aDictionary at:#Y1950 put:conv. 
  3477                                 ].
  3508     aDictionary at:#y1950 put:conv. 
  3478                                 (year between:1980 and:1999) ifTrue:[
  3509 
  3479                                     (year - 1900) printStringLeftPaddedTo:2 with:$0
  3510     conv := 
  3480                                 ] ifFalse:[    
  3511         [ 
  3481                                     (year - 2000) printStringLeftPaddedTo:2 with:$0
  3512             (year between:1980 and:2079) ifFalse:[ 
  3482                                 ]
  3513                 self error:'year cannot be represented'
  3483                             ].        
  3514             ].
       
  3515             (year between:1980 and:1999) ifTrue:[
       
  3516                 (year - 1900) printStringLeftPaddedTo:2 with:$0
       
  3517             ] ifFalse:[    
       
  3518                 (year - 2000) printStringLeftPaddedTo:2 with:$0
       
  3519             ]
       
  3520         ].        
       
  3521     aDictionary at:#Y1980 put:conv. 
       
  3522     aDictionary at:#y1980 put:conv. 
       
  3523 
       
  3524     conv := 
       
  3525         [ 
       
  3526             (year between:1970 and:2069) ifFalse:[ 
       
  3527                 self error:'year cannot be represented'
       
  3528             ].
       
  3529             (year between:1970 and:1999) ifTrue:[
       
  3530                 (year - 1900) printStringLeftPaddedTo:2 with:$0
       
  3531             ] ifFalse:[    
       
  3532                 (year - 2000) printStringLeftPaddedTo:2 with:$0
       
  3533             ]
       
  3534         ].        
       
  3535     aDictionary at:#Y1970 put:conv. 
       
  3536     aDictionary at:#y1970 put:conv. 
       
  3537 
       
  3538     conv := 
       
  3539         [ 
       
  3540             (year between:2000 and:2099) ifFalse:[ 
       
  3541                 self error:'year cannot be represented'
       
  3542             ].
       
  3543             (year - 2000) printStringLeftPaddedTo:2 with:$0
       
  3544         ].        
       
  3545     aDictionary at:#Y2000 put:conv. 
       
  3546     aDictionary at:#y2000 put:conv. 
       
  3547 
       
  3548     conv := 
       
  3549         [ 
       
  3550             (year between:1900 and:1999) ifFalse:[ 
       
  3551                 self error:'year cannot be represented'
       
  3552             ].
       
  3553             (year - 1900) printStringLeftPaddedTo:2 with:$0
       
  3554         ].        
       
  3555     aDictionary at:#Y1900 put:conv. 
       
  3556     aDictionary at:#y1900 put:conv. 
       
  3557 
  3484     aDictionary at:$w put:wsPadded0.
  3558     aDictionary at:$w put:wsPadded0.
  3485     aDictionary at:$W put:ws.
  3559     aDictionary at:$W put:ws.
  3486 
  3560 
  3487     aDictionary at:#monthName put:monthName.
  3561     aDictionary at:#monthName put:monthName.
  3488     aDictionary at:#monthname put:monthName asLowercase.
  3562     aDictionary at:#monthname put:monthName asLowercase.