Changeset 8162 for main/waeup.kofa/trunk/src
- Timestamp:
- 15 Apr 2012, 14:04:19 (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
r8158 r8162 20 20 """ 21 21 from datetime import datetime 22 from zope.datetime import parseDatetimetz, DateTimeError 22 23 from zope.formlib.interfaces import ConversionError, IDisplayWidget 23 24 from zope.formlib.textwidgets import ( … … 78 79 context, request, *args, **kw) 79 80 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 as 85 localtime (server time). 86 87 We do this by first turning the data-part of the string (w/o 88 TZ info) into some regular datetime (w/o TZ set), then 89 transforming the datetime into an unambigous string again 90 (this time with TZ info from initial string) and finally 91 turning the unambigous string into a datetime with TZ info set 92 (using zope.datetime.parseDatetimetz). 93 """ 94 # Split date and tz-info. XXX: Very fragile 95 date, tzdata = string[:19], string[19:] 96 # Turn string representation into something non-ambigious 97 # based on local date_format. Make sure we do not confuse 98 # 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-representation 103 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 += tzdata 108 return parseDatetimetz(iso_date) 109 80 110 def _toFieldValue(self, input): 81 111 # In import files we can use the hash symbol at the end of a … … 85 115 if input == self._missing: 86 116 return self.context.missing_value 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) 117 try: 118 value = self._toTZValue(input) 119 except (DateTimeError, ValueError, IndexError), v: 120 raise ConversionError("Invalid datetime data", v) 98 121 return value 99 122 -
main/waeup.kofa/trunk/src/waeup/kofa/widgets/tests/test_datetimewidget.py
r8158 r8162 7 7 from zope.interface.verify import verifyClass 8 8 9 from zope.datetime import parseDatetimetz 9 10 from zope.formlib.tests.test_browserwidget import ( 10 11 SimpleInputWidgetTest, BrowserWidgetTest, ) … … 57 58 def test_getDefaultInputValue(self, 58 59 value=u'2004-03-26 12:10:20', 59 check_value =datetime.datetime(2004, 3, 26, 12, 10, 20)):60 check_value = parseDatetimetz('2004-03-26 12:10:20 GMT')): 60 61 self._widget.request.form['field.foo'] = u'' 61 62 self.assertRaises(WidgetInputError, self._widget.getInputValue)
Note: See TracChangeset for help on using the changeset viewer.