在 python 中从时区获取 UTC 偏移量的正确方法是什么? 我需要一个函数来发送 pytz 时区并获取忽略夏令时的时区偏移量。
import pytz
tz = pytz.timezone('Europe/Madrid')
getOffset(tz) #datetime.timedelta(0, 3600)
最佳答案
pytz
时区对象服从 tzinfo
API specification在 datetime
模块中定义。因此,您可以使用它们的 .utcoffset()
和 .dst()
方法:
timestamp = datetime(2009, 1, 1) # any unambiguous timestamp will work here
def getOffset(tz):
return tz.utcoffset(timestamp) - tz.dst(timestamp)
关于python - 从忽略 DST 的时区获取 Python 中的时区偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38708572/