KEMBAR78
gh-88994: Change `datetime.now` to half-even rounding for consistency with `datetime.fromtimestamp`. by jhohm · Pull Request #134258 · python/cpython · GitHub
Skip to content

Conversation

@jhohm
Copy link
Contributor

@jhohm jhohm commented May 19, 2025

Resolves #88994 where, on platforms with nanosecond resolution system clocks (such as Windows), the truncation within datetime.now could cause time to appear to go backwards by 1 microsecond between datetime.datetime.fromtimestamp(time.time(), None) and datetime.datetime.now().

As suggested by @pganssle, not adding a test that would probably be flaky since it would involve comparing the results of multiple reads of the system clock. Verified manually that the following quickly fails the assert on Windows but passes with this commit:

import datetime
import time

def test():
    start = time.time()
    end = datetime.datetime.now()
    start = datetime.datetime.fromtimestamp(start, None)
    assert end >= start, f'{end=} < {start=}'

for i in range(10000000):
    test()

@jhohm jhohm requested review from abalkin and pganssle as code owners May 19, 2025 17:46
@python-cla-bot
Copy link

python-cla-bot bot commented May 19, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented May 19, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@vstinner
Copy link
Member

Would you mind to sign the CLA?

@vstinner
Copy link
Member

You should add a NEWS entry to document your change, see https://devguide.python.org/ which explains how to add one.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

cc @pganssle

Copy link
Member

@pganssle pganssle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jhohm If you are not already in the ACKS file, do you want to add yourself?

@@ -0,0 +1,2 @@
Change :func:`datetime.datetime.now` to half-even rounding for
consistency with :func:`datetime.datetime.fromtimestamp`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want you can also add "Patch by (Your name however you want it rendered)." here.

@StanFromIreland
Copy link
Member

StanFromIreland commented May 19, 2025

Verified manually that the following quickly fails the assert on Windows but passes with this commit:

I am unable to reproduce such success on Linux?

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
@vstinner vstinner merged commit 4709417 into python:main May 19, 2025
39 checks passed
@vstinner
Copy link
Member

Merged, thank you for your contribution!

@StanFromIreland
Copy link
Member

StanFromIreland commented May 19, 2025

@vstinner out of curiosity does it work for you, it's hard to believe it's somehow windows only.

@vstinner
Copy link
Member

@vstinner out of curiosity does it work for you, it's hard to believe it's somehow windows only.

Using a debug build, I failed to reproduce the issue. I trust the code, I know what it does :-)

@jhohm
Copy link
Contributor Author

jhohm commented May 19, 2025

Huh; I can still reproduce the problem but only on a release Linux build. I'm investigating to see if there's something else I can do to ensure consistency there.

@vstinner
Copy link
Member

start = time.time()
end = datetime.datetime.now()
start = datetime.datetime.fromtimestamp(start, None)

time.time() uses float which is not great in terms of accuracy. Using time.time_ns() would be better. Sadly, there is no "fromtimestampns()" function.

@jhohm jhohm deleted the gh-88994 branch May 20, 2025 13:25
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
…ython#134258)

Change `datetime.datetime.now` to half-even rounding
for consistency with `datetime.fromtimestamp`.
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
…ython#134258)

Change `datetime.datetime.now` to half-even rounding
for consistency with `datetime.fromtimestamp`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistency between datetime.now() and datetime.fromtimestamp(time.time(), None)

5 participants