I have some questions regarding PEP 495.
The classes in the datetime
module (well, some of them) now accept a fold
argument which value is 0
by default and this value can also be set to 1
.
For example, datetime.datetime(..., fold=1)
.
In my country (Slovenia, Central Europe) we set our time one hour ahead and one hour back between 2 AM and 3 AM. In other countries it's between 1 AM and 2 AM, I think.
1st question: 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?
2nd question: So setting fold
to 1
takes into account the daylight-saving time, right? Is that what it does?
3rd question: Is my understanding of the fold
argument even correct?
datetime. utcnow() uses gettimeofday(2) or time. time() on Python 2 (and gmtime(3) to convert the result into broken-down time).
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.
One of the modules that helps you work with date and time in Python is datetime . With the datetime module, you can get the current date and time, or the current date and time in a particular time zone.
The min attribute specifies the minimum value (date and time) for a datetime field.
A fold is a local time that's ambiguous. This happens whenever the clock is moved back. Take the next time change in Germany as an example:
On the 29th of October 2017 at 3 AM, the clocks will be set back to 2 AM.
Now Imagine that you tell someone that you'd like to meet on Oct 29th 2017 at 2:30 AM. Do you mean before or after the time change happened? It's ambiguous because there are two points in time where the clocks show this exact time.
The fold attribute that PEP 495 adds provides exactly that information. It's 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 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
objectsFrom the python datetime docs:
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.
TL;DR:
The standard library provides:
tzinfo
attribute for datetime
objects that can be used to implement time zone and daylight saving time information.The standard library does not provide:
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?
No, it's not, not even the python standard library provides this. I'm sure that there are third-party libraries that can do this, but I haven't used any of them until now.
So setting fold to 1 takes into account the daylight-saving time, right? Is that what it does?
Not really. The meaning of the fold argument is more like "is this the first or the second time that the clock displays this time". You only need this argument for the (usually) one hour where the clocks are moved back. Except from this case, local time is unambiguous, so you don't need the fold argument. Whether or not it's daylight saving time isn't relevant for the argument.
Is my understanding of the fold argument even correct?
Not really, but I hope that I could shed some light with my answers above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With