如何确定 DST 何时在 Python 中的特定位置开始或结束?

2023-07-03Python开发问题
4

本文介绍了如何确定 DST 何时在 Python 中的特定位置开始或结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在寻找一种方法来确定我正在处理的 Python 脚本中给定时区的 DST 何时开始或结束.

I'm looking for a method of determining when DST starts or ends for a given timezone in a Python script I'm working on.

我知道 pytz 可以将我正在工作的 UTC 日期转换为本地时间,并且会考虑 DST,但是对于这个特定的应用程序 我需要知道转换点.

I know pytz can convert the UTC dates I'm working into localtime, and will take DST into account, however for this particular application I need to know the point of the changeover.

有什么建议吗?

推荐答案

你可以看看你正在使用的时区的 _utc_transition_times 成员.

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

它将为您提供 DST 更改日期列表(DST 的开始和结束).

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

根据tzinfo.py的代码

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

因此,如果您将 _utc_transition_times_transition_info 混合使用,您将获取所有需要的信息、日期、时间和偏移量来应用;)

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

这篇关于如何确定 DST 何时在 Python 中的特定位置开始或结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10