--- a/Timestamp.st Sat Jun 06 06:39:31 2015 +0200
+++ b/Timestamp.st Sun Jun 07 06:38:49 2015 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
"
COPYRIGHT (c) 1989 by Claus Gittinger
All Rights Reserved
@@ -781,6 +783,7 @@
%d - day
%D - day
%(day) - day
+ %(dayOfYear) - 1..365/366
%(month) - month
@@ -792,6 +795,8 @@
72..99 map to 1972..1999;
%Y1900 - year, last 2 digits only, map to 1900..1999
%Y2000 - year, last 2 digits only, map to 2000..2099
+ %Y1950 - year, last 2 digits only, map to 1950..2049
+ %Y1980 - year, last 2 digits only, map to 1980..2079
an optional length after the % gives a field length;
i.e. %2h%2m%2s parses '123557' as 12:35:37
@@ -800,14 +805,14 @@
Please consider using a standard format, such as iso8601.
"
- |day month year
+ |day month year dayOfYear monthAndDay
hour minute second millisecond
utcOffset inStream formatStream error fChar format itemHandler
len now s|
error := [:msg |
exceptionalValue isBlock ifTrue:[
- ^ exceptionalValue valueWithOptionalArgument:'format error; space expcected'
+ ^ exceptionalValue valueWithOptionalArgument:'format error'
] ifFalse:[
^ exceptionalValue value
].
@@ -815,6 +820,7 @@
itemHandler := [:format |
|input|
+
input := len isNil ifTrue:[ inStream ] ifFalse:[ inStream next: len ].
( #('d' 'D' 'day' ) includes:format ) ifTrue:[
@@ -829,22 +835,43 @@
] ifFalse:[ ( format = 'Y' ) ifTrue:[
year := Integer readFrom:input onError:[ error value:'invalid year' ].
(year between:0 and: 99) ifFalse:[ error value:'invalid year' ].
- (year between:0 and:71) ifTrue:[
+ (year < 70) ifTrue:[
+ year := year + 2000
+ ] ifFalse:[
year := year + 1900
- ] ifFalse:[
- year := year + 2000
]
] ifFalse:[ (format = 'monthName') ifTrue:[
s := input nextMatching:[:c | c isLetter] thenMatching:[:c | c isLetter].
month := Date indexOfMonth:s asLowercase language:languageOrNil
- ] ifFalse:[ ( format = 'Y1900' ) ifTrue:[
+ ] ifFalse:[ (format = 'dayOfYear') ifTrue:[
+ dayOfYear := Integer readFrom:input onError:[ error value:'invalid day of year' ].
+
+ ] ifFalse:[ ( format sameAs: 'Y1900' ) ifTrue:[
year := Integer readFrom:input onError:[ error value:'invalid year' ].
(year between:0 and: 99) ifFalse:[ error value:'invalid year' ].
year := year + 1900
- ] ifFalse:[ ( format = 'Y2000' ) ifTrue:[
+ ] ifFalse:[ ( format sameAs: 'Y1950' ) ifTrue:[
+ year := Integer readFrom:input onError:[ error value:'invalid year' ].
+ (year between:0 and: 99) ifFalse:[ error value:'invalid year' ].
+ (year between:0 and: 49) ifTrue:[
+ year := year + 2000
+ ] ifFalse:[
+ year := year + 1900
+ ]
+
+ ] ifFalse:[ ( format sameAs: 'Y1980' ) ifTrue:[
+ year := Integer readFrom:input onError:[ error value:'invalid year' ].
+ (year between:0 and: 99) ifFalse:[ error value:'invalid year' ].
+ (year between:0 and: 79) ifTrue:[
+ year := year + 2000
+ ] ifFalse:[
+ year := year + 1900
+ ]
+
+ ] ifFalse:[ ( format sameAs: 'Y2000' ) ifTrue:[
year := Integer readFrom:input onError:[ error value:'invalid year' ].
(year between:0 and: 99) ifFalse:[ error value:'invalid year' ].
year := year + 2000
@@ -882,8 +909,8 @@
] ifFalse:[
error value:'unhandled format:',format
- ]]]]]]]]]]]]]]
- ].
+ ]]]]]]]]]]]]]]]]]
+ ].
hour := 0.
minute := 0.
@@ -931,14 +958,29 @@
year isNil ifTrue:[
year := (now := Timestamp now) year
].
-
- ^ (self year:year month:month day:day hour:(hour ? 0) minute:(minute ? 0) second:(second ? 0) millisecond:millisecond) + utcOffset
+
+ dayOfYear notNil ifTrue:[
+ monthAndDay := Date monthAndDayFromDayInYear:dayOfYear forYear:year.
+ month := (monthAndDay at:1).
+ day := (monthAndDay at:2).
+ ].
+
+ ^ (self
+ year:year month:month day:day
+ hour:(hour ? 0) minute:(minute ? 0) second:(second ? 0) millisecond:millisecond)
+ + utcOffset
"
Timestamp readFrom:'20-2-1995 13:11:06' format:'%day-%month-%year %h:%m:%s' language:nil onError:[self halt]
Timestamp readFrom:'20021995131106' format:'%2d%2month%4y%2h%2m%2s' language:nil onError:[self halt]
+ Timestamp readFrom:'200295131106' format:'%2d%2month%2y%2h%2m%2s' language:nil onError:[self halt]
+ Timestamp readFrom:'200260131106' format:'%2d%2month%2(y1900)%2h%2m%2s' language:nil onError:[self halt]
+ Timestamp readFrom:'200260131106' format:'%2d%2month%2(y2000)%2h%2m%2s' language:nil onError:[self halt]
+ Timestamp readFrom:'200260131106' format:'%2d%2month%2(y1950)%2h%2m%2s' language:nil onError:[self halt]
+ Timestamp readFrom:'200260131106' format:'%2d%2month%2(y1980)%2h%2m%2s' language:nil onError:[self halt]
Timestamp readFrom:'March 7 2009 7:30pm EST' format:'%monthName %day %year %u:%m%a %tz' language:#en onError:[self halt]
Timestamp readFrom:'March 7 2009 7:30pm UTC' format:'%monthName %day %year %u:%m%a %tz' language:#en onError:[self halt]
+ Timestamp readFrom:'2015103' format:'%4y%3dayOfYear' onError:[self halt]
"
!
@@ -2384,11 +2426,19 @@
super addPrintBindingsTo:dict language:languageOrNil.
date year == Date today year ifTrue:[
- dict at:#yearOrTime put:('%h:%m' expandPlaceholdersWith:dict).
+ dict at:#yearOrTime put:('%h:%m' expandPlaceholdersWith:dict).
].
"
- Timestamp now addPrintBindingsTo:Dictionary new inspect language:nil
+ |d|
+ d := Dictionary new.
+ Timestamp now addPrintBindingsTo:d language:nil.
+ d inspect.
+ "
+
+ "used by:
+ Timestamp now printStringFormat:'%y-%m-%d'
+ Timestamp now printStringFormat:'%(dayOfYear)'
"
!
@@ -3908,11 +3958,11 @@
!Timestamp class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.215 2015-06-05 17:41:37 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.220 2015-06-06 12:57:00 cg Exp $'
!
version_CVS
- ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.215 2015-06-05 17:41:37 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.220 2015-06-06 12:57:00 cg Exp $'
! !