Skip to content

Preserve fractional timedelta seconds until formatting - #1401

Merged
akx merged 2 commits into
python-babel:masterfrom
nawaaaaaAaar:fix/timedelta-subsecond-precision
Oct 26, 2026
Merged

akx merged 2 commits into
python-babel:masterfrom
nawaaaaaAaar:fix/timedelta-subsecond-precision

Conversation

@nawaaaaaAaar

@nawaaaaaAaar nawaaaaaAaar commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #1400.

Keep timedelta fractional seconds through the existing formatting logic by using total_seconds(). This is independent of the compound-unit change in #1399; this branch starts from unchanged upstream and contains no changes from that PR.

Before/after

Unchanged upstream: e2a17919f0eb988bfea2883b00793ca4d5d37c4f.

Previously, timedelta.days * 86400 + timedelta.seconds drops microseconds and floors negative durations:

  • Plus 0.5 seconds: “in 0 seconds”; minus 0.5 seconds: “1 second ago”.
  • Plus 1.9 seconds: “1 second”; minus 1.9 seconds: “2 seconds”.
  • Plus 50.999999 seconds: “50 seconds”; minus the same magnitude: “1 minute”.

Retaining the full signed duration lets the existing rounding, threshold and minimum-unit logic handle both signs consistently, without introducing a second rounding policy.

The final regression/control matrix produces sixteen failures and twenty-eight passes against unchanged production code. It checks both signs, direction on/off, fractional rounding boundaries, the minute threshold, zero, integral durations and the maximum whole-second positive timedelta.

Verification

  • Full suite on Python 3.12.13: 7,875 passed, seven skipped, two expected failures, one existing pytest deprecation warning.
  • All 44 added regression/control cases pass.
  • All 56 date-module doctests pass.
  • Full Ruff check, source/wheel builds and whitespace checks pass.
  • An isolated installed-wheel smoke test passes on Python 3.11.15, imported outside the checkout.
  • Tested together with Respect preformatted string numerators in compound-unit formatting #1399 in a separate worktree: 7,893 passed, seven skipped, two expected failures, one existing pytest warning, with full Ruff checks passing. The submitted branches remain independent.

CLDR 48.2 data was imported using the repository's download/import script and checksum validation. Local verification is Linux-only and does not claim every supported runtime or hosted CI passed.

Assistance

Investigated, implemented and tested with AI assistance. No personal account-owner review or provenance attestation is asserted.

Copilot AI balanced review requested due to automatic review settings October 22, 2026 12:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread tests/test_dates.py Outdated
@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.48%. Comparing base (e2a1791) to head (72aee5a).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1401      +/-   ##
==========================================
+ Coverage   93.37%   93.48%   +0.11%     
==========================================
  Files          27       27              
  Lines        4922     4930       +8     
==========================================
+ Hits         4596     4609      +13     
+ Misses        326      321       -5     
Flag Coverage Δ
macos-14-3.10 92.57% <100.00%> (+0.11%) ⬆️
macos-14-3.11 92.51% <100.00%> (+0.11%) ⬆️
macos-14-3.12 92.71% <100.00%> (+0.11%) ⬆️
macos-14-3.13 92.71% <100.00%> (+0.11%) ⬆️
macos-14-3.14 92.69% <100.00%> (+0.11%) ⬆️
macos-14-3.15 92.69% <100.00%> (+0.11%) ⬆️
macos-14-3.8 92.44% <100.00%> (+0.11%) ⬆️
macos-14-3.9 92.51% <100.00%> (+0.11%) ⬆️
macos-14-pypy3.10 92.57% <100.00%> (+0.11%) ⬆️
ubuntu-24.04-3.10 92.59% <100.00%> (+0.11%) ⬆️
ubuntu-24.04-3.11 92.53% <100.00%> (+0.11%) ⬆️
ubuntu-24.04-3.12 92.73% <100.00%> (+0.11%) ⬆️
ubuntu-24.04-3.13 92.73% <100.00%> (+0.11%) ⬆️
ubuntu-24.04-3.14 92.71% <100.00%> (+0.11%) ⬆️
ubuntu-24.04-3.15 92.71% <100.00%> (+0.11%) ⬆️
ubuntu-24.04-3.8 92.47% <100.00%> (+0.11%) ⬆️
ubuntu-24.04-3.9 92.53% <100.00%> (+0.11%) ⬆️
ubuntu-24.04-pypy3.10 92.59% <100.00%> (+0.11%) ⬆️
windows-2022-3.10 92.58% <100.00%> (+0.11%) ⬆️
windows-2022-3.11 92.52% <100.00%> (+0.11%) ⬆️
windows-2022-3.12 92.73% <100.00%> (+0.11%) ⬆️
windows-2022-3.13 92.73% <100.00%> (+0.11%) ⬆️
windows-2022-3.14 92.71% <100.00%> (+0.11%) ⬆️
windows-2022-3.15 92.71% <100.00%> (+0.11%) ⬆️
windows-2022-3.8 92.56% <100.00%> (+0.11%) ⬆️
windows-2022-3.9 92.52% <100.00%> (+0.11%) ⬆️
windows-2022-pypy3.10 92.58% <100.00%> (+0.11%) ⬆️

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.

@codspeed

codspeed Bot commented Oct 26, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 88 untouched benchmarks


Comparing nawaaaaaAaar:fix/timedelta-subsecond-precision (72aee5a) with master (4438709)

Open in CodSpeed

@akx
akx merged commit 9a3ef47 into python-babel:master Oct 26, 2026
36 checks passed
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_timedelta loses fractional seconds and rounds negative durations asymmetrically

3 participants