-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datetime.astimezone() method does not handle invalid local times as required by PEP 495 #83861
Comments
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:00 Also, 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 |
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:
(kudos to @pganssle!) |
WIP - test and pure python fix Resolves pythongh-83861
WIP - test and pure python fix Resolves pythongh-83861
* main: (24 commits) pythongh-98040: Move the Single-Phase Init Tests Out of test_imp (pythongh-102561) pythongh-83861: Fix datetime.astimezone() method (pythonGH-101545) pythongh-102856: Clean some of the PEP 701 tokenizer implementation (python#103634) pythongh-102856: Skip test_mismatched_parens in WASI builds (python#103633) pythongh-102856: Initial implementation of PEP 701 (python#102855) pythongh-103583: Add ref. dependency between multibytecodec modules (python#103589) pythongh-83004: Harden msvcrt further (python#103420) pythonGH-88342: clarify that `asyncio.as_completed` accepts generators yielding tasks (python#103626) pythongh-102778: IDLE - make sys.last_exc available in Shell after traceback (python#103314) pythongh-103582: Remove last references to `argparse.REMAINDER` from docs (python#103586) pythongh-103583: Always pass multibyte codec structs as const (python#103588) pythongh-103617: Fix compiler warning in _iomodule.c (python#103618) pythongh-103596: [Enum] do not shadow mixed-in methods/attributes (pythonGH-103600) pythonGH-100530: Change the error message for non-class class patterns (pythonGH-103576) pythongh-95299: Remove lingering setuptools reference in installer scripts (pythonGH-103613) [Doc] Fix a typo in optparse.rst (python#103504) pythongh-101100: Fix broken reference `__format__` in `string.rst` (python#103531) pythongh-95299: Stop installing setuptools as a part of ensurepip and venv (python#101039) pythonGH-103484: Docs: add linkcheck allowed redirects entries for most cases (python#103569) pythongh-67230: update whatsnew note for csv changes (python#103598) ...
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
The text was updated successfully, but these errors were encountered: