Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
@spacemanspiff2007

Description

@spacemanspiff2007

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions