You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Subtracting one datetime from another provides a wrong timedelta if daylight savings change happens between them #96148
3.Subtraction
... is defined only if both operands are naive, or if both are aware. If one is aware and the other is naive
TypeError is raised.
If both are naive, or both are aware and have the same tzinfo attribute, the tzinfo attributes are ignored, and the result is ...
If both are aware and have different tzinfo attributes, a-b acts as if a and b were first converted to naive UTC datetimes first ....
It's generally true that datetimeintends that timezone-aware arithmetic be done by converting to UTC first, doing the arithmetic there, and then converting back to the desired timezone. That wasn't the only design decision that was possible, but is the decision that was made, and can't be changed now.
Activity
0xYasser commentedon Aug 21, 2022
The issue is in
datetime__sub__function.cpython/Lib/datetime.py
Line 2198 in bcc4cb0
The early return condition
if self._tzinfo is other._tzinfo:should be removed and letutcoffset()check always run to catch this edge case.tim-one commentedon Aug 21, 2022
This is working as designed and as documented:
It's generally true that
datetimeintends that timezone-aware arithmetic be done by converting to UTC first, doing the arithmetic there, and then converting back to the desired timezone. That wasn't the only design decision that was possible, but is the decision that was made, and can't be changed now.StanFromIreland commentedon Mar 17, 2025
As Tim mentioned, this is the correct behavior as specified in the docs.
There is nothing to do here and this should be closed.