Subsequent datetime.now() calls return the same datetime
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.nowmay return the same instant #124834 - [3.13] gh-124832: Add a note to indicate that
datetime.nowmay return the same instant (GH-124834) #125145 - [3.12] gh-124832: Add a note to indicate that
datetime.nowmay return the same instant (GH-124834) #125146