Skip to content

gh-116111: Clarify DST behaviour in datetime arithmetic and zoneinfo docs - #137092

Draft
NehaJeevan wants to merge 5 commits into
python:mainfrom
NehaJeevan:fix-issue-116111-on-datetime-docs
Draft

NehaJeevan wants to merge 5 commits into
python:mainfrom
NehaJeevan:fix-issue-116111-on-datetime-docs

Conversation

@NehaJeevan

@NehaJeevan NehaJeevan commented Jul 24, 2025 •

Copy link
Copy Markdown

This PR updates the documentation for datetime and zoneinfo to clarify how arithmetic involving aware datetime objects behaves during daylight saving time (DST) transitions.

Changes include:

  • A .. warning:: in the timedelta section noting that arithmetic on aware datetime objects with the same tzinfo ignores DST.
  • A .. note:: in the datetime subtraction section, with a concrete example, explaining the "naive time" behavior.
  • A correction to the misleading sentence in zoneinfo.rst that previously claimed "no further intervention" was required to handle DST transitions.
  • A corresponding .. note:: in zoneinfo.rst to show how timedelta arithmetic skips DST transitions unless handled explicitly.

This PR resolves gh-116111 based on the suggestions on the issue


📚 Documentation preview 📚: https://cpython-previews--137092.org.readthedocs.build/

@python-cla-bot

python-cla-bot Bot commented Jul 24, 2025 •

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

Comment thread Doc/library/zoneinfo.rst Resolved
Neha Jeevan added 2 commits July 25, 2025 18:39
@NehaJeevan
NehaJeevan requested a review from cjw296 July 25, 2025 17:48
@StanFromIreland

Copy link
Copy Markdown
Member

Please do not use the Update Branch button unless necessary (e.g. fixing conflicts, jogging the CI, or very old PRs) as it uses valuable resources. For more information see the devguide.

@tusharchou

Copy link
Copy Markdown

I have been waiting for this fix. Thank you @NehaJeevan

@pganssle

Copy link
Copy Markdown
Member

I just took a cursory look, but the issue I have with this change in general is that it seems to be coming from a perspective that implies that datetime arithmetic is supposed to be absolute time arithmetic and that Python has some warts that makes this untrue in certain circumstances.

This was of thinking about things does the users a disservice, because that is not how datetime arithmetic works, and users should be as quickly disabused of this mental model as possible, rather than encouraging them to keep that mental model and introducing a few edge cases where it fails. With the correct mental model the behavior of datetime is entirely unsurprising.

@NehaJeevan

Copy link
Copy Markdown
Author

@pganssle Thanks for the comment. I understand that Python datetime arithmetic is designed around wall time (based on comments on gh-116111) and my doc change doesn’t dispute that.

But I think it’s important to acknowledge that this behavior is very often misunderstood, especially around DST transitions. Python is one of the few languages to expose both fold and tzinfo while also performing arithmetic in local time. That combination is unusual and silently surprising to users expecting “elapsed” behavior.

The phrasing in the docs doesn’t imply that the current behavior is incorrect or a failure. It explains how arithmetic behaves, what implications that has across DST, and gives a correct, standard workaround (astimezone to UTC). This is entirely in line with how similar patterns are documented in other places (e.g. in astimezone itself), and it's consistent with the language and tone of the Python documentation style guide.

I think simply saying “this is by design” doesn't help users who are surprised especially since this exact confusion is what led to issue gh-116111 in the first place.

If you feel the current wording reinforces the wrong mental model, I’m open to rephrasing but I’d really appreciate a concrete suggestion. It’s not clear to me what part you’d want changed, especially since the doc already makes it clear that wall time is the model being used. The goal here is just to help users avoid common misunderstandings without misrepresenting the design.

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 27, 2026
@Lenormju

Copy link
Copy Markdown
Contributor

Hi @NehaJeevan, thanks for this PR. I hit this behaviour myself recently and looked up how to fix the docs, which led me here.

I opened #162816, which follows pganssle's comment above: it describes same-tzinfo arithmetic as wall time arithmetic by design, and shows how to get absolute time semantics by doing the arithmetic in UTC. Your PR was the first to tackle the zoneinfo.rst sentence, and I credited you in the description. Reviews on my PR are welcome.

@picnixz

picnixz commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

@NehaJeevan Do you plan to continue working on that? Since the CLA isn't yet signed, we can't merge your contribution even if it is updated accordingly.

@picnixz
picnixz marked this pull request as draft November 28, 2026 17:02
@picnixz
picnixz marked this pull request as ready for review November 28, 2026 17:02
@picnixz
picnixz marked this pull request as draft November 28, 2026 17:02
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #36232768 | 📁 Comparing 270fa87 against main (4f74347)

  🔍 Preview build  

2 files changed
± library/datetime.html
± library/zoneinfo.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news stale Stale PR or inactive for long period of time.

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

datetime arithmetic during DST transitions is incorrect

7 participants