Python 3.6 Local Time Value (PEP 495)

I have some questions regarding PEP 495.

Classes in a module datetime

(well, some of them) now take an argument fold

, which 0

is the default, and this value can also be set to 1

.

Eg datetime.datetime(..., fold=1)

.

In my country (Slovenia, Central Europe) we set the time one hour forward and one hour backward from 2 am to 3 pm. I think in other countries it is between 1 and 2 o'clock.

1st question: is this enough fold

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?

2nd question: so the setting fold

in 1

takes into account daylight saving time, right? Is this what he does?

Third question: is my understanding of the argument correct fold

?

+3


source to share


1 answer


What is a fold?

Complexity is local time that is ambiguous. This happens when the clock moves back. Take the following time in Germany as an example:

On October 29, 2017, at 3 pm, the clock will be set to 2 am.

Now, imagine you are talking to someone you would like to meet on October 29, 2017 at 2:30 am. Do you mean before or after the time change? This is controversial, because there are two times when the clock shows this exact time.

The fold attribute, which PEP 495 adds, provides exactly this information. This is 0 for the time before the time change and 1 for the time after it.

This is how it is described in PEP 495 :

This PEP adds a new attribute fold to instances of the datetime.time and datetime.datetime classes that can be used to distinguish between two points in time for which the local times are the same. The allowed values ​​for the fold attribute are 0 and 1, with 0 corresponding to the previous one and 1 the last of two possible readings of ambiguous local time.

Summer time and datetime

objects

From the python datetime docs:

There are two types of date and time objects: "naive" and "lucid".

The known object has sufficient knowledge of the applicable algorithmic and political time adjustments, such as time zone information and daylight saving time, to find itself relative to other familiar objects. A helper object is used to represent a specific point in time that is not open for interpretation [1].

A naive object does not contain enough information to uniquely find itself relative to other date / time objects. Whether the naive object is Coordinated Universal Time (UTC), local time, or time in some other time zone depends only on the program, just as it depends on whether a particular number is meters, miles, or mass. Naive objects are easy to understand and work with, at the cost of ignoring some aspects of reality.

For applications that require object objects, datetime and time objects have an additional time zone information attribute, tzinfo, that can be set to an instance of a subclass of the abstract tzinfo class. These tzinfo objects record the offset from UTC, the time zone name, and whether daylight saving time is in effect. Note that only one particular tzinfo class, the timezone class, is provided by the datetime module. The time zone class can be simple time slots with a fixed offset from UTC, such as UTC or North American EST and EDT.Support for time zones at deeper levels of granularity is application dependent. Time regulation rules around the world are more political than rational, change frequently, and there is no standard suitable for every application other than UTC.

TL; DR:



The standard library provides:

  • Unambiguous local times (using the fold argument for times that would otherwise be ambiguous).
  • An attribute tzinfo

    for objects datetime

    that can be used to implement time zone and daylight saving time information.

The standard library does not provide:

  • Convert time between different time zones that know about daylight saving time.

Answering your questions

Is this fold smart enough to determine if the daytime is set between 2:00 and 3:00 or if it is set between 1 AM and 2 AM?

No, it doesn't, even the python standard library doesn't provide this. I'm sure there are third party libraries that can do this, but I still haven't used them.

So setting fold to 1 allows for daylight saving time, right? Is this what he does?

Not really. Fold is more like "this is the first or second time the clock shows this time". This argument is only needed for (usually) one hour when the clock goes backwards. Except for this case, the local time is unambiguous, so you don't need the fold argument. Whether or not it is daylight saving time for an argument.

Is my understanding of the fold argument even correct?

Not really, but I hope I can shed some light on my answers above.

+2


source







All Articles