Typeconverters for #time #timeOrNil #timestamp #timestampOrNil
authorStefan Vogel <sv@exept.de>
Sun, 09 Mar 2008 23:06:15 +0100
changeset 2401 6ce0d64635e0
parent 2400 b47e642dd9bc
child 2402 cf6437ff07d2
Typeconverters for #time #timeOrNil #timestamp #timestampOrNil
TypeConverter.st
--- a/TypeConverter.st	Sat Mar 08 15:20:20 2008 +0100
+++ b/TypeConverter.st	Sun Mar 09 23:06:15 2008 +0100
@@ -385,6 +385,14 @@
     "Created: / 26.10.1997 / 14:01:02 / cg"
 !
 
+date
+    "setup the converter to convert from a string to a date
+     and vice versa. Nil is converted to todays date-string,
+     likewise, an empty string is converted back to todays date."
+
+    ^ self dateWithFormat:nil orDefault:Date today
+!
+
 dateDDMMYYYY
     "setup the converter to convert from a string to a date formatted by printFormat
      DD-MM-YYYY; see also Date>>printFormat:"
@@ -1304,7 +1312,7 @@
                         ].
                         value := s asSymbol
                     ] ifFalse:[
-                        (#('true' 'false') includes:s) ifTrue:[
+                        (#(true false) includes:s) ifTrue:[
                             value := Object readFromString:s.
                         ] ifFalse:[
                             value := s asSymbol
@@ -1359,6 +1367,10 @@
     "Modified: / 26.5.1998 / 15:06:06 / cg"
 !
 
+time
+     ^ self time24H
+!
+
 time12H
     "setup the converter to convert from a string to a time formatted by 12 hours
      and vice versa."
@@ -1377,6 +1389,47 @@
 
 !
 
+timeOfClass:timeClass withFormat:aFormatString orDefault:defaultValue
+    "setup the converter to convert from a string to a time
+     and vice versa. TimeClass is the class that should be used (e.g. Time or Timestamp). 
+     Invalid times are converted to defaultValue; likewise,
+     a nil time is converted to an empty string."
+
+    self
+        getBlock:[:model |
+                |time|
+
+                (time := model value) isNil ifTrue:[
+                    time := defaultValue
+                ].
+                time isNil ifTrue:[
+                    ''
+                ] ifFalse:[
+                    aFormatString isNil ifTrue:[
+                        time printString
+                    ] ifFalse:[
+                        time printStringFormat:aFormatString
+                ]]]
+
+        putBlock:
+                [:model :string |
+
+                |value|
+
+                string isEmpty ifTrue:[
+                    value := defaultValue
+                ] ifFalse:[
+"/                    aFormatString isNil ifTrue:[
+                        value := timeClass readFrom:string onError:defaultValue
+"/                    ] ifFalse:[
+"/                        value := timeClass readFrom:string printFormat:aFormatString onError:defaultValue
+"/                    ]
+                ].
+                model value:value]
+
+        updateBlock: [:m :a :p | true]
+!
+
 timeOrNil
     "setup the converter to convert from a string to a time and vice versa."
 
@@ -1387,11 +1440,10 @@
 !
 
 timeStampOrNil
+    <resource: #obsolete>
     "setup the converter to convert from a string to a timeStamp and vice versa."
 
-    self objectOrNilOfType:Timestamp
-
-    "Modified: / 12-01-2008 / 19:06:06 / cg"
+    self timestampOrNil
 !
 
 timeToText
@@ -1431,10 +1483,24 @@
 
     "Modified: 21.2.1997 / 18:59:06 / cg"
     "Created: 4.3.1997 / 11:52:47 / cg"
+!
+
+timestamp
+    "setup the converter to convert from a string to a timestamp and vice versa."
+
+    ^ self timeOfClass:Timestamp withFormat:nil orDefault:Timestamp now
+!
+
+timestampOrNil
+    "setup the converter to convert from a string to a timestamp and vice versa."
+
+    self objectOrNilOfType:Timestamp
+
+    "Modified: / 12-01-2008 / 19:06:06 / cg"
 ! !
 
 !TypeConverter class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.51 2008-02-04 09:33:03 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.52 2008-03-09 22:06:15 stefan Exp $'
 ! !