Skip to content

Commit 9736493

Browse files
miss-islingtonammaraskar
authored andcommitted
bpo-29097: Forego fold detection on windows for low timestamp values (GH-2385) (GH-8466)
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. (cherry picked from commit 96d1e69) Co-authored-by: Ammar Askar <ammar_askar@hotmail.com>
1 parent ec02c58 commit 9736493

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

Lib/datetime.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import time as _time
88
import math as _math
9+
import sys
910

1011
def _cmp(x, y):
1112
return 0 if x == y else 1 if x > y else -1
@@ -1572,6 +1573,14 @@ def _fromtimestamp(cls, t, utc, tz):
15721573
# 23 hours at 1969-09-30 13:00:00 in Kwajalein.
15731574
# Let's probe 24 hours in the past to detect a transition:
15741575
max_fold_seconds = 24 * 3600
1576+
1577+
# On Windows localtime_s throws an OSError for negative values,
1578+
# thus we can't perform fold detection for values of time less
1579+
# than the max time fold. See comments in _datetimemodule's
1580+
# version of this method for more details.
1581+
if t < max_fold_seconds and sys.platform.startswith("win"):
1582+
return result
1583+
15751584
y, m, d, hh, mm, ss = converter(t - max_fold_seconds)[:6]
15761585
probe1 = cls(y, m, d, hh, mm, ss, us, tz)
15771586
trans = result - probe1 - timedelta(0, max_fold_seconds)

Lib/test/datetimetester.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_name_cleanup(self):
7070
if not name.startswith('__') and not name.endswith('__'))
7171
allowed = set(['MAXYEAR', 'MINYEAR', 'date', 'datetime',
7272
'datetime_CAPI', 'time', 'timedelta', 'timezone',
73-
'tzinfo'])
73+
'tzinfo', 'sys'])
7474
self.assertEqual(names - allowed, set([]))
7575

7676
def test_divide_and_round(self):
@@ -4955,6 +4955,11 @@ def test_fromtimestamp_lord_howe(self):
49554955
self.assertEqual(t0.fold, 0)
49564956
self.assertEqual(t1.fold, 1)
49574957

4958+
def test_fromtimestamp_low_fold_detection(self):
4959+
# Ensure that fold detection doesn't cause an
4960+
# OSError for really low values, see bpo-29097
4961+
self.assertEqual(datetime.fromtimestamp(0).fold, 0)
4962+
49584963
@support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0')
49594964
def test_timestamp(self):
49604965
dt0 = datetime(2014, 11, 2, 1, 30)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix bug where :meth:`datetime.fromtimestamp` erronously throws an
2+
:exc:`OSError` on Windows for values between 0 and 86400.
3+
Patch by Ammar Askar.

Modules/_datetimemodule.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4625,7 +4625,22 @@ 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.
4639+
*/
4640+
#ifdef MS_WINDOWS
4641+
&& (timet - max_fold_seconds > 0)
4642+
#endif
4643+
) {
46294644
long long probe_seconds, result_seconds, transition;
46304645

46314646
result_seconds = utc_to_seconds(year, month, day,

0 commit comments

Comments
 (0)