l.a.w.t.LocalDateTimeWidget(TextWidget) : class documentation

Part of lp.app.widgets.textwidgets View In Hierarchy

A datetime widget that uses a particular time zone.
Method _toFieldValue Convert a string to a datetime value.
Method _toFormValue Convert a date to its string representation.
def _toFieldValue(self, input):

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', ...)
def _toFormValue(self, 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'
API Documentation for Launchpad, generated by pydoctor at 2022-06-16 00:00:12.