Skip to content

Commit 7453c3c

Browse files
committed
Support negative integer timestamps too; skip tests that would generate negative timestamps.
1 parent a3e5749 commit 7453c3c

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

Lib/test/datetimetester.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2730,6 +2730,7 @@ def test_timestamp_limits(self):
27302730
# If that assumption changes, this value can change as well
27312731
self.assertEqual(max_ts, 253402300799.0)
27322732

2733+
@unittest.skipIf(sys.platform == "win32", "Windows can't generate negative timestamps")
27332734
def test_fromtimestamp_limits(self):
27342735
try:
27352736
self.theclass.fromtimestamp(-2**32 - 1)
@@ -2769,6 +2770,7 @@ def test_fromtimestamp_limits(self):
27692770
# OverflowError, especially on 32-bit platforms.
27702771
self.theclass.fromtimestamp(ts)
27712772

2773+
@unittest.skipIf(sys.platform == "win32", "Windows can't generate negative timestamps")
27722774
def test_utcfromtimestamp_limits(self):
27732775
with self.assertWarns(DeprecationWarning):
27742776
try:

Modules/_datetimemodule.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5548,23 +5548,19 @@ datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp,
55485548
time_t timet;
55495549
long us;
55505550

5551-
#ifdef MS_WINDOWS
5552-
if (PyFloat_Check(timestamp) && PyFloat_AsDouble(timestamp) < 0) {
5553-
if (_PyTime_ObjectToTimeval(timestamp,
5554-
&timet, &us, _PyTime_ROUND_HALF_EVEN) == -1)
5555-
return NULL;
5551+
if (_PyTime_ObjectToTimeval(timestamp,
5552+
&timet, &us, _PyTime_ROUND_HALF_EVEN) == -1)
5553+
return NULL;
55565554

5555+
#ifdef MS_WINDOWS
5556+
if (timet < 0) {
55575557
int normalize = 1, factor = 1;
55585558
PyObject *dt = datetime_from_timet_and_us(cls, f, 0, 0, tzinfo);
55595559
PyObject *delta = new_delta(0, (int)timet, us, normalize);
55605560
return add_datetime_timedelta(PyDateTime_CAST(dt), PyDelta_CAST(delta), factor);
55615561
}
55625562
#endif
55635563

5564-
if (_PyTime_ObjectToTimeval(timestamp,
5565-
&timet, &us, _PyTime_ROUND_HALF_EVEN) == -1)
5566-
return NULL;
5567-
55685564
return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo);
55695565
}
55705566

0 commit comments

Comments
 (0)