Skip to content

Fix mixed calendar and fixed-unit arithmetic across DST - #1038

Open
pranavpk404 wants to merge 1 commit into
python-pendulum:masterfrom
pranavpk404:fix/mixed-unit-dst-arithmetic
Open

pranavpk404 wants to merge 1 commit into
python-pendulum:masterfrom
pranavpk404:fix/mixed-unit-dst-arithmetic

Conversation

@pranavpk404

Copy link
Copy Markdown

Fixes #838.

Summary

For aware datetimes, a mixed calendar/fixed-duration call currently applies all
units in naive local time. Reconstructing a time in a DST gap then normalizes
it forward, even when the requested fixed-duration shift was backwards.

For example, from 2024-04-01T04:00:00+03:00 in Europe/Sofia:

  • subtract(days=1) reaches 2024-03-31T04:00:00+03:00.
  • subtract(days=1, seconds=1) currently moves 3,599 seconds later.
  • With this fix it reaches 2024-03-31T02:59:59+02:00, one second earlier.

Apply the existing calendar-only operation first, then the existing fixed-only
operation on the UTC timeline. This retains pure-calendar/fixed behavior and
the existing combined fractional rounding for naive datetimes. Document the
ordering for aware mixed-unit add and subtract calls.

Regression coverage

33 parametrized cases cover both methods and directions; days/weeks/months/years;
fractional seconds/calendar days; microseconds/minutes/hours; gaps and folds;
half-hour DST; multiple calendar units; UTC, naive and fixed-offset controls;
and subclasses. Elapsed-time expectations use independent stdlib arithmetic.

Validation

  • Before the fix: 25 new regression cases failed, 8 controls passed.
  • Updated upstream base 6394a10: full pure-Python suite 1882 passed, 3 skipped.
  • Full newly built native suite: 1882 passed, 3 skipped.
  • Native backend freshly rebuilt with locked PyO3 0.29.3 and Rust 1.83.0; activation verified.
  • Full mypy: pass (229 files).
  • prek run --all-files, including Ruff, Rust formatting and clippy: pass.
  • git diff --check: pass.

Validated on Linux / CPython 3.14.7 with current locked dependencies.

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

AI assistance was used for investigation, implementation and tests. The source
changes, independent reproduction, and regression expectations were reviewed
before submission.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible bug in .subtract() across a DST change

1 participant