Skip to content

fix(forge): count elapsed time across DST in DateTimeTool diff - #315

Merged
Eigenwise merged 1 commit into
Eigenwise:mainfrom
darshjoshi:fix/datetime-diff-across-dst
Oct 19, 2026
Merged

Eigenwise merged 1 commit into
Eigenwise:mainfrom
darshjoshi:fix/datetime-diff-across-dst

Conversation

@darshjoshi

Copy link
Copy Markdown
Contributor

The diff operation of the Forge DateTimeTool is off by an hour whenever a DST change falls between the two inputs, and only when the inputs come without an explicit offset.

Both naive inputs get the same ZoneInfo attached. Python subtracts two datetimes that share a tzinfo by wall clock and ignores their UTC offsets, so the result depends on how the same two instants are written:

2026-03-08T00:00:00 -> 2026-03-09T00:00:00 America/New_York => 86400.0 1 day
2026-03-08T00:00:00-05:00 -> 2026-03-09T00:00:00-04:00  => 82800.0 23 hours
2026-10-25T00:00:00 -> 2026-10-26T00:00:00 Europe/Brussels => 86400.0 1 day

The first and second lines describe the same two instants, and the real elapsed time is 23 hours. The Brussels day has 25.

The fix converts both values to UTC before subtracting. With it:

2026-03-08T00:00:00 -> 2026-03-09T00:00:00 America/New_York => 82800.0 23 hours
2026-03-08T00:00:00-05:00 -> 2026-03-09T00:00:00-04:00  => 82800.0 23 hours
2026-10-25T00:00:00 -> 2026-10-26T00:00:00 Europe/Brussels => 90000.0 1 day, 1 hour

I added a test covering both directions of the change; it fails on main. The tool's tests pass (13), and black --check and flake8 are clean.

Two naive inputs both get the same ZoneInfo, and Python subtracts
datetimes that share a tzinfo by wall clock, so a diff across a DST change
was off by an hour (a day in New York across the spring change was
reported as 24 hours instead of 23). Subtract the UTC instants instead.

@Eigenwise Eigenwise left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Verified with the declared tzdata dependency installed on Windows: the new regression fails on main and all 13 date tool tests pass with the fix. UTC subtraction gives elapsed time across both DST transitions. Thanks.

@Eigenwise
Eigenwise merged commit 4aea1fa into Eigenwise:main Oct 19, 2026
2 checks passed
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.

2 participants