What is the correct way to get an UTC offset from a timezone in python? I need a function to send a pytz timezone and get the timezone offset ignoring the Daylight Saving Time.
import pytz
tz = pytz.timezone('Europe/Madrid')
getOffset(tz) #datetime.timedelta(0, 3600)
pytz timezone objects obey tzinfo API specification defined in the datetime module. Therefore you can use their .utcoffset() and .dst() methods:
timestamp = datetime(2009, 1, 1) # any unambiguous timestamp will work here
def getOffset(tz):
return tz.utcoffset(timestamp) - tz.dst(timestamp)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With