-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
Possible bug in datetime utc #88829
Comments
>>> datetime.now(timezone.utc).timestamp()
1626556067.054988
>>> datetime.utcnow().timestamp()
1626566875.174921 Should there be a difference between the two modes? |
I don't seem to be able to reproduce this by running this one-liner: from datetime import datetime, timezone; print(datetime.now(timezone.utc).timestamp()); print(datetime.u
tcnow().timestamp()); on 3.9.5. How did you achieve this? It looks like the difference one would expect from (fast) human input). |
Nope, the timestamps in the original report are about 3 hours apart (10808+ seconds). Reports like these are often much clearer if they state the timezone of the system they're running on. Plausible here: as the docs say, But """ So I _expect_ the results to differ unless the box this is running on uses UTC as its local time. On my box, in native timezone CDT, the two ways are 5 hours apart, which is indeed CDT's offset from UTC. |
Would it be possible to change .utcnow to now return a datetime annotated with UTC "timezone"? After all, now we have timezone-aware datetimes and the convention that naive means local, this behavior might even be considered a bug. |
The difference between the two is the difference between your local time and utc. datetime.now(timezone.utc) This returns the current time in utc and is timezone aware. So the timestamp can figure out the seconds since epoch taking into account the timezone. datetime.utcnow() Returns the current utc time but is not timezone aware. When timestamp method is run, it is interpreted as a local timestamp. This is explained in the docs but perhaps it should be made clearer that I'm not sure about changing the behaviour of utcnow to return a timezone-aware dt as is it could cause hard to detect bugs in existing code. But I did have issues recently where I was using utcnow until I went back and read the docs and changed to datetime.now(timezone.utc). So it's probably a common trap to fall into. From the docs: " Note There is no method to obtain the POSIX timestamp directly from a naive datetime instance representing UTC time. If your application uses this convention and your system timezone is not set to UTC, you can obtain the POSIX timestamp by supplying tzinfo=timezone.utc: timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)" Warning Because naive datetime objects are treated by many datetime methods as local times, it is preferred to use aware datetimes to represent times in UTC. As such, the recommended way to create an object representing the current time in UTC is by calling datetime.now(timezone.utc). |
If you want to pursue changing what utcnow() does, python-ideas or python-dev would probably need to be involved. Backward-incompatible changes are very hard sells. As Paul Ganssle noted here, https://blog.ganssle.io/articles/2019/11/utcnow.html in Python 2, naïve datetimes generally did _not_ get treated as "being in local time", ever. They refused to pretend they had any opinion about time zone, and operations like .timestamp() (just "like", because .timestamp() didn't exist in Python 2) raised an exception when applied to a naïve datetime. Which, IMO, Python 3 should have stuck with. "Naïve time" was never intended to be a synonym for "local time" - or for UTC time - or for any other timezone-aware notion of time. But as Paul also said, if Python 2 had concrete tzinfo classes to begin with, it's a pretty safe bet |
@pganssle How about deprecate |
It's been deprecated, so closing this as out of date.
|
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: