-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-88994: Change datetime.now to half-even rounding for consistency with datetime.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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…etime.fromtimestamp`.
|
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 |
|
Would you mind to sign the CLA? |
|
You should add a NEWS entry to document your change, see https://devguide.python.org/ which explains how to add one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
cc @pganssle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| @@ -0,0 +1,2 @@ | |||
| Change :func:`datetime.datetime.now` to half-even rounding for | |||
| consistency with :func:`datetime.datetime.fromtimestamp`. | |||
There was a problem hiding this comment.
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.
I am unable to reproduce such success on Linux? |
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
|
Merged, thank you for your contribution! |
|
@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 :-) |
|
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. |
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. |
…ython#134258) Change `datetime.datetime.now` to half-even rounding for consistency with `datetime.fromtimestamp`.
…ython#134258) Change `datetime.datetime.now` to half-even rounding for consistency with `datetime.fromtimestamp`.
Resolves #88994 where, on platforms with nanosecond resolution system clocks (such as Windows), the truncation within
datetime.nowcould 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: