Skip to content

Commit 8f163d3

Browse files
ammaraskarAmmar Askar
authored andcommitted
Forego fold detection for timestamps in the range [0, 86399] on Windows.
On Windows, passing a negative value to local results in an OSError because localtime_s on Windows does not support negative timestamps. Unfortunately this means that fold detection for timestamps between 0 and max_fold_seconds will result in this OSError since we subtract max_fold_seconds from the timestamp to detect a fold. However, since we know there haven't been any folds in the interval [0, max_fold_seconds) in any timezone, we can hackily just forego fold detection for this time range on Windows.
1 parent 55e53c3 commit 8f163d3

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Modules/_datetimemodule.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4625,7 +4625,21 @@ datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us,
46254625
second = Py_MIN(59, tm.tm_sec);
46264626

46274627
/* local timezone requires to compute fold */
4628-
if (tzinfo == Py_None && f == _PyTime_localtime) {
4628+
if (tzinfo == Py_None && f == _PyTime_localtime
4629+
/* On Windows, passing a negative value to local results
4630+
* in an OSError because localtime_s on Windows does
4631+
* not support negative timestamps. Unfortunately this
4632+
* means that fold detection for time values between
4633+
* 0 and max_fold_seconds will result in an identical
4634+
* error since we subtract max_fold_seconds to detect a
4635+
* fold. However, since we know there haven't been any
4636+
* folds in the interval [0, max_fold_seconds) in any
4637+
* timezone, we can hackily just forego fold detection
4638+
* for this time range on Windows. */
4639+
#ifdef MS_WINDOWS
4640+
&& (timet - max_fold_seconds > 0)
4641+
#endif
4642+
) {
46294643
long long probe_seconds, result_seconds, transition;
46304644

46314645
result_seconds = utc_to_seconds(year, month, day,

0 commit comments

Comments
 (0)