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

编程入门 行业动态 更新时间:2024-10-26 10:39:33
本文介绍了如何确定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中的特定位置开始或结束?

本文发布于:2023-10-08 03:05:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471321.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   结束   DST   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!