Python 3.6+ 本地时间消歧(PEP 495)

Python 3.6+ Local Time Disambiguation (PEP 495)

我对 PEP 495 有一些疑问。

datetime 模块中的 类(嗯,其中一些)现在接受一个 fold 参数,默认值为 0,这个值也可以设置为 1.

例如,datetime.datetime(..., fold=1).

在我的国家(斯洛文尼亚,中欧),我们在凌晨 2 点到凌晨 3 点之间将时间提前一小时和推迟一小时。我想在其他国家/地区是凌晨 1 点到凌晨 2 点之间。

第一个问题:这个 fold 是否足够智能,可以确定夏令时是设置在凌晨 2 点到凌晨 3 点之间,还是设置在凌晨 1 点到凌晨 2 点之间?

第二个问题:所以把fold设置成1考虑到了夏令时,对吧?这是它的作用吗?

第三个问题:我对fold论点的理解是否正确?

什么是弃牌?

折叠是一个模糊的本地时间。每当时钟向后移动时都会发生这种情况。以德国的下一个时间变化为例:

On the 29th of October 2017 at 3 AM, the clocks will be set back to 2 AM.

现在想象一下,你告诉某人你想在 2017 年 10 月 29 日 2:30 上午见面。你是说时间变化发生之前还是之后?这是不明确的,因为有两个时间点的时钟显示这个确切时间。

PEP 495 添加的折叠属性正好提供了该信息。在之前时间更改为0,在之后时间更改为1。

PEP 495中是这样描述的:

This PEP adds a new attribute fold to instances of the datetime.time and datetime.datetime classes that can be used to differentiate between two moments in time for which local times are the same. The allowed values for the fold attribute will be 0 and 1 with 0 corresponding to the earlier and 1 to the later of the two possible readings of an ambiguous local time.

夏令时和 datetime 个对象

来自 python datetime 文档:

There are two kinds of date and time objects: “naive” and “aware”.

An aware object has sufficient knowledge of applicable algorithmic and political time adjustments, such as time zone and daylight saving time information, to locate itself relative to other aware objects. An aware object is used to represent a specific moment in time that is not open to interpretation [1].

A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it is up to the program whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality.

For applications requiring aware objects, datetime and time objects have an optional time zone information attribute, tzinfo, that can be set to an instance of a subclass of the abstract tzinfo class. These tzinfo objects capture information about the offset from UTC time, the time zone name, and whether Daylight Saving Time is in effect. Note that only one concrete tzinfo class, the timezone class, is supplied by the datetime module. The timezone class can represent simple timezones with fixed offset from UTC, such as UTC itself or North American EST and EDT timezones. Supporting timezones at deeper levels of detail is up to the application. The rules for time adjustment across the world are more political than rational, change frequently, and there is no standard suitable for every application aside from UTC.

长话短说:

标准库提供:

  • 明确的本地时间(使用 fold 参数表示否则会不明确的时间)。
  • datetime 对象的 tzinfo 属性,可用于实现时区和夏令时信息。

标准库没有提供:

  • 可识别夏令时的不同时区之间的时间转换。

回答您的问题

Is this fold smart enough to determine if daylight-saving time is set between 2 AM and 3 AM or if it is set between 1 AM and 2 AM?

不,不是,甚至 python 标准库也没有提供这个。我确信有第三方库可以做到这一点,但直到现在我还没有使用过它们。

So setting fold to 1 takes into account the daylight-saving time, right? Is that what it does?

不是真的。 fold参数的意思更像是“这次时钟显示的是第一次还是第二次”。您只需要在时钟向后移动的(通常)一个小时内使用此参数。除了这种情况,本地时间是明确的,所以你不需要 fold 参数。是否是夏令时与参数无关。

Is my understanding of the fold argument even correct?

不是真的,但我希望我上面的回答能阐明一些问题。