How to determine when DST starts or ends in a specific location in Python? [duplicate]

寵の児 提交于 2019-11-30 08:47:53

You could have a look to the _utc_transition_times memberof the timezone you're using.

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

It will give you the list of the DST change dates (start and end of DST).

According to the code of 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

So if you mix the _utc_transition_times with _transition_info you will grab all your needed informations, date, time and offset to apply ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!