Commit d91dc623 authored by R David Murray's avatar R David Murray

Merge #14766: Add correct algorithm for when a 'time' object is naive.

This patch also clarifies the definition of Naive and Aware.

Original patch by Greg Weller, I modified the first hunk
somewhat to make the exposition even clearer (I hope).
parents a79e7fed 539f239e
...@@ -12,28 +12,34 @@ ...@@ -12,28 +12,34 @@
The :mod:`datetime` module supplies classes for manipulating dates and times in The :mod:`datetime` module supplies classes for manipulating dates and times in
both simple and complex ways. While date and time arithmetic is supported, the both simple and complex ways. While date and time arithmetic is supported, the
focus of the implementation is on efficient attribute extraction for output focus of the implementation is on efficient attribute extraction for output
formatting and manipulation. For related formatting and manipulation. For related functionality, see also the
functionality, see also the :mod:`time` and :mod:`calendar` modules. :mod:`time` and :mod:`calendar` modules.
There are two kinds of date and time objects: "naive" and "aware". This There are two kinds of date and time objects: "naive" and "aware".
distinction refers to whether the object has any notion of time zone, daylight
saving time, or other kind of algorithmic or political time adjustment. Whether An aware object has sufficient knowledge of applicable algorithmic and
a naive :class:`.datetime` object represents Coordinated Universal Time (UTC), political time adjustments, such as time zone and daylight saving time
local time, or time in some other timezone is purely up to the program, just information, to locate itself relative to other aware objects. An aware object
like it's up to the program whether a particular number represents metres, is used to represent a specific moment in time that is not open to
miles, or mass. Naive :class:`.datetime` objects are easy to understand and to interpretation [#]_.
work with, at the cost of ignoring some aspects of reality.
A naive object does not contain enough information to unambiguously locate
For applications requiring more, :class:`.datetime` and :class:`.time` objects itself relative to other date/time objects. Whether a naive object represents
have an optional time zone information attribute, :attr:`tzinfo`, that can be Coordinated Universal Time (UTC), local time, or time in some other timezone is
set to an instance of a subclass of the abstract :class:`tzinfo` class. These purely up to the program, just like it is up to the program whether a
:class:`tzinfo` objects capture information about the offset from UTC time, the particular number represents metres, miles, or mass. Naive objects are easy to
time zone name, and whether Daylight Saving Time is in effect. Note that only understand and to work with, at the cost of ignoring some aspects of reality.
one concrete :class:`tzinfo` class, the :class:`timezone` class, is supplied by the
:mod:`datetime` module. The :class:`timezone` class can represent simple For applications requiring aware objects, :class:`.datetime` and :class:`.time`
timezones with fixed offset from UTC such as UTC itself or North American EST and objects have an optional time zone information attribute, :attr:`tzinfo`, that
EDT timezones. Supporting timezones at whatever level of detail is can be set to an instance of a subclass of the abstract :class:`tzinfo` class.
required is up to the application. The rules for time adjustment across the These :class:`tzinfo` objects capture information about the offset from UTC
time, the time zone name, and whether Daylight Saving Time is in effect. Note
that only one concrete :class:`tzinfo` class, the :class:`timezone` class, is
supplied by the :mod:`datetime` module. The :class:`timezone` class can
represent simple timezones with fixed offset from UTC, such as UTC itself or
North American EST and EDT timezones. Supporting timezones at deeper levels of
detail is up to the application. The rules for time adjustment across the
world are more political than rational, change frequently, and there is no world are more political than rational, change frequently, and there is no
standard suitable for every application aside from UTC. standard suitable for every application aside from UTC.
...@@ -114,10 +120,13 @@ Objects of these types are immutable. ...@@ -114,10 +120,13 @@ Objects of these types are immutable.
Objects of the :class:`date` type are always naive. Objects of the :class:`date` type are always naive.
An object *d* of type :class:`.time` or :class:`.datetime` may be naive or aware. An object of type :class:`.time` or :class:`.datetime` may be naive or aware.
*d* is aware if ``d.tzinfo`` is not ``None`` and ``d.tzinfo.utcoffset(d)`` does A :class:`.datetime` object *d* is aware if ``d.tzinfo`` is not ``None`` and
not return ``None``. If ``d.tzinfo`` is ``None``, or if ``d.tzinfo`` is not ``d.tzinfo.utcoffset(d)`` does not return ``None``. If ``d.tzinfo`` is
``None`` but ``d.tzinfo.utcoffset(d)`` returns ``None``, *d* is naive. ``None``, or if ``d.tzinfo`` is not ``None`` but ``d.tzinfo.utcoffset(d)``
returns ``None``, *d* is naive. A :class:`.time` object *t* is aware
if ``t.tzinfo`` is not ``None`` and ``t.tzinfo.utcoffset(None)`` does not return
``None``. Otherwise, *t* is naive.
The distinction between naive and aware doesn't apply to :class:`timedelta` The distinction between naive and aware doesn't apply to :class:`timedelta`
objects. objects.
...@@ -1846,3 +1855,7 @@ Notes: ...@@ -1846,3 +1855,7 @@ Notes:
When the ``%z`` directive is provided to the :meth:`strptime` method, an When the ``%z`` directive is provided to the :meth:`strptime` method, an
aware :class:`.datetime` object will be produced. The ``tzinfo`` of the aware :class:`.datetime` object will be produced. The ``tzinfo`` of the
result will be set to a :class:`timezone` instance. result will be set to a :class:`timezone` instance.
.. rubric:: Footnotes
.. [#] If, that is, we ignore the effects of Relativity
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment