-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-124832: Add a note to indicate that datetime.now
may return the same instant
#124834
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
replace warning with note
Co-authored-by: Victor Stinner <[email protected]>
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
I'm not a native speaker, but maybe it sounds better if we turn the sentence around? Depending on the precision of the underlying clock, it's possible that subsequent |
Doc/library/datetime.rst
Outdated
It's possible that subsequent ``datetime.now()`` return the same instant, | ||
depending on the precision of the underlying clock. |
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.
How about:
Depending on the precision of the underlying clock, it's possible that subsequent datetime.now() statements return the same instant.
Looking at the docs, this doesn't seem to be cluttering things up too much, but I do wonder what the threshold should be for this sort of thing. It seems like it would be obvious that if you create two It also seems like you could get To me, it feels like the kinds of edge cases we should be warning against are:
I am not convinced that this warning meets either threshold, to be honest. Maybe the @python/editorial-board wants to weigh in on the level of granularity we want with these docs, but I feel like if we start listing out all the edge cases where someone could make a mistake, the documentation will eventually be nothing but that. |
I personally agree with Paul. |
On Windows in Python 3.13, I modified the clock used by datetime to have a resolution around 238 ns (under 1 microsecond) instead of 15.6 ms: It makes this issue way less likely. |
Thanks for the PR @spacemanspiff2007. As a member of the docs editorial board, I'm a bit hesitant to add this note to the docs. As a developer, I expect that |
Well - it was not obvious to me and also not obvious to the other people who maintain the library where I created an issue because of this surprising behavior which I suspected to be a bug.
Can you show me the location in the docs where it shows that
Maybe the goal should be to improve the description then? |
datetime.now
may return the same instant
(I've edited the title since it's neither a runtime nor a docs warning). |
Still speaking for myself, while I agree that the proposed new text is clearer, I still don’t understand why this warning is needed. The phenomenon is rather obvious, unless you assume the clock has infinite precision. I could get behind a more general discussion of clock precision and its implications though, especially noting the 3.12 and before coarse precision on Windows. (That even deserves a “changed in 3.13 note”). |
Co-authored-by: Bénédikt Tran <[email protected]>
datetime provides microsecond and it does not state in the datetime docs that the precision might be less than that. If you look at time.time() it states that precision might be as low as 1s even though it's a float. There the docs make it clear. |
Looks like a confusion between accuracy and precision. Possibly the docs weren’t written with that distinction clear. For which our apologies, we should do better about that. still, how many times do you think you can call that function on a microsecond? Try to measure that (using a larger time interval ;-) on Linux. |
What I'm saying is that the datetime type implies a nanosecond precision/accuracy and the docs never state otherwise. As for what it's worth noting in the docs: In any case please feel free to close this PR. |
It probably makes the most sense to rewrite the method description (starting at line 926). Given @vstinner's change in 3.13, the likelihood of getting the same value is slim for 3.13+. That said, after rereading the method description, I found the wording awkward.
@spacemanspiff2007, that is a fair point about implying nanosecond precision/accuracy. Perhaps the best solution would be to add something similar to the description and remove the proposed note: @gvanrossum Does that make sense to you? |
I'm currently trying to find time to really take in everything that is said about this topic in both the datetime and the time module. There are several related concepts, accuracy, precision (not the same thing!), resolution, and the capacity of the representation. |
These things are hard. Despite that I wrote the original code, offhand I'm not even sure whether successive calls to |
Noting that it's still quite possible for . >>> from datetime import datetime
>>> now = datetime.now
>>> len(set(now() for i in range(1_000_000)))
492
>>> len(set(now() for i in range(1_000_000)))
488 So, as shown, when going full-bore it's rare not to get a repeated value. |
Thanks @tim-one for chiming in and offering your thoughtful perspective.
Completely agree, and perhaps all that needs to be said is "the behaviors of the platform OS and C library functions" influence the return value. |
Alas, I don’t think I will find the time any time soon. Someone else might do that review; in the meantime I don’t want to hold up this PR. Maybe @willingc can merge it? |
Sure thing @gvanrossum. @spacemanspiff2007 I'm going to merge this, and we can always revisit in the future. Thanks for your PR and patience during the review discussion. ☀️ |
Thanks @spacemanspiff2007 for the PR, and @willingc for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13. |
Thanks @spacemanspiff2007 for the PR, and @willingc for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12. |
…n the same instant (pythonGH-124834) * Update datetime.rst * Update datetime.rst replace warning with note * Update Doc/library/datetime.rst Co-authored-by: Victor Stinner <[email protected]> * Update Doc/library/datetime.rst Co-authored-by: Bénédikt Tran <[email protected]> --------- (cherry picked from commit 760b1e1) Co-authored-by: spacemanspiff2007 <[email protected]> Co-authored-by: Victor Stinner <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
…n the same instant (pythonGH-124834) * Update datetime.rst * Update datetime.rst replace warning with note * Update Doc/library/datetime.rst Co-authored-by: Victor Stinner <[email protected]> * Update Doc/library/datetime.rst Co-authored-by: Bénédikt Tran <[email protected]> --------- (cherry picked from commit 760b1e1) Co-authored-by: spacemanspiff2007 <[email protected]> Co-authored-by: Victor Stinner <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
GH-125145 is a backport of this pull request to the 3.13 branch. |
GH-125146 is a backport of this pull request to the 3.12 branch. |
…rn the same instant (GH-124834) (#125145) gh-124832: Add a note to indicate that `datetime.now` may return the same instant (GH-124834) * Update datetime.rst * Update datetime.rst replace warning with note * Update Doc/library/datetime.rst * Update Doc/library/datetime.rst --------- (cherry picked from commit 760b1e1) Co-authored-by: spacemanspiff2007 <[email protected]> Co-authored-by: Victor Stinner <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
…rn the same instant (GH-124834) (#125146) gh-124832: Add a note to indicate that `datetime.now` may return the same instant (GH-124834) * Update datetime.rst * Update datetime.rst replace warning with note * Update Doc/library/datetime.rst * Update Doc/library/datetime.rst --------- (cherry picked from commit 760b1e1) Co-authored-by: spacemanspiff2007 <[email protected]> Co-authored-by: Victor Stinner <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
…n the same instant (python#124834) * Update datetime.rst * Update datetime.rst replace warning with note * Update Doc/library/datetime.rst Co-authored-by: Victor Stinner <[email protected]> * Update Doc/library/datetime.rst Co-authored-by: Bénédikt Tran <[email protected]> --------- Co-authored-by: Victor Stinner <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
Made the implied behavior more clear
📚 Documentation preview 📚: https://cpython-previews--124834.org.readthedocs.build/