Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 60 additions & 13 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ Supported operations:
``datetime2 - datetime1 == timedelta`` after. :exc:`OverflowError` is raised if
``datetime2.year`` would be smaller than :const:`MINYEAR` or larger than
:const:`MAXYEAR`. Note that no time zone adjustments are done even if the
input is an aware object.
input is an aware object (see :ref:`datetime-wall-time-semantics`).

(2)
Computes the ``datetime2`` such that ``datetime2 + timedelta == datetime1``. As for
Expand All @@ -1305,10 +1305,10 @@ Supported operations:
both operands are naive, or if both are aware. If one is aware and the other is
naive, :exc:`TypeError` is raised.

If both are naive, or both are aware and have the same :attr:`~.datetime.tzinfo` attribute,
the :attr:`~.datetime.tzinfo` attributes are ignored, and the result is a :class:`timedelta`
object ``t`` such that ``datetime2 + t == datetime1``. No time zone adjustments
are done in this case.
If both are naive, or both are aware and share the same :attr:`~.datetime.tzinfo`
object, the :attr:`~.datetime.fold` attribute is ignored and the result is a
:class:`timedelta` object ``t`` such that ``datetime2 + t == datetime1``,
using :ref:`wall time semantics <datetime-wall-time-semantics>`.

If both are aware and have different :attr:`~.datetime.tzinfo` attributes, ``a-b`` acts
as if ``a`` and ``b`` were first converted to naive UTC datetimes. The
Expand All @@ -1321,14 +1321,14 @@ Supported operations:

Naive and aware :class:`.datetime` objects are never equal.

If both comparands are aware, and have the same :attr:`!tzinfo` attribute,
the :attr:`!tzinfo` and :attr:`~.datetime.fold` attributes are ignored and
the base datetimes are compared.
If both comparands are aware and share the same :attr:`!tzinfo` object,
the :attr:`~.datetime.fold` attribute is ignored and the base datetimes
are compared (see :ref:`datetime-wall-time-semantics`).
If both comparands are aware and have different :attr:`~.datetime.tzinfo`
attributes, the comparison acts as comparands were first converted to UTC
datetimes except that the implementation never overflows.
:class:`.datetime` instances in a repeated interval are never equal to
:class:`!datetime` instances in other time zone.
:class:`.datetime` instances in a repeated or skipped interval are never equal to
:class:`!datetime` instances in another time zone.

(5)
*datetime1* is considered less than *datetime2* when *datetime1* precedes
Expand All @@ -1337,9 +1337,9 @@ Supported operations:
Order comparison between naive and aware :class:`.datetime` objects
raises :exc:`TypeError`.

If both comparands are aware, and have the same :attr:`!tzinfo` attribute,
the :attr:`!tzinfo` and :attr:`~.datetime.fold` attributes are ignored and
the base datetimes are compared.
If both comparands are aware and share the same :attr:`!tzinfo` object,
the :attr:`~.datetime.fold` attribute is ignored and the base datetimes
are compared (see :ref:`datetime-wall-time-semantics`).
If both comparands are aware and have different :attr:`~.datetime.tzinfo`
attributes, the comparison acts as comparands were first converted to UTC
datetimes except that the implementation never overflows.
Expand Down Expand Up @@ -1690,6 +1690,53 @@ Instance methods:
See also :ref:`strftime-strptime-behavior` and :meth:`datetime.isoformat`.


.. _datetime-wall-time-semantics:

Wall time and absolute time semantics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Some operations use *wall time* semantics. They work on the local date and
time fields, as read on a clock, and ignore :attr:`~.datetime.fold` and the
UTC offset. These operations are:

* adding or subtracting a :class:`timedelta`;
* subtracting or comparing two :ref:`naive <datetime-naive-aware>` datetimes;
* subtracting or comparing two :ref:`aware <datetime-naive-aware>` datetimes
that share the same :attr:`~.datetime.tzinfo`.

For example, adding ``timedelta(days=1)`` gives the same clock time on the
next day, even across a change of UTC offset. The result can be a time that
the local clock skips or repeats. The result always has ``fold=0``.

Subtracting or comparing aware datetimes with different :attr:`!tzinfo`
uses *absolute time* semantics: both operands are first converted to UTC.
Equality has one exception: a time that is skipped or repeated (see
:attr:`~.datetime.fold`) is never equal to a time in another time zone.

To use absolute time semantics within a single time zone, do the arithmetic
in UTC::

>>> import datetime as dt
>>> from zoneinfo import ZoneInfo
>>> new_york = ZoneInfo("America/New_York")
>>> first = dt.datetime(2026, 11, 1, 1, 30, tzinfo=new_york)
>>> second = first.replace(fold=1)
>>> second - first
datetime.timedelta(0)
>>> second.astimezone(dt.timezone.utc) - first.astimezone(dt.timezone.utc)
datetime.timedelta(seconds=3600)
>>> wall = first + dt.timedelta(hours=1)
>>> print(wall.fold, wall)
0 2026-11-01 02:30:00-05:00
>>> absolute = (first.astimezone(dt.timezone.utc) + dt.timedelta(hours=1)).astimezone(new_york)
>>> print(absolute.fold, absolute)
1 2026-11-01 01:30:00-05:00

Adding one hour of wall time to 01:30 EDT gives 02:30 EST, two hours later in
elapsed time, because the repeated hour is skipped. Adding one elapsed hour
gives the second 01:30, with ``fold=1``.


Examples of usage: :class:`!datetime`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
5 changes: 3 additions & 2 deletions Doc/library/zoneinfo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ method or :meth:`datetime.astimezone <datetime.datetime.astimezone>`::
>>> when.tzname()
'PDT'

Datetimes constructed in this way are compatible with datetime arithmetic and
handle daylight saving time transitions with no further intervention::
Arithmetic on these datetimes uses :ref:`wall time semantics
<datetime-wall-time-semantics>`: adding a day gives the same clock time on
the next day, while 25 hours have elapsed due to the UTC offset change::

>>> when_add = when + dt.timedelta(days=1)

Expand Down
Loading