Skip to content

Commit ffde86a

Browse files
committed
Remove workarounds for windows not supporting negative timestamps
1 parent b3082b9 commit ffde86a

File tree

3 files changed

+6
-26
lines changed

3 files changed

+6
-26
lines changed

Lib/_pydatetime.py

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

99
import time as _time
1010
import math as _math
11-
import sys
1211
from operator import index as _index
1312

1413
def _cmp(x, y):
@@ -1849,13 +1848,6 @@ def _fromtimestamp(cls, t, utc, tz):
18491848
# Let's probe 24 hours in the past to detect a transition:
18501849
max_fold_seconds = 24 * 3600
18511850

1852-
# On Windows localtime_s throws an OSError for negative values,
1853-
# thus we can't perform fold detection for values of time less
1854-
# than the max time fold. See comments in _datetimemodule's
1855-
# version of this method for more details.
1856-
if t < max_fold_seconds and sys.platform.startswith("win"):
1857-
return result
1858-
18591851
y, m, d, hh, mm, ss = converter(t - max_fold_seconds)[:6]
18601852
probe1 = cls(y, m, d, hh, mm, ss, us, tz)
18611853
trans = result - probe1 - timedelta(0, max_fold_seconds)

Lib/test/datetimetester.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,6 +2727,11 @@ def test_timestamp_limits(self):
27272727
# If that assumption changes, this value can change as well
27282728
self.assertEqual(max_ts, 253402300799.0)
27292729

2730+
def test_fromtimestamp_roundtrip_near_epoch(self):
2731+
for ts in range(0, 1, 2):
2732+
roundtripped_ts = self.theclass.fromtimestamp(ts).timestamp()
2733+
self.assertEqual(roundtripped_ts, ts)
2734+
27302735
def test_fromtimestamp_limits(self):
27312736
try:
27322737
# See if the platform can handle timestamps that are near the min.
@@ -2829,13 +2834,11 @@ def test_insane_utcfromtimestamp(self):
28292834
self.assertRaises(OverflowError, self.theclass.utcfromtimestamp,
28302835
insane)
28312836

2832-
@unittest.skipIf(sys.platform == "win32", "Windows doesn't accept negative timestamps")
28332837
def test_negative_float_fromtimestamp(self):
28342838
# The result is tz-dependent; at least test that this doesn't
28352839
# fail (like it did before bug 1646728 was fixed).
28362840
self.theclass.fromtimestamp(-1.05)
28372841

2838-
@unittest.skipIf(sys.platform == "win32", "Windows doesn't accept negative timestamps")
28392842
def test_negative_float_utcfromtimestamp(self):
28402843
with self.assertWarns(DeprecationWarning):
28412844
d = self.theclass.utcfromtimestamp(-1.05)

Modules/_datetimemodule.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5460,22 +5460,7 @@ datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us,
54605460
second = Py_MIN(59, tm.tm_sec);
54615461

54625462
/* local timezone requires to compute fold */
5463-
if (tzinfo == Py_None && f == _PyTime_localtime
5464-
/* On Windows, passing a negative value to local results
5465-
* in an OSError because localtime_s on Windows does
5466-
* not support negative timestamps. Unfortunately this
5467-
* means that fold detection for time values between
5468-
* 0 and max_fold_seconds will result in an identical
5469-
* error since we subtract max_fold_seconds to detect a
5470-
* fold. However, since we know there haven't been any
5471-
* folds in the interval [0, max_fold_seconds) in any
5472-
* timezone, we can hackily just forego fold detection
5473-
* for this time range.
5474-
*/
5475-
#ifdef MS_WINDOWS
5476-
&& (timet - max_fold_seconds > 0)
5477-
#endif
5478-
) {
5463+
if (tzinfo == Py_None && f == _PyTime_localtime) {
54795464
long long probe_seconds, result_seconds, transition;
54805465

54815466
result_seconds = utc_to_seconds(year, month, day,

0 commit comments

Comments
 (0)