Closed
Description
BPO | 44831 |
---|---|
Nosy | @rhettinger, @pfmoore, @abalkin, @vstinner, @larryhastings, @tjguk, @stevendaprano, @zware, @zooba, @pganssle, @Miksus |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = None
created_at = <Date 2021-08-04.17:49:51.110>
labels = ['type-bug', 'library', 'OS-windows', '3.11']
title = 'Inconsistency between datetime.now() and datetime.fromtimestamp(time.time(), None)'
updated_at = <Date 2021-10-04.08:57:15.356>
user = 'https://github.com/Miksus'
bugs.python.org fields:
activity = <Date 2021-10-04.08:57:15.356>
actor = 'vstinner'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)', 'Windows']
creation = <Date 2021-08-04.17:49:51.110>
creator = 'Miksus'
dependencies = []
files = []
hgrepos = []
issue_num = 44831
keywords = []
message_count = 10.0
messages = ['398919', '398922', '399068', '399070', '399090', '399096', '399097', '403063', '403124', '403125']
nosy_count = 11.0
nosy_names = ['rhettinger', 'paul.moore', 'belopolsky', 'vstinner', 'larry', 'tim.golden', 'steven.daprano', 'zach.ware', 'steve.dower', 'p-ganssle', 'Miksus']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue44831'
versions = ['Python 3.11']
Linked PRs
Metadata
Metadata
Assignees
Projects
Status
Done
Activity
Miksus commentedon Aug 4, 2021
I am trying to measure time twice and the second measurement gives a time that is 1 microsecond before the first measurement about half of the time.
My experiment in short:
---------------------------------------------------
The problem is somewhat interesting. This does not fail:
---------------------------------------------------
And neither does this:
---------------------------------------------------
And it seems datetime.datetime.now() works the same way as to how I handled the "start" time in my first experiment:
https://github.com/python/cpython/blob/3.6/Lib/datetime.py#L1514
and therefore the issue seems to be under the hood.
I have tested this on two Windows 10 machines (Python 3.6 & 3.8) in which cases this occurred. This did not happen on Raspberry Pi OS using Python 3.7.
In short:
My actual application has some code in between the measurements suffering from the same problem thus this is not an issue affecting only toy examples.
Miksus commentedon Aug 4, 2021
I accidentially posted Python 3.6 link to the declaration of datetime.datetime.now() but this has been unchanged:
cpython/Lib/datetime.py
Line 1696 in 3d2b4c6
The actual piece of code as of now:
...
import time as _time
...
@classmethod
def now(cls, tz=None):
"Construct a datetime from time.time() and optional time zone info."
t = _time.time()
return cls.fromtimestamp(t, tz)
stevendaprano commentedon Aug 6, 2021
Confirmed on Windows with Python 3.9.6.
stevendaprano commentedon Aug 6, 2021
I've replicated this under Linux as well.
Then run it in a loop:
So while it is not as frequent as on Windows, it does occur on Linux as well.
abalkin commentedon Aug 6, 2021
Can someone try to replicate this while disabling the C acceleration:
(Before any other imports.)
If anything, this is likely to be a problem with the C implementation.
25 remaining items