Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the timezone offset in Python from a timezone ignoring DST

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)
like image 239
iago1460 Avatar asked Apr 09 '26 08:04

iago1460


1 Answers

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)
like image 89
void Avatar answered Apr 10 '26 22:04

void