I store my datetime int UTC like so:
import pytz, datetime
timeUTC = datetime.datetime(2013, 5, 23, 19, 27, 50, 0)
timezoneLocal = pytz.timezone('Europe/Vilnius')
timeLocal = timezoneLocal.localize(timeUTC)
But when I try to print it, it just gives me regular UTC hours
>>> timeLocal.strftime('%H:%M:%S')
'19:27:50'
I would expect this to return '22:27:50'
since this is the local time (pytz.timezone('Europe/Vilnius')
is +3 at the moment). What am I missing here?
Localize the date string as a UTC datetime, then use astimezone
to convert it to the local timezone.
import pytz, datetime
timeUTC = datetime.datetime(2013, 5, 23, 19, 27, 50, 0)
timezoneLocal = pytz.timezone('Europe/Vilnius')
utc = pytz.utc
timeLocal = utc.localize(timeUTC).astimezone(timezoneLocal)
print(timeLocal)
# 2013-05-23 22:27:50+03:00
localize
does not convert datetimes, it interprets the date string as though it were written in that timezone. localize
builds a timezone-aware datetime out of a naive datetime (such as timeUTC
). astimezone
converts timezone-aware datetimes to other timezones.