Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-88994: Changedatetime.now to half-even rounding for consistency withdatetime.fromtimestamp.#134258

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vstinner merged 5 commits intopython:mainfromjhohm:gh-88994
May 19, 2025

Conversation

jhohm
Copy link
Contributor

@jhohmjhohm commentedMay 19, 2025
edited
Loading

Resolves#88994 where, on platforms with nanosecond resolution system clocks (such as Windows), the truncation withindatetime.now could cause time to appear to go backwards by 1 microsecond betweendatetime.datetime.fromtimestamp(time.time(), None) anddatetime.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:

importdatetimeimporttimedeftest():start=time.time()end=datetime.datetime.now()start=datetime.datetime.fromtimestamp(start,None)assertend>=start,f'{end=} <{start=}'foriinrange(10000000):test()

@python-cla-bot
Copy link

python-cla-botbot commentedMay 19, 2025
edited
Loading

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip 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, seehttps://devguide.python.org/ which explains how to add one.

Copy link
Member

@vstinnervstinner 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

@pgansslepganssle 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 theACKS 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
Contributor

StanFromIreland commentedMay 19, 2025
edited
Loading

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>
@vstinnervstinner merged commit4709417 intopython:mainMay 19, 2025
39 checks passed
@vstinner
Copy link
Member

Merged, thank you for your contribution!

@StanFromIreland
Copy link
Contributor

StanFromIreland commentedMay 19, 2025
edited
Loading

@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 :-)

StanFromIreland reacted with thumbs up emoji

@jhohm
Copy link
ContributorAuthor

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.

@jhohmjhohm deleted the gh-88994 branchMay 20, 2025 13:25
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@StanFromIrelandStanFromIrelandStanFromIreland left review comments

@vstinnervstinnervstinner approved these changes

@cmaloneycmaloneycmaloney approved these changes

@pgansslepgansslepganssle approved these changes

@abalkinabalkinAwaiting requested review from abalkinabalkin is a code owner

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Inconsistency between datetime.now() and datetime.fromtimestamp(time.time(), None)
5 participants
@jhohm@vstinner@StanFromIreland@cmaloney@pganssle

[8]ページ先頭

©2009-2025 Movatter.jp