Is there a shortcut for the pieces of code described below:
if value.tzinfo is None:
value = default_tzinfo.localize(value)
new_value = value.astimezone(default_tzinfo)
else:
new_value = value
I tried to use datetime.replace() but returns GMT -8 instead of -7 when I use US/Pacific time zone.
See: http://wwp.greenwichmeantime.com/time-zone/usa/pacific-time/pacific-daylight-time.htm
value = value.replace(tzinfo=default_tzinfo)
Example output:
2012-05-06 13:12:45-08:00
US/Pacific
---
2012-05-07 00:12:45+03:00
Europe/Istanbul
The answer is given in the pytz documentation: http://pytz.sourceforge.net/#localized-times-and-date-arithmetic
Once you’ve used
localizeyou should have a datetime with the proper timezone. The call toastimezoneis redundant and may be messing you up.The time zone needs to know the date and time so that it can adjust itself for daylight savings transitions. That’s why
replacedoesn’t work.