GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Subsequent datetime.now() calls return the same datetime · Issue #124832 · python/cpython

Subsequent datetime.now() calls return the same datetime #124832

New issue
Copy link
New issue
Copy link
Closed
Closed
Subsequent datetime.now() calls return the same datetime#124832
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@spacemanspiff2007
spacemanspiff2007
opened on Oct 1, 2024
Issue body actions

Bug report

Bug description:

Subsequent datetime.now() may return the same moment in time.

from datetime import datetime, UTC

now1 = datetime.now(UTC)
for i in range(10_000):
    a = i
now2 = datetime.now(UTC)

assert now1 < now2
    assert now1 < now2
           ^^^^^^^^^^^
AssertionError

This does seem to work as expected:

from time import sleep
from datetime import datetime, UTC

now1 = datetime.now(UTC)
sleep(0.000_000_001)
now2 = datetime.now(UTC)

assert now1 < now2

I'm not sure if this is a bug, but at least it's a documentation issue.
There's nothing in the docs explicitly indicating this behavior.

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs

  • gh-124832: Add a note to indicate that datetime.now may return the same instant #124834
  • [3.13] gh-124832: Add a note to indicate that datetime.now may return the same instant (GH-124834) #125145
  • [3.12] gh-124832: Add a note to indicate that datetime.now may return the same instant (GH-124834) #125146

Activity

  1. spacemanspiff2007
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Oct 1, 2024
  2. vstinner commented on Oct 1, 2024

    @vstinner
    vstinner
    on Oct 1, 2024
    Member
    More actions

    CPython versions tested on: 3.12
    Operating systems tested on: Windows

    Test with Python 3.13, it got better clocks: https://docs.python.org/dev/whatsnew/3.13.html#time

    On Windows, time() now uses the GetSystemTimePreciseAsFileTime() clock for a resolution of 1 microsecond, instead of the GetSystemTimeAsFileTime() clock which has a resolution of 15.6 milliseconds. (Contributed by Victor Stinner in gh-63207.)

    On Python 3.12, it's more likely that two consecutive calls to datetime.now() gives the same microseconds.

    Reacted by Gilles Marchand
  3. vstinner commented on Oct 1, 2024

    @vstinner
    vstinner
    on Oct 1, 2024
    Member
    More actions

    I'm not sure if this is a bug, but at least it's a documentation issue.

    It's not a bug.

    There's nothing in the docs explicitly indicating this behavior.

    What do you expect in the doc?

  4. spacemanspiff2007 commented on Oct 1, 2024

    @spacemanspiff2007
    spacemanspiff2007
    on Oct 1, 2024
    ContributorAuthor
    More actions

    I understand and suspected as much.

    What do you expect in the doc?

    A warning that due to the clock resolution it's possible that two datetime.now() calls can return the same time even though a "substantial" amount of time has passed.

    I had some tests failing which check that a datetime value which was updated was in the future.
    That failed and I could not figure out why. I supplied UTC as tz because the docs mention it might increase precision but with no success.
    A warning or hint would have been nice which I tried to add in the PR.

  5. willingc
    added a commit that references this issue on Oct 8, 2024

    gh-124832: Add a note to indicate that `datetime.now` may return the …

    760b1e1
  6. miss-islington
    added 2 commits that reference this issue on Oct 8, 2024

    pythongh-124832: Add a note to indicate that `datetime.now` may retur…

    bf0b607

    pythongh-124832: Add a note to indicate that `datetime.now` may retur…

    fac7a3e
  7. willingc
    added 2 commits that reference this issue on Oct 8, 2024

    [3.13] gh-124832: Add a note to indicate that `datetime.now` may retu…

    f5fea4d

    [3.12] gh-124832: Add a note to indicate that `datetime.now` may retu…

    382ee1c
  8. willingc
    closed this as completedon Oct 8, 2024
  9. efimov-mikhail
    added a commit that references this issue on Oct 9, 2024

    pythongh-124832: Add a note to indicate that `datetime.now` may retur…

    bbe670b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.