从python中取消时区的时区忽略了DST

发布于 2025-02-07 07:12:01 字数 188 浏览 2 评论 0原文

从Python的时区获得UTC偏移的正确方法是什么? 我需要一个函数来发送PYTZ时区,并获得时区偏移,而忽略了节目的节省时间。

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路弥 2025-02-14 07:12:01

pytz时区对象obey tzinfo API规范dateTime模块中定义。因此,您可以使用他们的.utcoffset().dst()方法:

timestamp = datetime(2009, 1, 1)  # any unambiguous timestamp will work here

def getOffset(tz):
    return tz.utcoffset(timestamp) - tz.dst(timestamp)

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)
暗喜 2025-02-14 07:12:01

还可以很好地与Python 3.9's directInInfo.ZoneInfo.ZoneInfo

from datetime import datetime, timedelta
from zoneinfo import ZoneInfo

def utc_offset_no_dst(dt: datetime, tz: ZoneInfo) -> timedelta:
    return tz.utcoffset(dt) - tz.dst(dt)

z = "Europe/Berlin" # +1 (DST=0) / +2 (DST=1)
tz = ZoneInfo(z)

for dt in datetime(2022,1,15), datetime(2022,6,15):
    print(utc_offset_no_dst(dt, tz))
    
# 1:00:00
# 1:00:00

also works nicely with Python 3.9 's zoneinfo.ZoneInfo:

from datetime import datetime, timedelta
from zoneinfo import ZoneInfo

def utc_offset_no_dst(dt: datetime, tz: ZoneInfo) -> timedelta:
    return tz.utcoffset(dt) - tz.dst(dt)

z = "Europe/Berlin" # +1 (DST=0) / +2 (DST=1)
tz = ZoneInfo(z)

for dt in datetime(2022,1,15), datetime(2022,6,15):
    print(utc_offset_no_dst(dt, tz))
    
# 1:00:00
# 1:00:00
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文