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

How to determine if a timezone specific date in the past is daylight saving or not in python?

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

    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))

根据我的经验,时区数据比日期时间更容易处理时区敏感 pandas.Timestamp()。我很确定时区敏感性从日期本身推断出夏令时。通过首先将日期时间转换为 numpy.datetime64,将日期时间转换为 pandas.timestamp() 是微不足道的。

Timestamp(numpy.datetime64('2012-05-01T01:00:00.000000'))

http://wesmckinney.com/blog/easy-high-performance-time-zone-handling-in-pandas-0-8-0/

Converting between datetime, Timestamp and datetime64

您也可以尝试查看 pandas 源代码并弄清楚它是如何推导出 tz 信息的。 https://github.com/pydata/pandas/blob/master/pandas/src/datetime/np_datetime.c