Part of lp.app.widgets.textwidgets View In Hierarchy
Method | _toFieldValue | Convert a string to a datetime value. |
Method | _toFormValue | Convert a date to its string representation. |
Convert a string to a datetime value.
>>> from zope.publisher.browser import TestRequest >>> from zope.schema import Field >>> field = Field(__name__='foo', title=u'Foo') >>> widget = LocalDateTimeWidget(field, TestRequest())
The widget converts an empty string to the missing value:
>>> widget._toFieldValue('') == field.missing_value True
By default, the date is interpreted as UTC:
>>> print widget._toFieldValue('2006-01-01 12:00:00') 2006-01-01 12:00:00+00:00
But it will handle other time zones:
>>> widget.timeZoneName = 'Australia/Perth' >>> print widget._toFieldValue('2006-01-01 12:00:00') 2006-01-01 12:00:00+08:00
Invalid dates result in a ConversionError:
>>> print widget._toFieldValue('not a date') #doctest: +ELLIPSIS Traceback (most recent call last): ... ConversionError: ('Invalid date value', ...)
Convert a date to its string representation.
>>> from zope.publisher.browser import TestRequest >>> from zope.schema import Field >>> field = Field(__name__='foo', title=u'Foo') >>> widget = LocalDateTimeWidget(field, TestRequest())
The 'missing' value is converted to an empty string:
>>> widget._toFormValue(field.missing_value) u''
Dates are displayed without an associated time zone:
>>> dt = datetime.datetime(2006, 1, 1, 12, 0, 0, ... tzinfo=pytz.timezone('UTC')) >>> widget._toFormValue(dt) '2006-01-01 12:00:00'
The date value will be converted to the widget's time zone before being displayed:
>>> widget.timeZoneName = 'Australia/Perth' >>> widget._toFormValue(dt) '2006-01-01 20:00:00'