Both datetime.now() and datetime.utcnow() return a datetime object withouttzinfo. As such .astimezone() has no reference timezone to convert from when applying the desired timezone, and assumes the datetime object to be in local time. In this instance .astimezone() assumes the result from .utcnow() to be in local time, hence performs no conversion (in the case where city matches the local timezone) leading to localtime() returning unmodified UTC time disguised as a different timezone.
For this reason the documentation on datetime.utcnow() recommends to use datetime.now() with an explicit timezone argument like timezone.utc or in the case of this PR, the desired timezone directly.
Fixes #72, CC @fritzzjan
Both
datetime.now()
anddatetime.utcnow()
return adatetime
object withouttzinfo
. As such.astimezone()
has no reference timezone to convert from when applying the desired timezone, and assumes thedatetime
object to be in local time. In this instance.astimezone()
assumes the result from.utcnow()
to be in local time, hence performs no conversion (in the case wherecity
matches the local timezone) leading tolocaltime()
returning unmodified UTC time disguised as a different timezone.For this reason the documentation on
datetime.utcnow()
recommends to usedatetime.now()
with an explicit timezone argument liketimezone.utc
or in the case of this PR, the desired timezone directly.