Timestamp.st
branchjv
changeset 18457 214d760f8247
parent 18442 bd42fa983e3f
parent 18455 cba486c3a61a
child 18768 99079a967eb0
equal deleted inserted replaced
18442:bd42fa983e3f 18457:214d760f8247
       
     1 "{ Encoding: utf8 }"
       
     2 
     1 "
     3 "
     2  COPYRIGHT (c) 1989 by Claus Gittinger
     4  COPYRIGHT (c) 1989 by Claus Gittinger
     3 	      All Rights Reserved
     5 	      All Rights Reserved
     4 
     6 
     5  This software is furnished under a license and may be used
     7  This software is furnished under a license and may be used
   779         %a      am/pm
   781         %a      am/pm
   780 
   782 
   781         %d             - day
   783         %d             - day
   782         %D             - day
   784         %D             - day
   783         %(day)         - day
   785         %(day)         - day
       
   786         %(dayOfYear)   - 1..365/366
   784 
   787 
   785         %(month)       - month
   788         %(month)       - month
   786 
   789 
   787         %(monthName)   - monthName
   790         %(monthName)   - monthName
   788 
   791 
   790         %Y             - year, last 2 digits only,
   793         %Y             - year, last 2 digits only,
   791                          0..71 map to 2000..2071;
   794                          0..71 map to 2000..2071;
   792                          72..99 map to 1972..1999;
   795                          72..99 map to 1972..1999;
   793         %Y1900          - year, last 2 digits only, map to 1900..1999
   796         %Y1900          - year, last 2 digits only, map to 1900..1999
   794         %Y2000          - year, last 2 digits only, map to 2000..2099
   797         %Y2000          - year, last 2 digits only, map to 2000..2099
       
   798         %Y1950          - year, last 2 digits only, map to 1950..2049
       
   799         %Y1980          - year, last 2 digits only, map to 1980..2079
   795 
   800 
   796      an optional length after the % gives a field length;
   801      an optional length after the % gives a field length;
   797         i.e. %2h%2m%2s parses '123557' as 12:35:37
   802         i.e. %2h%2m%2s parses '123557' as 12:35:37
   798         and '%4(year)%2(month)%2(day)' parses '20060620' as 2006-06-20 00:00:00
   803         and '%4(year)%2(month)%2(day)' parses '20060620' as 2006-06-20 00:00:00
   799 
   804 
   800      Please consider using a standard format, such as iso8601.
   805      Please consider using a standard format, such as iso8601.
   801     "
   806     "
   802 
   807 
   803     |day month year
   808     |day month year dayOfYear monthAndDay
   804      hour minute second millisecond
   809      hour minute second millisecond
   805      utcOffset inStream formatStream error fChar format itemHandler
   810      utcOffset inStream formatStream error fChar format itemHandler
   806      len now s|
   811      len now s|
   807 
   812 
   808     error := [:msg |
   813     error := [:msg |
   809                 exceptionalValue isBlock ifTrue:[
   814                 exceptionalValue isBlock ifTrue:[
   810                     ^ exceptionalValue valueWithOptionalArgument:'format error; space expcected'
   815                     ^ exceptionalValue valueWithOptionalArgument:'format error'
   811                 ] ifFalse:[
   816                 ] ifFalse:[
   812                     ^ exceptionalValue value
   817                     ^ exceptionalValue value
   813                 ].
   818                 ].
   814              ].
   819              ].
   815 
   820 
   816     itemHandler := [:format |
   821     itemHandler := [:format |
   817         |input|
   822         |input|
       
   823         
   818         input := len isNil ifTrue:[ inStream ] ifFalse:[ inStream next: len ].
   824         input := len isNil ifTrue:[ inStream ] ifFalse:[ inStream next: len ].
   819 
   825 
   820         ( #('d' 'D' 'day' ) includes:format ) ifTrue:[
   826         ( #('d' 'D' 'day' ) includes:format ) ifTrue:[
   821             day := Integer readFrom:input onError:[ error value:'invalid day' ].
   827             day := Integer readFrom:input onError:[ error value:'invalid day' ].
   822 
   828 
   827             year := Integer readFrom:input onError:[ error value:'invalid year' ].
   833             year := Integer readFrom:input onError:[ error value:'invalid year' ].
   828 
   834 
   829         ] ifFalse:[ ( format = 'Y' ) ifTrue:[
   835         ] ifFalse:[ ( format = 'Y' ) ifTrue:[
   830             year := Integer readFrom:input onError:[ error value:'invalid year' ].
   836             year := Integer readFrom:input onError:[ error value:'invalid year' ].
   831             (year between:0 and: 99) ifFalse:[ error value:'invalid year' ].
   837             (year between:0 and: 99) ifFalse:[ error value:'invalid year' ].
   832             (year between:0 and:71) ifTrue:[
   838             (year < 70) ifTrue:[
       
   839                 year := year + 2000
       
   840             ] ifFalse:[
   833                 year := year + 1900
   841                 year := year + 1900
   834             ] ifFalse:[
       
   835                 year := year + 2000
       
   836             ]
   842             ]
   837 
   843 
   838         ] ifFalse:[ (format = 'monthName') ifTrue:[
   844         ] ifFalse:[ (format = 'monthName') ifTrue:[
   839             s := input nextMatching:[:c | c isLetter] thenMatching:[:c | c isLetter].
   845             s := input nextMatching:[:c | c isLetter] thenMatching:[:c | c isLetter].
   840             month := Date indexOfMonth:s asLowercase language:languageOrNil
   846             month := Date indexOfMonth:s asLowercase language:languageOrNil
   841 
   847 
   842         ] ifFalse:[ ( format = 'Y1900' ) ifTrue:[
   848         ] ifFalse:[ (format = 'dayOfYear') ifTrue:[
       
   849             dayOfYear := Integer readFrom:input onError:[ error value:'invalid day of year' ].
       
   850 
       
   851         ] ifFalse:[ ( format sameAs: 'Y1900' ) ifTrue:[
   843             year := Integer readFrom:input onError:[ error value:'invalid year' ].
   852             year := Integer readFrom:input onError:[ error value:'invalid year' ].
   844             (year between:0 and: 99) ifFalse:[ error value:'invalid year' ].
   853             (year between:0 and: 99) ifFalse:[ error value:'invalid year' ].
   845             year := year + 1900
   854             year := year + 1900
   846 
   855 
   847         ] ifFalse:[ ( format = 'Y2000' ) ifTrue:[
   856         ] ifFalse:[ ( format sameAs:  'Y1950' ) ifTrue:[
       
   857             year := Integer readFrom:input onError:[ error value:'invalid year' ].
       
   858             (year between:0 and: 99) ifFalse:[ error value:'invalid year' ].
       
   859             (year between:0 and: 49) ifTrue:[ 
       
   860                 year := year + 2000
       
   861             ] ifFalse:[    
       
   862                 year := year + 1900
       
   863             ]  
       
   864 
       
   865         ] ifFalse:[ ( format sameAs:  'Y1980' ) ifTrue:[
       
   866             year := Integer readFrom:input onError:[ error value:'invalid year' ].
       
   867             (year between:0 and: 99) ifFalse:[ error value:'invalid year' ].
       
   868             (year between:0 and: 79) ifTrue:[ 
       
   869                 year := year + 2000
       
   870             ] ifFalse:[    
       
   871                 year := year + 1900
       
   872             ]  
       
   873 
       
   874         ] ifFalse:[ ( format sameAs:  'Y2000' ) ifTrue:[
   848             year := Integer readFrom:input onError:[ error value:'invalid year' ].
   875             year := Integer readFrom:input onError:[ error value:'invalid year' ].
   849             (year between:0 and: 99) ifFalse:[ error value:'invalid year' ].
   876             (year between:0 and: 99) ifFalse:[ error value:'invalid year' ].
   850             year := year + 2000
   877             year := year + 2000
   851 
   878 
   852         ] ifFalse:[ ( format = 'h' or:[ format = 'H' ]) ifTrue:[
   879         ] ifFalse:[ ( format = 'h' or:[ format = 'H' ]) ifTrue:[
   880                 ]
   907                 ]
   881             ]
   908             ]
   882 
   909 
   883         ] ifFalse:[
   910         ] ifFalse:[
   884             error value:'unhandled format:',format
   911             error value:'unhandled format:',format
   885         ]]]]]]]]]]]]]]
   912         ]]]]]]]]]]]]]]]]]
   886    ].
   913     ].
   887 
   914 
   888     hour := 0.
   915     hour := 0.
   889     minute := 0.
   916     minute := 0.
   890     second := 0.
   917     second := 0.
   891     millisecond := 0.
   918     millisecond := 0.
   929     ].
   956     ].
   930 
   957 
   931     year isNil ifTrue:[
   958     year isNil ifTrue:[
   932         year := (now := Timestamp now) year
   959         year := (now := Timestamp now) year
   933     ].
   960     ].
   934 
   961     
   935     ^ (self year:year month:month day:day hour:(hour ? 0) minute:(minute ? 0) second:(second ? 0) millisecond:millisecond) + utcOffset
   962     dayOfYear notNil ifTrue:[
       
   963         monthAndDay := Date monthAndDayFromDayInYear:dayOfYear forYear:year.
       
   964         month := (monthAndDay at:1).
       
   965         day := (monthAndDay at:2).  
       
   966     ].
       
   967     
       
   968     ^ (self 
       
   969         year:year month:month day:day 
       
   970         hour:(hour ? 0) minute:(minute ? 0) second:(second ? 0) millisecond:millisecond) 
       
   971             + utcOffset
   936 
   972 
   937     "
   973     "
   938      Timestamp readFrom:'20-2-1995 13:11:06' format:'%day-%month-%year %h:%m:%s' language:nil onError:[self halt]
   974      Timestamp readFrom:'20-2-1995 13:11:06' format:'%day-%month-%year %h:%m:%s' language:nil onError:[self halt]
   939      Timestamp readFrom:'20021995131106' format:'%2d%2month%4y%2h%2m%2s' language:nil onError:[self halt]
   975      Timestamp readFrom:'20021995131106' format:'%2d%2month%4y%2h%2m%2s' language:nil onError:[self halt]
       
   976      Timestamp readFrom:'200295131106' format:'%2d%2month%2y%2h%2m%2s' language:nil onError:[self halt]
       
   977      Timestamp readFrom:'200260131106' format:'%2d%2month%2(y1900)%2h%2m%2s' language:nil onError:[self halt]
       
   978      Timestamp readFrom:'200260131106' format:'%2d%2month%2(y2000)%2h%2m%2s' language:nil onError:[self halt]
       
   979      Timestamp readFrom:'200260131106' format:'%2d%2month%2(y1950)%2h%2m%2s' language:nil onError:[self halt]
       
   980      Timestamp readFrom:'200260131106' format:'%2d%2month%2(y1980)%2h%2m%2s' language:nil onError:[self halt]
   940      Timestamp readFrom:'March 7 2009 7:30pm EST' format:'%monthName %day %year %u:%m%a %tz' language:#en onError:[self halt]
   981      Timestamp readFrom:'March 7 2009 7:30pm EST' format:'%monthName %day %year %u:%m%a %tz' language:#en onError:[self halt]
   941      Timestamp readFrom:'March 7 2009 7:30pm UTC' format:'%monthName %day %year %u:%m%a %tz' language:#en onError:[self halt]
   982      Timestamp readFrom:'March 7 2009 7:30pm UTC' format:'%monthName %day %year %u:%m%a %tz' language:#en onError:[self halt]
       
   983      Timestamp readFrom:'2015103' format:'%4y%3dayOfYear' onError:[self halt]
   942     "
   984     "
   943 !
   985 !
   944 
   986 
   945 readFrom:aStringOrStream onError:exceptionBlock
   987 readFrom:aStringOrStream onError:exceptionBlock
   946     "return a new Timestamp, reading a printed representation from aStream.
   988     "return a new Timestamp, reading a printed representation from aStream.
  2382     date := self asDate.
  2424     date := self asDate.
  2383     date addPrintBindingsTo:dict language:languageOrNil.
  2425     date addPrintBindingsTo:dict language:languageOrNil.
  2384     super addPrintBindingsTo:dict language:languageOrNil.
  2426     super addPrintBindingsTo:dict language:languageOrNil.
  2385 
  2427 
  2386     date year == Date today year ifTrue:[
  2428     date year == Date today year ifTrue:[
  2387 	dict at:#yearOrTime put:('%h:%m' expandPlaceholdersWith:dict).
  2429         dict at:#yearOrTime put:('%h:%m' expandPlaceholdersWith:dict).
  2388     ].
  2430     ].
  2389 
  2431 
  2390     "
  2432     "
  2391 	Timestamp now addPrintBindingsTo:Dictionary new inspect language:nil
  2433      |d|
       
  2434      d := Dictionary new.
       
  2435      Timestamp now addPrintBindingsTo:d language:nil.
       
  2436      d inspect.
       
  2437     "
       
  2438     
       
  2439     "used by:
       
  2440         Timestamp now printStringFormat:'%y-%m-%d'
       
  2441         Timestamp now printStringFormat:'%(dayOfYear)'
  2392     "
  2442     "
  2393 !
  2443 !
  2394 
  2444 
  2395 printGeneralizedOn:aStream
  2445 printGeneralizedOn:aStream
  2396     "append a representation of the receiver to aStream in a general format,
  2446     "append a representation of the receiver to aStream in a general format,
  3906 ! !
  3956 ! !
  3907 
  3957 
  3908 !Timestamp class methodsFor:'documentation'!
  3958 !Timestamp class methodsFor:'documentation'!
  3909 
  3959 
  3910 version
  3960 version
  3911     ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.215 2015-06-05 17:41:37 cg Exp $'
  3961     ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.220 2015-06-06 12:57:00 cg Exp $'
  3912 !
  3962 !
  3913 
  3963 
  3914 version_CVS
  3964 version_CVS
  3915     ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.215 2015-06-05 17:41:37 cg Exp $'
  3965     ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.220 2015-06-06 12:57:00 cg Exp $'
  3916 ! !
  3966 ! !
  3917 
  3967 
  3918 
  3968 
  3919 Timestamp initialize!
  3969 Timestamp initialize!