Skip to content
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

Open
abalkin opened this issue Feb 19, 2020 · 2 comments
Assignees
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@abalkin
Copy link
Member

abalkin commented Feb 19, 2020

BPO 39680
Nosy @abalkin, @pganssle

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:

assignee = 'https://github.com/abalkin'
closed_at = None
created_at = <Date 2020-02-19.00:56:00.603>
labels = ['type-bug', '3.9']
title = 'datetime.astimezone() method does not handle invalid local times as required by PEP 495'
updated_at = <Date 2020-02-19.00:56:00.603>
user = 'https://github.com/abalkin'

bugs.python.org fields:

activity = <Date 2020-02-19.00:56:00.603>
actor = 'belopolsky'
assignee = 'belopolsky'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2020-02-19.00:56:00.603>
creator = 'belopolsky'
dependencies = []
files = []
hgrepos = []
issue_num = 39680
keywords = []
message_count = 1.0
messages = ['362241']
nosy_count = 2.0
nosy_names = ['belopolsky', 'p-ganssle']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue39680'
versions = ['Python 3.9']

Linked PRs

@abalkin
Copy link
Member Author

abalkin commented Feb 19, 2020

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

@abalkin abalkin added the 3.9 only security fixes label Feb 19, 2020
@abalkin abalkin self-assigned this Feb 19, 2020
@abalkin abalkin added type-bug An unexpected behavior, bug, or error 3.9 only security fixes labels Feb 19, 2020
@abalkin abalkin self-assigned this Feb 19, 2020
@abalkin abalkin added the type-bug An unexpected behavior, bug, or error label Feb 19, 2020
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@abalkin
Copy link
Member Author

abalkin commented Feb 2, 2023

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!)

abalkin added a commit to abalkin/cpython-1 that referenced this issue Feb 3, 2023
abalkin added a commit to abalkin/cpython-1 that referenced this issue Feb 6, 2023
carljm added a commit to carljm/cpython that referenced this issue Apr 20, 2023
* 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)
  ...
@iritkatriel iritkatriel added the stdlib Python modules in the Lib dir label Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

2 participants