Inconsistency between datetime.now() and time.time()
See original GitHub issueThe following code snippet
import time
import freeze gun
from datetime import datetime
with freezegun.freeze_time("1970-01-01 00:00"):
print(datetime.now())
print(datetime.fromtimestamp(time.time()))
prints out the following when run in the timezone Europe/Berlin (CET)
1970-01-01 00:00:00
1970-01-01 01:00:00
The expected result would be 1970-01-01 00:00:00
in both cases.
What seems to happen is that time.time()
returns the unix-time which corresponds to 1970-01-01 00:00:00 UTC
, i.e. time.time() == 0
. When converting it to a datetime object, a time-zone shift is applied. This however is inconsistent with datettime.now()
.
In general, what is the assumption about the time zone of the argument passed to freeze_time
? Is it assumed to be in UTC or in the local time zone?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Issue 44831: Inconsistency between datetime.now() and ...
now() seems to measure time slightly differently than the time module imported by a Python user. - This seems to be Windows specific....
Read more >Django: datetime.now() time inconsistency in model object save
datetime.now() returns local time, so the only way I can see that this could occur is if different requests have a different local...
Read more >Inconsistent date formats across time functions - MathWorks
I'm converting dates, and noticed that the format specifiers in datetime() are inconsistent with formats in datestr(). A full specification for datetime for ......
Read more >DATETIME() - AppSheet Help
DATETIME (TODAY()) returns today's date at 12:00:00 AM from a Date value ( TODAY() returns a Date value, which has no time component,...
Read more >Incorrect time is getting displayed in PowerApp
If you are still have issues with datetime inconsistency between SQL Table and the PowerApps Value please convert the table column from datetime ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I am not sure what you mean, the indenting seems fine to me. Could you elaborate?
I personally would prefer assuming the local timezone, if the timezone is not explicitly specified. This fits my use case more and seems to be more inline with what Python does: it returns datetimes in the local timezone by default. But assuming UTC is also fine.
In any case (whether the datetime passed to freeze_time is assumed to be in UTC or in the local timezone), I think the result of
datetime.now()
anddatetime.fromtimestamp(time.time())
should be the same. If UTC is assumed, then currently the result ofdatetime.now()
is not correct as it should return the datetime in the local timezone, i.e. with an offset to the datetime passed to freeze_time.I have submitted a pull request with the tests, which currently fail of course. The second test assumes that the datetime passed to freeze_time is in UTC.
I guess the only way to make everything make sense is to ALWAYS pass the timezone you want somehow. You could pass
"local"
if you want the local, but if it’s explicit then the confusion would go away. We would have to have a deprecation warning for the current API then and then make it illegal.