有没有一种方法可以检查特定时区在我指定的日期是否处于夏令时?
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/