Skip to content

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

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

Closed
spacemanspiff2007 opened this issue Oct 1, 2024 · 3 comments
Closed

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

spacemanspiff2007 opened this issue Oct 1, 2024 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@spacemanspiff2007
Copy link
Contributor

spacemanspiff2007 commented Oct 1, 2024

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

@vstinner
Copy link
Member

vstinner commented Oct 1, 2024

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.

@vstinner
Copy link
Member

vstinner commented Oct 1, 2024

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?

@spacemanspiff2007
Copy link
Contributor Author

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.

willingc pushed a commit that referenced this issue Oct 8, 2024
…same instant (#124834)

* Update datetime.rst

* Update datetime.rst

replace warning with note

* Update Doc/library/datetime.rst

Co-authored-by: Victor Stinner <[email protected]>

* Update Doc/library/datetime.rst

Co-authored-by: Bénédikt Tran <[email protected]>

---------

Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 8, 2024
…n the same instant (pythonGH-124834)

* Update datetime.rst

* Update datetime.rst

replace warning with note

* Update Doc/library/datetime.rst

Co-authored-by: Victor Stinner <[email protected]>

* Update Doc/library/datetime.rst

Co-authored-by: Bénédikt Tran <[email protected]>

---------

(cherry picked from commit 760b1e1)

Co-authored-by: spacemanspiff2007 <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 8, 2024
…n the same instant (pythonGH-124834)

* Update datetime.rst

* Update datetime.rst

replace warning with note

* Update Doc/library/datetime.rst

Co-authored-by: Victor Stinner <[email protected]>

* Update Doc/library/datetime.rst

Co-authored-by: Bénédikt Tran <[email protected]>

---------

(cherry picked from commit 760b1e1)

Co-authored-by: spacemanspiff2007 <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
willingc pushed a commit that referenced this issue Oct 8, 2024
…rn the same instant (GH-124834) (#125145)

gh-124832: Add a note to indicate that `datetime.now` may return the same instant (GH-124834)

* Update datetime.rst

* Update datetime.rst

replace warning with note

* Update Doc/library/datetime.rst



* Update Doc/library/datetime.rst



---------

(cherry picked from commit 760b1e1)

Co-authored-by: spacemanspiff2007 <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
willingc pushed a commit that referenced this issue Oct 8, 2024
…rn the same instant (GH-124834) (#125146)

gh-124832: Add a note to indicate that `datetime.now` may return the same instant (GH-124834)

* Update datetime.rst

* Update datetime.rst

replace warning with note

* Update Doc/library/datetime.rst



* Update Doc/library/datetime.rst



---------

(cherry picked from commit 760b1e1)

Co-authored-by: spacemanspiff2007 <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
@willingc willingc closed this as completed Oct 8, 2024
efimov-mikhail pushed a commit to efimov-mikhail/cpython that referenced this issue Oct 9, 2024
…n the same instant (python#124834)

* Update datetime.rst

* Update datetime.rst

replace warning with note

* Update Doc/library/datetime.rst

Co-authored-by: Victor Stinner <[email protected]>

* Update Doc/library/datetime.rst

Co-authored-by: Bénédikt Tran <[email protected]>

---------

Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants