From 3eba13baf95a63fe815a31b17b9d5ceb1da6c7d9 Mon Sep 17 00:00:00 2001 From: Julien LENORMAND Date: Thu, 5 Nov 2026 11:21:39 +0100 Subject: [PATCH] gh-116111: Document wall time arithmetic for datetimes sharing a tzinfo Add a section on wall time and absolute time semantics, with an example of doing the arithmetic in UTC, and link to it from the datetime operation notes and from zoneinfo. In the subtraction and comparison notes, say that fold is ignored when both operands share the same tzinfo object, and that skipped times are never equal to times in another time zone. --- Doc/library/datetime.rst | 73 +++++++++++++++++++++++++++++++++------- Doc/library/zoneinfo.rst | 5 +-- 2 files changed, 63 insertions(+), 15 deletions(-) diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index 20d6f0cb9d4abff..4af765e74f59821 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -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 @@ -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 `. 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 @@ -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 @@ -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. @@ -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 ` datetimes; +* subtracting or comparing two :ref:`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` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/Doc/library/zoneinfo.rst b/Doc/library/zoneinfo.rst index f5d3ade478ffb22..439af70b89b243b 100644 --- a/Doc/library/zoneinfo.rst +++ b/Doc/library/zoneinfo.rst @@ -46,8 +46,9 @@ method or :meth:`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 +`: 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)