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
datetime utcfromtimestamp ignores astimezone #81080
Comments
See below code:
>>> datetime.utcfromtimestamp(1557395250).astimezone(get_localzone()).strftime('%Y-%m-%d %H:%M:%S %z %Z')
'2019-05-09 09:47:30 +0200 CEST'
>>> datetime.fromtimestamp(1557395250).astimezone(get_localzone()).strftime('%Y-%m-%d %H:%M:%S %z %Z')
'2019-05-09 11:47:30 +0200 CEST' As you can see, utcfromtimestamp refuses to be 'timezoned', second one is correct. |
The utcfromtimestamp returns a naïve object that is assumed to be in the UTC timezone, you're then effectively turning it into a aware object in CEST timezone. I'm not sure what you think is wrong with utcfromtimestamp here, but it behaves according to documentation. |
Docs state that fromtimestamp returns a naive datetime object (https://docs.python.org/3.7/library/datetime.html#datetime.datetime.fromtimestamp) and not utcfromtimestamp. Perhaps it needs fixing. Additionally, astimezone fails silently, I even tried
>>> datetime.utcfromtimestamp(1557395250).astimezone(pytz.utc).astimezone(get_localzone()).strftime('%Y-%m-%d %H:%M:%S %z %Z')
'2019-05-09 09:47:30 +0200 CEST'
Which looks weird, but why it ignores that function silently is also weird. |
In the same page about utcfromtimestamp https://docs.python.org/3.7/library/datetime.html#datetime.datetime.utcfromtimestamp it says:
Also below, "To get an aware datetime object". All this means or implies naïve object. I've no idea what you mean by "fails silently". In what way does astimezone fail silently. You have your naïve object, then you convert it aware object, then to a different timezone. What is the problem here? |
Ok, now I see that naive object assumes my timezone in some cases. That's still mind-boggling that utcfromtimestamp returns an object that shows utc hour but when astimezone is applied it reverts without converting the hour to local timezone. See below as explanation:
>>> datetime.utcfromtimestamp(1557395250).astimezone(pytz.utc).strftime('%Y-%m-%d %H:%M:%S %z %Z')
'2019-05-09 07:47:30 +0000 UTC'
>>> datetime.utcfromtimestamp(1557395250).astimezone(get_localzone()).strftime('%Y-%m-%d %H:%M:%S %z %Z')
'2019-05-09 09:47:30 +0200 CEST' |
Naïve object does not assume anything, naïve object *lacks* timezone information. It cannot convert anything. It is a mistake to set a wrong timezone to a naïve object, a mistake that you're making. Piotr, this is a bug tracker for development of CPython, let's not turn it into support forum, there are many of those on the internet. I will not respond any further and I hope you're satisfied that this is not an issue in datetime module as distributed with standard library. |
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: