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.

Author p-ganssle
Recipients belopolsky, docs@python, p-ganssle
Date 2019-07-02.16:01:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1562083298.89.0.799843667155.issue37488@roundup.psfhosted.org>
In-reply-to
Content
Between Python 2 and Python 3, the meaning of a naive datetime underwent a subtle change. Previously a naive datetime was mostly treated as an abstract datetime in the same way a unitless number is treated as an abstract quantity (this is reflected in the current datetime documentation). In Python 3, though, it became more concrete in the sense that rather than just throwing an error whenever you try to do an operation that requires knowing the absolute datetime (e.g. convert between time zones, convert to timestamp, etc), certain operations will succeed with the assumption that naive times represent system local times. This makes `utcnow()` and `utcfromtimestamp()` dangerous, because they create a naive datetime as if the naive datetime represents UTC, but this context is then lost and if you ever use one of these "aware-only" operations (.astimezone, .timestamp, etc), you'll get an unexpected answer.

For example, see this script, executed this with `TZ=America/New_York` on a machine with IANA data installed:

    >>> from datetime import datetime
    >>> dt = datetime.utcfromtimestamp(0)
    >>> dt
    datetime.datetime(1970, 1, 1, 0, 0)
    >>> dt.timestamp()
    18000

This happens because EST is 18000s behind UTC, and `.timestamp()` gives a number of seconds after UTC (a concrete, not an abstract time). You get the same gotchas with `utcnow()`.

I'm not sure if actually deprecating `utcnow` and `utcfromtimestamp` is worth it at the moment, but we can definitely start by adding a warning box to `utcnow()` and `utcfromtimestamp()` suggesting that you use the timezone-aware versions of these instead. We may also want to adjust the opening phrasing for the "naive objects" portion of the datetime documentation as well (https://docs.python.org/3.7/library/datetime.html), to reflect the fact that naive datetimes are no longer purely abstract quantities.
History
Date User Action Args
2019-07-02 16:01:38p-gansslesetrecipients: + p-ganssle, belopolsky, docs@python
2019-07-02 16:01:38p-gansslesetmessageid: <1562083298.89.0.799843667155.issue37488@roundup.psfhosted.org>
2019-07-02 16:01:38p-gansslelinkissue37488 messages
2019-07-02 16:01:38p-gansslecreate