diff options
-rw-r--r-- | Lib/test/test_datetime.py | 6 | ||||
-rw-r--r-- | Modules/datetimemodule.c | 7 |
2 files changed, 13 insertions, 0 deletions
diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py index 2528b4a..203bea1 100644 --- a/Lib/test/test_datetime.py +++ b/Lib/test/test_datetime.py @@ -1400,6 +1400,12 @@ class TestDateTime(TestDate): got = self.theclass.utcfromtimestamp(ts) self.verify_field_equality(expected, got) + def test_microsecond_rounding(self): + # Test whether fromtimestamp "rounds up" floats that are less + # than one microsecond smaller than an integer. + self.assertEquals(self.theclass.fromtimestamp(0.9999999), + self.theclass.fromtimestamp(1)) + def test_insane_fromtimestamp(self): # It's possible that some platform maps time_t to double, # and that this test will fail there. This test should diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c index b7bddff..648ebe5 100644 --- a/Modules/datetimemodule.c +++ b/Modules/datetimemodule.c @@ -3683,6 +3683,13 @@ datetime_from_timestamp(PyObject *cls, TM_FUNC f, double timestamp, return NULL; fraction = timestamp - (double)timet; us = (int)round_to_long(fraction * 1e6); + /* If timestamp is less than one microsecond smaller than a + * full second, round up. Otherwise, ValueErrors are raised + * for some floats. */ + if (us == 1000000) { + timet += 1; + us = 0; + } return datetime_from_timet_and_us(cls, f, timet, us, tzinfo); } |