This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: add warning to datetime.replace documentation to not use it for setting tzinfo unless UTC or None
Type: enhancement Stage: resolved
Components: Documentation Versions:
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Benedikt Bleimhofer, docs@python, p-ganssle
Priority: normal Keywords:

Created on 2020-04-01 13:59 by Benedikt Bleimhofer, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg365469 - (view) Author: Benedikt Bleimhofer (Benedikt Bleimhofer) Date: 2020-04-01 13:59
It would probably save some people a lot of time if the documentation of datetime.replace ( showed a warning not to use it for setting the timezone on a datetime object or at least that there is a high chance that the result will not be what you expect.

If you don't use tz.localize(), but use datetime.replace(), chances are that a historical offset is used instead; tz.localize() will pick the right offset in effect for the given date.
More information on the problem can be found here:

I ran into this problem and it took me quite some time to figure this out. datetime.replace seems more intuitive to use in this case, but since it does not work it might be useful to even link to tz.localize.
msg365470 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2020-04-01 14:04
That is a specific problem with the third-party library `pytz`, not a standard feature of the datetime module. Using `datetime.replace` is the intended way to set a time zone, see:

As of Python 3.6, we've been recommending instead of pytz, and assuming PEP 615 is accepted ( ), we will have a built in time zone type that supports IANA time zones.

I am going to close this because this is not a bug in CPython, but if you think otherwise feel free to continue using this ticket to make the case.
msg365497 - (view) Author: Benedikt Bleimhofer (Benedikt Bleimhofer) Date: 2020-04-01 17:03
Thx for this really helpful info.
After reading the article i switched all my code from using pytz to
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84317
2020-04-01 17:03:53Benedikt Bleimhofersetmessages: + msg365497
2020-04-01 14:04:39p-gansslesetstatus: open -> closed

nosy: + p-ganssle
messages: + msg365470

resolution: not a bug
stage: resolved
2020-04-01 13:59:05Benedikt Bleimhofercreate