【问题标题】:Getting the timezone offset in Python from a timezone ignoring DST从忽略 DST 的时区获取 Python 中的时区偏移量
【发布时间】:2016-12-07 02:35:25
【问题描述】:

在 python 中从时区获取 UTC 偏移量的正确方法是什么? 我需要一个函数来发送 pytz 时区并获取忽略夏令时的时区偏移量。

import pytz
tz = pytz.timezone('Europe/Madrid')
getOffset(tz) #datetime.timedelta(0, 3600)

【问题讨论】:

    标签: python datetime timezone


    【解决方案1】:

    pytz 时区对象遵循datetime 模块中定义的tzinfo API specification。因此您可以使用他们的.utcoffset().dst() 方法:

    timestamp = datetime(2009, 1, 1)  # any unambiguous timestamp will work here
    
    def getOffset(tz):
        return tz.utcoffset(timestamp) - tz.dst(timestamp)
    

    【讨论】:

    • 很好的答案。要注意为什么仍然需要timestamp,请考虑多年来许多时区已更改其标准时间。
    • 真的很有用。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 2012-06-05
    • 2011-08-04
    • 2012-09-24
    • 1970-01-01
    • 2011-07-29
    相关资源
    最近更新 更多