Reputation: 1575
I have a python datetime
object in a database, and it is stored in UTC timezone:
2012-10-24 14:10:00+00:00
I am able to convert it to local time, using
start_localtime = start.replace(tzinfo=tz.gettz('Europe/Zurich'))
start_localtime is now: 2012-10-24 14:10:00+02:00
Which is fine, technically. But I need the timezone be +00:00
, so the hour would be 16 (+2 hours).
Is there a way to do it?
Upvotes: 0
Views: 1066
Reputation: 515
Have you tried using datetime.astimezone()?
http://docs.python.org/2/library/datetime.html#datetime.datetime.astimezone
I figured out a way to do it using the pytz library: http://pypi.python.org/pypi/pytz/
>>> import pytz
>>> from pytz import timezone
>>> a = datetime.now(timezone("UTC"))
>>> b = a.astimezone(timezone("Europe/Zurich"))
>>> a
datetime.datetime(2012, 12, 12, 10, 39, 20, 158993, tzinfo=<UTC>)
>>> b
datetime.datetime(2012, 12, 12, 11, 39, 20, 158993, tzinfo=<DstTzInfo 'Europe/Zurich' CET+1:00:00 STD>)
>>> b.utcoffset()
datetime.timedelta(0, 3600)
Upvotes: 2