Get UTC offset from time zone name in python
How can I get UTC offset from time zone name in python?
For example: I have Asia/Jerusalem
and I want to get +0200
Because of DST (Daylight Saving Time), the result depends on the time of the year:
import datetime, pytz
datetime.datetime.now(pytz.timezone('Asia/Jerusalem')).strftime('%z')
# returns '+0300' (because 'now' they have DST)
pytz.timezone('Asia/Jerusalem').localize(datetime.datetime(2011,1,1)).strftime('%z')
# returns '+0200' (because in January they didn't have DST)
Have you tried using the pytz project and the utcoffset
method?
e.g.
>>> import datetime
>>> import pytz
>>> pacific_now = datetime.datetime.now(pytz.timezone('US/Pacific'))
>>> pacific_now.utcoffset().total_seconds()/60/60
-7.0