[New-bugs-announce] [issue40136] add warning to datetime.replace documentation to not use it for setting tzinfo unless UTC or None

Benedikt Bleimhofer report at bugs.python.org
Wed Apr 1 09:59:05 EDT 2020


New submission from Benedikt Bleimhofer <benedikt.bleimhofer at gmail.com>:

It would probably save some people a lot of time if the documentation of datetime.replace (https://docs.python.org/3/library/datetime.html#datetime.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:
https://stackoverflow.com/questions/13994594/how-to-add-timezone-into-a-naive-datetime-instance-in-python

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.

----------
assignee: docs at python
components: Documentation
messages: 365469
nosy: Benedikt Bleimhofer, docs at python
priority: normal
severity: normal
status: open
title: add warning to datetime.replace documentation to not use it for setting tzinfo unless UTC or None
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40136>
_______________________________________


More information about the New-bugs-announce mailing list