datetime.datetime.utcnow()
Why does this datetime not have any timezone info given that it is explicitly a UTC datetime?
I would expect that this would contain tzinfo.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That means it is timezone naive, so you can’t use it with
datetime.astimezoneyou can give it a timezone like this
now you can change timezones
To get the current time in a given timezone, you could pass tzinfo to
datetime.now()directly:It works for any timezone including those that observe daylight saving time (DST) i.e., it works for timezones that may have different utc offsets at different times (non-fixed utc offset). Don’t use
tz.localize(datetime.now())— it may fail during end-of-DST transition when the local time is ambiguous.