Datetime¶
Support for reading, writing and converting date and time related objects.
See the datetime
module, as well as the
zope.interface.common.idatetime
module for types of objects.
These are generally meant to be used as zope.interface adapters once this package has been configured, but they can be called manually as well.
-
class
date_to_string
(date)[source]¶ Bases:
object
Produce an IOS8601 string from a date.
Registered as an adapter from
zope.interface.common.idatetime.IDate
toIInternalObjectExternalizer
.>>> import datetime >>> from nti.externalization.externalization import to_external_object >>> from nti.externalization.datetime import date_to_string >>> from zope import component >>> component.provideAdapter(date_to_string) >>> to_external_object(datetime.date(1982, 1, 31)) '1982-01-31'
-
date_from_string
(string)[source]¶ This adapter allows any field which comes in as a string in IOS8601 format to be transformed into a date. The schema field must be an
zope.schema.Object
field with a type ofzope.interface.common.idatetime.IDate
.If you need a schema field that accepts human input, rather than programattic input, you probably want to use a custom field that uses
zope.datetime.parse()
in itsfromUnicode
method.>>> from nti.externalization.datetime import date_from_string >>> date_from_string('1982-01-31') datetime.date(1982, 1, 31)
-
class
datetime_to_string
(date)[source]¶ Bases:
object
Produce an IOS8601 string from a datetime.
Registered as an adapter from
zope.interface.common.idatetime.IDateTime
toIInternalObjectExternalizer
.>>> from zope.component import provideAdapter >>> import datetime >>> from nti.externalization import to_external_object >>> from nti.externalization.datetime import datetime_to_string >>> provideAdapter(datetime_to_string) >>> to_external_object(datetime.datetime(1982, 1, 31)) '1982-01-31T00:00:00Z'
-
datetime_from_string
(string, assume_local=False, local_tzname=None)[source]¶ This adapter allows any field which comes in as a string in IOS8601 format to be transformed into a
datetime.datetime
. The schema field should be annti.schema.field.Object
field with a type ofzope.interface.common.idatetime.IDateTime
or an instance ofnti.schema.field.ValidDateTime
. Wrap this with annti.schema.fieldproperty.AdaptingFieldProperty
.Datetime values produced by this object will always be in GMT/UTC time, and they will always be datetime naive objects.
If you need a schema field that accepts human input, rather than programattic input, you probably want to use a custom field that uses
zope.datetime.parse()
in itsfromUnicode
method.When used as an adapter, no parameters are accepted.
>>> from zope.interface.common.idatetime import IDateTime >>> from zope.component import provideAdapter >>> from nti.externalization.datetime import datetime_from_string >>> provideAdapter(datetime_from_string) >>> IDateTime('1982-01-31T00:00:00Z') datetime.datetime(1982, 1, 31, 0, 0)
Parameters: - assume_local (bool) – If
False
, the default, then when we parse a string that does not include timezone information, we will assume that it is already meant to be in UTC. Otherwise, if set to true, when we parse such a string we will assume that it is meant to be in the “local” timezone and adjust accordingly. If the local timezone experiences DST, then the time will be interpreted with the UTC offset as-of the DST rule in effect on the date parsed, not the current date, if possible. If not possible, the current rule will be used. - local_tzname (str) – If given, either a string acceptable to
pytz.timezone()
to produce atzinfo
object, or a two-tuple as given fromtime.timezone
. If not given, local timezone will be determined automatically.
- assume_local (bool) – If
-
datetime_from_timestamp
(value)[source]¶ Produce a
datetime.datetime
from a UTC timestamp.This is a registered adapter for both integers and floats.
>>> from zope.interface.common.idatetime import IDateTime >>> from zope.component import provideAdapter >>> from nti.externalization.datetime import datetime_from_timestamp >>> provideAdapter(datetime_from_timestamp, (int,)) >>> provideAdapter(datetime_from_timestamp, (float,)) >>> IDateTime(123456) datetime.datetime(1970, 1, 2, 10, 17, 36) >>> IDateTime(654321.0) datetime.datetime(1970, 1, 8, 13, 45, 21)
-
class
duration_to_string
(date)[source]¶ Bases:
object
Produce an IOS8601 format duration from a
datetime.timedelta
object.Timedelta objects do not represent years or months (the biggest duration they accept is weeks) and internally they normalize everything to days and smaller. Thus, the format produced by this transformation will never have a field larger than days.
Registered as an adapter from
zope.interface.common.idatetime.ITimeDelta
toIInternalObjectExternalizer
.>>> import datetime >>> from zope.component import provideAdapter >>> from nti.externalization import to_external_object >>> from nti.externalization.datetime import duration_to_string >>> provideAdapter(duration_to_string) >>> to_external_object(datetime.timedelta(weeks=16)) 'P112D'
-
duration_from_string
(value)[source]¶ Produce a
datetime.timedelta
from a ISO8601 format duration string.>>> from zope.interface.common.idatetime import ITimeDelta >>> from zope.component import provideAdapter >>> from nti.externalization.datetime import duration_from_string >>> provideAdapter(duration_from_string) >>> ITimeDelta('P0D') datetime.timedelta(0)