Repository navigation
datetime.astimezone() method does not handle invalid local times as required by PEP 495 #83861
Copy link
Copy link
Closed
Labels
3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Activity
Let g be an invalid time in a New York spring-forward gap:
>>> g = datetime(2020, 3, 8, 2, 30)
According to PEP-495, conversion of such instance to UTC should return a value that corresponds to a valid local time greater than g, but
>>> print(g.astimezone(timezone.utc).astimezone()) 2020-03-08 01:30:00-05:00Also, conversion of the same instance with fold=1 to UTC and back should produce a lesser time, but
>>> print(g.replace(fold=1).astimezone(timezone.utc).astimezone()) 2020-03-08 03:30:00-04:00
Note that conversion to and from timestamp works correctly:
>>> print(datetime.fromtimestamp(g.timestamp())) 2020-03-08 03:30:00 >>> print(datetime.fromtimestamp(g.replace(fold=1).timestamp())) 2020-03-08 01:30:00
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error3.9 (EOL)end of lifeend of life
on Feb 19, 2020 - addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Feb 19, 2020 This bug is still present in the current master:
Python 3.12.0a4+ (heads/main:0675b8f032, Feb 2 2023, 18:31:34) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import time, os >>> from datetime import datetime, timezone >>> os.environ['TZ'] = 'America/New_York' >>> time.tzset() >>> time_in_gap = datetime(2020, 3, 8, 2, 30) >>> print(time_in_gap.astimezone(timezone.utc).astimezone()) 2020-03-08 01:30:00-05:00 >>> print(time_in_gap.replace(fold=1).astimezone(timezone.utc).astimezone()) 2020-03-08 03:30:00-04:00
Note that the same calculation done with explicit zone info gives correct results:
>>> from zoneinfo import ZoneInfo >>> New_York = ZoneInfo('America/New_York') >>> time_in_gap = datetime(2020, 3, 8, 2, 30, tzinfo=New_York) >>> print(time_in_gap.astimezone(timezone.utc).astimezone(New_York)) 2020-03-08 03:30:00-04:00 >>> print(time_in_gap.replace(fold=1).astimezone(timezone.utc).astimezone(New_York)) 2020-03-08 01:30:00-05:00(kudos to @pganssle!)
- added a commit that references this issue
on Apr 20, 2023 - addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Nov 28, 2023 The PR was merged for 3.12, so I'll close.
Metadata
Metadata
Assignees
Labels
3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs