Fix US timezone fold handling - #441
Open
deepakganesh78 wants to merge 1 commit into
Open
Conversation
Honor datetime.fold for ambiguous US DST transition times and set fold during UTC conversions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses the
USTimeZonehalf of #141.Reproduction
On current
master, ambiguous US/Eastern wall times at the 2011 fall DST transition ignoredatetime.fold:Converting from UTC has the same problem: both
2011-11-06T05:30Zand2011-11-06T06:30Zmap to a local01:30withfold=0, so the two distinct instants become indistinguishable.Root cause
USTimeZone.dst()used a single[start, end)DST interval and stored the DST end transition as 1am standard time, so the repeated fall-back hour was always treated as standard time.USTimeZonealso relied on the basetzinfo.fromutc(), which cannot mark repeated local times withfold=1.Fix
USTimeZone._dst_range().dst()fold-aware for both the repeated hour (fall back) and the missing hour (spring forward), following PEP 495 semantics.USTimeZone.fromutc()so that the second occurrence of a repeated local time is returned withfold=1.Dependency-free, no new imports, and compatible with the Python versions boltons supports (
foldis read viagetattr(dt, 'fold', 0)).Scope note
This PR deliberately covers only
USTimeZone. The issue also mentionsLocalTZInfo, whoseis_dst()delegates totime.mktime()/time.localtime(); making that fold-aware requires gap/ambiguity detection against the host's own zone rules and cannot be regression-tested portably (time.tzsetis unavailable on Windows, and the class cachestime.timezone/time.altzoneat import). I did not want to ship an untested change to it in the same PR — happy to follow up separately if you'd like that half too, or to adjust the scope of this one.Validation
python -m pytest tests\test_timeutils.py -q→ 6 passedboltons\timeutils.pywhile keeping the new regression tests → 2 failed, 4 passed (the tests genuinely fail without the fix)python -m pytest→ 447 passed in 7.21spyproject.toml,setup.cfg, ortox.ini.