我正在寻找一种方法来确定我正在处理的 Python 脚本中给定时区的 DST 何时开始或结束。
我知道pytz可以将我正在工作的 UTC 日期转换为本地时间,并将 DST 考虑在内,但是对于这个特定的应用程序,我需要知道转换的点。
有什么建议么?
您可以查看_utc_transition_times
您正在使用的时区的成员。
>>> from pytz import timezone
>>> tz = timezone("Europe/Paris")
>>> print tz._utc_transition_times
[datetime.datetime(1, 1, 1, 0, 0), datetime.datetime(1911, 3, 10, 23, 51, 39), datetime.datetime(1916, 6, 14, 23, 0), datetime.datetime(1916, 10, 1, 23, 0), date
....
datetime.datetime(2037, 3, 29, 1, 0), datetime.datetime(2037, 10, 25, 1, 0)]
它将为您提供 DST 更改日期的列表(DST 的开始和结束)。
根据tzinfo.py的代码
class DstTzInfo(BaseTzInfo):
'''A timezone that has a variable offset from UTC
The offset might change if daylight savings time comes into effect,
or at a point in history when the region decides to change their
timezone definition.
'''
# Overridden in subclass
_utc_transition_times = None # Sorted list of DST transition times in UTC
_transition_info = None # [(utcoffset, dstoffset, tzname)] corresponding
# to _utc_transition_times entries
因此,如果您将其与您混合使用_utc_transition_times
,_transition_info
您将获取所有需要的信息、日期、时间和偏移量以应用;)