Skip to content
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

Closed
PiotrKamoda mannequin opened this issue May 13, 2019 · 6 comments
Closed

datetime utcfromtimestamp ignores astimezone #81080

PiotrKamoda mannequin opened this issue May 13, 2019 · 6 comments
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@PiotrKamoda
Copy link
Mannequin

PiotrKamoda mannequin commented May 13, 2019

BPO 36899

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 = <Date 2019-05-13.09:32:50.700>
created_at = <Date 2019-05-13.09:29:19.823>
labels = ['3.7', 'invalid', 'type-bug', 'library']
title = 'datetime utcfromtimestamp ignores astimezone'
updated_at = <Date 2019-05-13.10:21:22.522>
user = 'https://bugs.python.org/PiotrKamoda'

bugs.python.org fields:

activity = <Date 2019-05-13.10:21:22.522>
actor = 'SilentGhost'
assignee = 'none'
closed = True
closed_date = <Date 2019-05-13.09:32:50.700>
closer = 'SilentGhost'
components = ['Library (Lib)']
creation = <Date 2019-05-13.09:29:19.823>
creator = 'Piotr Kamoda'
dependencies = []
files = []
hgrepos = []
issue_num = 36899
keywords = []
message_count = 6.0
messages = ['342297', '342299', '342300', '342301', '342303', '342305']
nosy_count = 2.0
nosy_names = ['SilentGhost', 'Piotr Kamoda']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue36899'
versions = ['Python 3.7']

@PiotrKamoda
Copy link
Mannequin Author

PiotrKamoda mannequin commented May 13, 2019

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.

@PiotrKamoda PiotrKamoda mannequin added the 3.7 (EOL) end of life label May 13, 2019
@SilentGhost
Copy link
Mannequin

SilentGhost mannequin commented May 13, 2019

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.

@SilentGhost SilentGhost mannequin closed this as completed May 13, 2019
@SilentGhost SilentGhost mannequin added invalid type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels May 13, 2019
@PiotrKamoda
Copy link
Mannequin Author

PiotrKamoda mannequin commented May 13, 2019

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.

@SilentGhost
Copy link
Mannequin

SilentGhost mannequin commented May 13, 2019

In the same page about utcfromtimestamp https://docs.python.org/3.7/library/datetime.html#datetime.datetime.utcfromtimestamp it says:

with tzinfo None

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?

@PiotrKamoda
Copy link
Mannequin Author

PiotrKamoda mannequin commented May 13, 2019

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'

@SilentGhost
Copy link
Mannequin

SilentGhost mannequin commented May 13, 2019

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

0 participants