Skip to content

Preserve distinct interval endpoints across a DST fold - #1501

Open
0718lol wants to merge 2 commits into
python-babel:masterfrom
0718lol:fix-format-interval-dst-fold
Open

0718lol wants to merge 2 commits into
python-babel:masterfrom
0718lol:fix-format-interval-dst-fold

Conversation

@0718lol

@0718lol 0718lol commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #1500.

Summary

During a DST fall-back, two datetime values can have identical wall-clock fields and share a tzinfo object while representing different instants. Python's equality comparison ignores fold in that case, so format_interval() can take its single-instant shortcut before applying the requested display timezone.

When equal datetime endpoints share a timezone and have different folds, compare their UTC offsets before treating them as one instant. Distinct offsets now take the existing interval-formatting path.

Verification

  • Added a regression test for the two occurrences of 01:15 in New York. With UTC as the display timezone, the expected output is 5:15 – 6:15 AM.
  • On upstream master at 394f91a, the new test was the only failure in tests/test_date_intervals.py before applying the fix.
  • After applying the fix, the interval, dates, date/time formatting, and day-period suites reported 2,632 passed and 2 expected failures on Python 3.12, using the CLDR data bundled with Babel 2.19.0.
  • Ruff lint and git diff --check passed.

Investigation, implementation, tests, and drafting were developed with Codex assistance.

Check UTC offsets before collapsing equal wall-clock datetimes with different fold values.
@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.44%. Comparing base (394f91a) to head (f2bf034).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1501   +/-   ##
=======================================
  Coverage   94.43%   94.44%           
=======================================
  Files          27       27           
  Lines        5051     5055    +4     
=======================================
+ Hits         4770     4774    +4     
  Misses        281      281           
Flag Coverage Δ
macos-26-3.10 93.53% <100.00%> (+<0.01%) ⬆️
macos-26-3.11 93.47% <100.00%> (+<0.01%) ⬆️
macos-26-3.12 93.72% <100.00%> (+<0.01%) ⬆️
macos-26-3.13 93.72% <100.00%> (+<0.01%) ⬆️
macos-26-3.14 93.70% <100.00%> (+<0.01%) ⬆️
macos-26-3.15 93.70% <100.00%> (+<0.01%) ⬆️
macos-26-3.9 93.46% <100.00%> (+<0.01%) ⬆️
macos-26-pypy3.11 93.43% <100.00%> (+<0.01%) ⬆️
macos-26-pypy3.12 93.68% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.10 93.55% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.11 93.49% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.12 93.74% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.13 93.74% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.14 93.72% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.15 93.72% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.9 93.48% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-pypy3.11 93.45% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-pypy3.12 93.70% <100.00%> (+<0.01%) ⬆️
windows-2025-3.10 93.62% <100.00%> (+<0.01%) ⬆️
windows-2025-3.11 93.56% <100.00%> (+<0.01%) ⬆️
windows-2025-3.12 93.82% <100.00%> (+<0.01%) ⬆️
windows-2025-3.13 93.82% <100.00%> (+<0.01%) ⬆️
windows-2025-3.14 93.80% <100.00%> (+<0.01%) ⬆️
windows-2025-3.15 93.80% <100.00%> (+<0.01%) ⬆️
windows-2025-3.9 93.56% <100.00%> (+<0.01%) ⬆️
windows-2025-pypy3.11 93.56% <100.00%> (+<0.01%) ⬆️
windows-2025-pypy3.12 93.82% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@akx

akx commented Nov 27, 2026

Copy link
Copy Markdown
Member

The test seems to be a contrived example. Can you think of a real life situation where someone would format an interval of the same instant across DST folds?

@codspeed

codspeed Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 note

ℹ️ This run is excluded from the branch history

This commit was force-pushed, so this run no longer appears in the branch's performance history.

✅ 88 untouched benchmarks


Comparing 0718lol:fix-format-interval-dst-fold (10a5f63) with master (394f91a)

Open in CodSpeed

@0718lol

0718lol commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

They aren't the same instant — start (01:15 EDT, 05:15 UTC) and end (01:15 EST, 06:15 UTC) are one hour apart. Python's datetime.__eq__ just returns True for them under PEP 495 because they share a tzinfo and wall-clock time.

A real-world case is a 1-hour scheduled event (maintenance window, on-call shift, or calendar meeting) spanning the autumn DST fall-back night in America/New_York (e.g. 05:15Z–06:15Z, localized via .astimezone() to 01:15 EDT and 01:15 EST), then formatted for a viewer in another timezone such as UTC or Europe/Helsinki (5:15 – 6:15 AM / 7.15–8.15). Without this check, if start == end: triggers before converting to tzinfo and collapses the 1-hour interval to a single timestamp.

I updated the test to construct the endpoints via .astimezone() from UTC timestamps one hour apart so the scenario is clearer instead of calling .replace(fold=1) directly.

@0718lol
0718lol force-pushed the fix-format-interval-dst-fold branch from 10a5f63 to f2bf034 Compare November 28, 2026 05:22

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

format_interval collapses distinct instants during the DST fall-back hour

2 participants