X Tutup
The Wayback Machine - https://web.archive.org/web/20240419183457/https://github.com/python/cpython/issues/116111
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 arithmetic during DST transitions is incorrect #116111

Open
cjw296 opened this issue Feb 29, 2024 · 1 comment
Open

datetime arithmetic during DST transitions is incorrect #116111

cjw296 opened this issue Feb 29, 2024 · 1 comment
Labels
type-bug An unexpected behavior, bug, or error

Comments

@cjw296
Copy link
Contributor

cjw296 commented Feb 29, 2024

Bug description:

While working on a followup to #116035, given:

>>> from datetime import datetime, timedelta
>>> from zoneinfo import ZoneInfo
>>> t = datetime(2024, 10, 27, 1, tzinfo=ZoneInfo('Europe/London'), fold=0)
>>> str(t)
'2024-10-27 01:00:00+01:00'
>>> t2 = t + timedelta(minutes=30)
>>> t2
datetime.datetime(2024, 10, 27, 1, 30, tzinfo=zoneinfo.ZoneInfo(key='Europe/London'))
>>> str(t2)
'2024-10-27 01:30:00+01:00'

I noticed this buggy behaviour:

>>> t3 = t + timedelta(minutes=60)
>>> str(t3)
'2024-10-27 02:00:00+00:00'
>>> t3
datetime.datetime(2024, 10, 27, 2, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/London'))

The correct answer would be:

>>> datetime(2024, 10, 27, 1, tzinfo=ZoneInfo('Europe/London'), fold=1)
datetime.datetime(2024, 10, 27, 1, 0, fold=1, tzinfo=zoneinfo.ZoneInfo(key='Europe/London'))
>>> str(datetime(2024, 10, 27, 1, tzinfo=ZoneInfo('Europe/London'), fold=1))
'2024-10-27 01:00:00+00:00'

Similarly, given:

>>> t1 = datetime(2024, 3, 31, 0, tzinfo=ZoneInfo('Europe/London'))
>>> t1
datetime.datetime(2024, 3, 31, 0, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/London'))
>>> str(t1)
'2024-03-31 00:00:00+00:00'

This is incorrect:

>>> t2 = t1 + timedelta(minutes=60)
>>> t2
datetime.datetime(2024, 3, 31, 1, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/London'))
>>> str(t2)
'2024-03-31 01:00:00+00:00'

The above point in time doesn't exist, it should be:

>>> str(datetime(2024, 3, 31, 2, 0, tzinfo=ZoneInfo(key='Europe/London')))
'2024-03-31 02:00:00+01:00'

CPython versions tested on:

3.12

Operating systems tested on:

No response

@cjw296 cjw296 added the type-bug An unexpected behavior, bug, or error label Feb 29, 2024
@cjw296 cjw296 changed the title datetime arithmetic during DST folds is incorrect datetime arithmetic during DST transitions is incorrect Feb 29, 2024
@cjw296
Copy link
Contributor Author

cjw296 commented Feb 29, 2024

#116038 probably related...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

1 participant
X Tutup