Changeset 8165
- Timestamp:
- 15 Apr 2012, 17:49:59 (13 years ago)
- Location:
- main/waeup.kofa/trunk/src/waeup/kofa/widgets
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/widgets/datetimewidget.py
r8162 r8165 20 20 """ 21 21 from datetime import datetime 22 from zope.datetime import parseDatetimetz, DateTimeError23 22 from zope.formlib.interfaces import ConversionError, IDisplayWidget 24 23 from zope.formlib.textwidgets import ( … … 79 78 context, request, *args, **kw) 80 79 81 def _toTZValue(self, string):82 """Try to turn `string` into a timezone-aware datetime.83 84 If the string provides no TZ info it is interpreted as85 localtime (server time).86 87 We do this by first turning the data-part of the string (w/o88 TZ info) into some regular datetime (w/o TZ set), then89 transforming the datetime into an unambigous string again90 (this time with TZ info from initial string) and finally91 turning the unambigous string into a datetime with TZ info set92 (using zope.datetime.parseDatetimetz).93 """94 # Split date and tz-info. XXX: Very fragile95 date, tzdata = string[:19], string[19:]96 # Turn string representation into something non-ambigious97 # based on local date_format. Make sure we do not confuse98 # mm/dd/yy with dd/mm/yy.99 try:100 value = datetime.strptime(date, self.date_format)101 except (DateTimeError, ValueError, IndexError), v:102 # Fallback: use ISO-representation103 value = datetime.strptime(104 date, FormattedDatetimeWidget.date_format)105 # Create a string unambigous for parseDatetimetz and with TZ info.106 iso_date = value.strftime('%Y-%m-%dT%H:%M:%S')107 iso_date += tzdata108 return parseDatetimetz(iso_date)109 110 80 def _toFieldValue(self, input): 111 81 # In import files we can use the hash symbol at the end of a … … 115 85 if input == self._missing: 116 86 return self.context.missing_value 117 try: 118 value = self._toTZValue(input) 119 except (DateTimeError, ValueError, IndexError), v: 120 raise ConversionError("Invalid datetime data", v) 87 else: 88 try: 89 value = datetime.strptime(input, self.date_format) 90 except (ValueError, IndexError), v: 91 try: 92 # Try ISO format as fallback. 93 # This is needed for instance during imports. 94 value = datetime.strptime( 95 input, FormattedDatetimeWidget.date_format) 96 except (ValueError, IndexError), v: 97 raise ConversionError("Invalid datetime data", v) 121 98 return value 122 99 -
main/waeup.kofa/trunk/src/waeup/kofa/widgets/tests/test_datetimewidget.py
r8162 r8165 7 7 from zope.interface.verify import verifyClass 8 8 9 from zope.datetime import parseDatetimetz10 9 from zope.formlib.tests.test_browserwidget import ( 11 10 SimpleInputWidgetTest, BrowserWidgetTest, ) … … 58 57 def test_getDefaultInputValue(self, 59 58 value=u'2004-03-26 12:10:20', 60 check_value = parseDatetimetz('2004-03-26 12:10:20 GMT')):59 check_value=datetime.datetime(2004, 3, 26, 12, 10, 20)): 61 60 self._widget.request.form['field.foo'] = u'' 62 61 self.assertRaises(WidgetInputError, self._widget.getInputValue)
Note: See TracChangeset for help on using the changeset viewer.