date conversion cleanup.
--- a/TypeConverter.st Wed Oct 08 16:10:08 2003 +0200
+++ b/TypeConverter.st Thu Oct 09 21:52:59 2003 +0200
@@ -393,32 +393,20 @@
and vice versa. Invalid dates are converted to nil; likewise,
a nil date is converted to an empty string."
- self
- getBlock:[:model |
- |date|
-
- (date := model value) isNil ifTrue:[
- ''
- ] ifFalse:[
- date printString
- ]]
-
- putBlock:
- [:model :string |
+ ^ self dateWithFormat:nil orDefault:nil
+!
- |value|
+dateOrNilWithFormat:aFormatString
+ "setup the converter to convert from a string to a date
+ and vice versa. Invalid dates are converted to nil; likewise,
+ a nil date is converted to an empty string.
+ The format string must have one of the forms:
+ 'mm/dd/yyyy' (for VW-backward compatibility)
+ or:
+ '%m %d %y'
+ "
- string isEmpty ifTrue:[
- value := nil
- ] ifFalse:[
- value := Date readFrom:string onError:nil
- ].
- model value:value]
-
- updateBlock: [:m :a :p | true]
-
- "Created: / 4.3.1997 / 11:56:36 / cg"
- "Modified: / 26.10.1997 / 13:51:06 / cg"
+ ^ self dateWithFormat:aFormatString orDefault:nil
!
dateToText
@@ -426,32 +414,7 @@
and vice versa. Nil is converted to todays date-string,
likewise, an empty string is converted back to todays date."
- self
- getBlock:[:model |
- |date|
-
- (date := model value) isNil ifTrue:[
- Date today printString
- ] ifFalse:[
- date printString
- ]]
-
- putBlock:
- [:model :string |
-
- |value|
-
- string isEmpty ifTrue:[
- value := Date today
- ] ifFalse:[
- value := Date readFrom:string onError:Date today
- ].
- model value:value]
-
- updateBlock: [:m :a :p | true]
-
- "Created: / 4.3.1997 / 12:32:19 / cg"
- "Modified: / 26.10.1997 / 13:52:00 / cg"
+ ^ self dateWithFormat:nil orDefault:Date today
!
dateToTextFormattedBy: printFormat
@@ -460,26 +423,107 @@
likewise, an empty string is converted back to todays date.
Uses the old, obsolete ST80 printFormat (see Date printFormat:)"
+ ^ self dateWithFormat:printFormat orDefault:Date today
+!
+
+dateWithFormat:aFormatString
+ "setup the converter to convert from a string to a date
+ and vice versa. Invalid dates are converted to nil; likewise,
+ a nil date is converted to an empty string.
+ The format string must have one of the forms:
+ 'mm/dd/yyyy' (for VW-backward compatibility)
+ or:
+ '%m %d %y'
+ "
+
+ ^ self dateWithFormat:aFormatString orDefault:Date today
+!
+
+dateWithFormat:aFormatString orDefault:defaultValue
+ "setup the converter to convert from a string to a date
+ and vice versa. Invalid dates are converted to nil; likewise,
+ a nil date is converted to an empty string.
+ The format string must have one of the forms:
+ 'mm/dd/yyyy' (for VW-backward compatibility)
+ or:
+ '%m %d %y'
+ "
+
+ |in out stxFormat c nDigits|
+
+ aFormatString notNil ifTrue:[
+ (aFormatString includes:$%) ifTrue:[
+ "/ a new (ST/X) formatString
+ stxFormat := aFormatString
+ ] ifFalse:[
+ "/ an old (VW) formatString
+ out := '' writeStream.
+ in := aFormatString readStream.
+ [in atEnd] whileFalse:[
+ c := in next.
+ nDigits := 1.
+ c == $m ifTrue:[
+ [in peek == $m] whileTrue:[ nDigits := nDigits + 1. in next ].
+ nDigits == 1 ifTrue:[
+ out nextPutAll:'%M'. "/ unpadded
+ ] ifFalse:[
+ out nextPutAll:'%m'.
+ ].
+ ] ifFalse:[
+ c == $d ifTrue:[
+ [in peek == $d] whileTrue:[ nDigits := nDigits + 1. in next ].
+ nDigits == 1 ifTrue:[
+ out nextPutAll:'%D'. "/ unpadded
+ ] ifFalse:[
+ out nextPutAll:'%d'.
+ ].
+ ] ifFalse:[
+ c == $y ifTrue:[
+ [in peek == $y] whileTrue:[ nDigits := nDigits + 1. in next ].
+ nDigits == 2 ifTrue:[
+ out nextPutAll:'%Y'. "/ 2 digits only
+ ] ifFalse:[
+ out nextPutAll:'%y'.
+ ].
+ ] ifFalse:[
+ out nextPut:c.
+ ].
+ ].
+ ].
+ ].
+ stxFormat := out contents.
+ ].
+ ].
+
self
getBlock:[:model |
- |date|
+ |date|
- date := model value.
- date isNil ifTrue:[
- date := Date today.
- ].
- date printFormat:printFormat
- ]
+ (date := model value) isNil ifTrue:[
+ date := defaultValue
+ ].
+ date isNil ifTrue:[
+ ''
+ ] ifFalse:[
+ stxFormat isNil ifTrue:[
+ date printString
+ ] ifFalse:[
+ date printStringFormat:stxFormat
+ ]]]
putBlock:
[:model :string |
|value|
- string isEmpty ifTrue:[
- value := Date today
- ] ifFalse:[
- value := Date readFrom:string printFormat:printFormat onError:[Date today]
+ string isEmpty ifTrue:[
+ value := defaultValue
+ ] ifFalse:[
+ stxFormat isNil ifTrue:[
+ value := Date readFrom:string onError:defaultValue
+ ] ifFalse:[
+ value := Date readFrom:string printFormat:stxFormat onError:defaultValue
+ ]
].
model value:value]
@@ -1272,5 +1316,5 @@
!TypeConverter class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.45 2003-08-19 10:02:05 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.46 2003-10-09 19:52:59 cg Exp $'
! !