gpt4 book ai didi

python - 如何确定过去的时区特定日期是否在 python 中是夏令时?

转载 作者:太空狗 更新时间:2023-10-29 23:58:33 30 4
gpt4 key购买 nike

有没有一种方法可以检查特定时区在我指定的日期是否处于夏令时?

    test_dt = datetime(year=2015, month=2, day=1)
pst = pytz.timezone('America/Los_Angeles')
test_dt = pst.localize(test_dt)

# should return False
is_day_light_saving(test_dt)

最佳答案

只需调用 the datetime.dst() method :

def is_summer_time(aware_dt):
assert aware_dt.tzinfo is not None
assert aware_dt.tzinfo.utcoffset(aware_dt) is not None
return bool(aware_dt.dst())

例子:

#!/usr/bin/env python
from datetime import datetime
import pytz # $ pip install pytz

naive = datetime(2015, 2, 1)
pacific = pytz.timezone('America/Los_Angeles')
aware = pacific.localize(naive, is_dst=None)

print(is_summer_time(aware))

相当于:

bool(pytz.timezone('America/Los_Angeles').dst(datetime(2015, 2, 1), is_dst=None))

关于python - 如何确定过去的时区特定日期是否在 python 中是夏令时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31146092/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com