Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d91dc62

Browse files
committed
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).
2 parents a79e7fe + 539f239 commit d91dc62

1 file changed

Lines changed: 39 additions & 26 deletions

File tree

Doc/library/datetime.rst

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,34 @@
1212
The :mod:`datetime` module supplies classes for manipulating dates and times in
1313
both simple and complex ways. While date and time arithmetic is supported, the
1414
focus of the implementation is on efficient attribute extraction for output
15-
formatting and manipulation. For related
16-
functionality, see also the :mod:`time` and :mod:`calendar` modules.
17-
18-
There are two kinds of date and time objects: "naive" and "aware". This
19-
distinction refers to whether the object has any notion of time zone, daylight
20-
saving time, or other kind of algorithmic or political time adjustment. Whether
21-
a naive :class:`.datetime` object represents Coordinated Universal Time (UTC),
22-
local time, or time in some other timezone is purely up to the program, just
23-
like it's up to the program whether a particular number represents metres,
24-
miles, or mass. Naive :class:`.datetime` objects are easy to understand and to
25-
work with, at the cost of ignoring some aspects of reality.
26-
27-
For applications requiring more, :class:`.datetime` and :class:`.time` objects
28-
have an optional time zone information attribute, :attr:`tzinfo`, that can be
29-
set to an instance of a subclass of the abstract :class:`tzinfo` class. These
30-
:class:`tzinfo` objects capture information about the offset from UTC time, the
31-
time zone name, and whether Daylight Saving Time is in effect. Note that only
32-
one concrete :class:`tzinfo` class, the :class:`timezone` class, is supplied by the
33-
:mod:`datetime` module. The :class:`timezone` class can represent simple
34-
timezones with fixed offset from UTC such as UTC itself or North American EST and
35-
EDT timezones. Supporting timezones at whatever level of detail is
36-
required is up to the application. The rules for time adjustment across the
15+
formatting and manipulation. For related functionality, see also the
16+
:mod:`time` and :mod:`calendar` modules.
17+
18+
There are two kinds of date and time objects: "naive" and "aware".
19+
20+
An aware object has sufficient knowledge of applicable algorithmic and
21+
political time adjustments, such as time zone and daylight saving time
22+
information, to locate itself relative to other aware objects. An aware object
23+
is used to represent a specific moment in time that is not open to
24+
interpretation [#]_.
25+
26+
A naive object does not contain enough information to unambiguously locate
27+
itself relative to other date/time objects. Whether a naive object represents
28+
Coordinated Universal Time (UTC), local time, or time in some other timezone is
29+
purely up to the program, just like it is up to the program whether a
30+
particular number represents metres, miles, or mass. Naive objects are easy to
31+
understand and to work with, at the cost of ignoring some aspects of reality.
32+
33+
For applications requiring aware objects, :class:`.datetime` and :class:`.time`
34+
objects have an optional time zone information attribute, :attr:`tzinfo`, that
35+
can be set to an instance of a subclass of the abstract :class:`tzinfo` class.
36+
These :class:`tzinfo` objects capture information about the offset from UTC
37+
time, the time zone name, and whether Daylight Saving Time is in effect. Note
38+
that only one concrete :class:`tzinfo` class, the :class:`timezone` class, is
39+
supplied by the :mod:`datetime` module. The :class:`timezone` class can
40+
represent simple timezones with fixed offset from UTC, such as UTC itself or
41+
North American EST and EDT timezones. Supporting timezones at deeper levels of
42+
detail is up to the application. The rules for time adjustment across the
3743
world are more political than rational, change frequently, and there is no
3844
standard suitable for every application aside from UTC.
3945

@@ -114,10 +120,13 @@ Objects of these types are immutable.
114120

115121
Objects of the :class:`date` type are always naive.
116122

117-
An object *d* of type :class:`.time` or :class:`.datetime` may be naive or aware.
118-
*d* is aware if ``d.tzinfo`` is not ``None`` and ``d.tzinfo.utcoffset(d)`` does
119-
not return ``None``. If ``d.tzinfo`` is ``None``, or if ``d.tzinfo`` is not
120-
``None`` but ``d.tzinfo.utcoffset(d)`` returns ``None``, *d* is naive.
123+
An object of type :class:`.time` or :class:`.datetime` may be naive or aware.
124+
A :class:`.datetime` object *d* is aware if ``d.tzinfo`` is not ``None`` and
125+
``d.tzinfo.utcoffset(d)`` does not return ``None``. If ``d.tzinfo`` is
126+
``None``, or if ``d.tzinfo`` is not ``None`` but ``d.tzinfo.utcoffset(d)``
127+
returns ``None``, *d* is naive. A :class:`.time` object *t* is aware
128+
if ``t.tzinfo`` is not ``None`` and ``t.tzinfo.utcoffset(None)`` does not return
129+
``None``. Otherwise, *t* is naive.
121130

122131
The distinction between naive and aware doesn't apply to :class:`timedelta`
123132
objects.
@@ -1846,3 +1855,7 @@ Notes:
18461855
When the ``%z`` directive is provided to the :meth:`strptime` method, an
18471856
aware :class:`.datetime` object will be produced. The ``tzinfo`` of the
18481857
result will be set to a :class:`timezone` instance.
1858+
1859+
.. rubric:: Footnotes
1860+
1861+
.. [#] If, that is, we ignore the effects of Relativity

0 commit comments

Comments
 (0)