How can I get UTC offset from time zone name in python?
For example: I have Asia/Jerusalem
and I want to get +0200
Advertisement
Answer
Because of DST (Daylight Saving Time), the result depends on the time of the year:
JavaScript
x
11
11
1
import datetime, pytz
2
3
datetime.datetime.now(pytz.timezone('Asia/Jerusalem')).strftime('%z')
4
5
# returns '+0300' (because 'now' they have DST)
6
7
8
pytz.timezone('Asia/Jerusalem').localize(datetime.datetime(2011,1,1)).strftime('%z')
9
10
# returns '+0200' (because in January they didn't have DST)
11