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

Document the "gotcha" behaviors in utcnow() and utcfromtimestamp() #81669

Closed
pganssle opened this issue Jul 2, 2019 · 5 comments
Closed

Document the "gotcha" behaviors in utcnow() and utcfromtimestamp() #81669

pganssle opened this issue Jul 2, 2019 · 5 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@pganssle
Copy link
Member

pganssle commented Jul 2, 2019

BPO 37488
Nosy @abalkin, @pganssle, @miss-islington, @epicfaace
PRs
  • bpo-37488 : Document a warning for datetime.utcnow() and utcfromtimestamp() #15773
  • [3.8] bpo-37488 : Document a warning for datetime.utcnow() and utcfromtimestamp() (GH-15773) #16059
  • 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 = None
    closed_at = <Date 2019-09-12.14:59:27.100>
    created_at = <Date 2019-07-02.16:01:38.841>
    labels = ['3.8', 'type-feature', '3.7', '3.9', 'docs']
    title = 'Document the "gotcha" behaviors in utcnow() and utcfromtimestamp()'
    updated_at = <Date 2019-09-12.14:59:27.100>
    user = 'https://github.com/pganssle'

    bugs.python.org fields:

    activity = <Date 2019-09-12.14:59:27.100>
    actor = 'p-ganssle'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2019-09-12.14:59:27.100>
    closer = 'p-ganssle'
    components = ['Documentation']
    creation = <Date 2019-07-02.16:01:38.841>
    creator = 'p-ganssle'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37488
    keywords = ['patch']
    message_count = 5.0
    messages = ['347148', '351567', '351884', '352189', '352192']
    nosy_count = 5.0
    nosy_names = ['belopolsky', 'docs@python', 'p-ganssle', 'miss-islington', 'epicfaace']
    pr_nums = ['15773', '16059']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue37488'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    @pganssle
    Copy link
    Member Author

    pganssle commented Jul 2, 2019

    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.

    @pganssle pganssle added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes labels Jul 2, 2019
    @pganssle pganssle added docs Documentation in the Doc dir type-feature A feature request or enhancement labels Jul 2, 2019
    @epicfaace
    Copy link
    Mannequin

    epicfaace mannequin commented Sep 10, 2019

    Why not deprecate them?

    @miss-islington
    Copy link
    Contributor

    New changeset 1a53c78 by Miss Islington (bot) (Joannah Nanjekye) in branch 'master':
    bpo-37488 : Document a warning for datetime.utcnow() and utcfromtimestamp() (GH-15773)
    1a53c78

    @miss-islington
    Copy link
    Contributor

    New changeset 307c5fe by Miss Islington (bot) in branch '3.8':
    bpo-37488 : Document a warning for datetime.utcnow() and utcfromtimestamp() (GH-15773)
    307c5fe

    @pganssle
    Copy link
    Member Author

    Thanks Joannah!

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes docs Documentation in the Doc dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants