]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
authorGeorg Brandl <georg@python.org>
Fri, 28 Apr 2006 19:09:29 +0000 (19:09 +0000)
committerGeorg Brandl <georg@python.org>
Fri, 28 Apr 2006 19:09:29 +0000 (19:09 +0000)
possibly "rounding up" to the next whole second.
 (backport from rev. 45792)

Lib/test/test_datetime.py
Modules/datetimemodule.c

index 382ee74cde5ee22c783ad0cff499b05cba2e71a9..99e1c4bcb38b230a2733ee14d96b761ea085ed9f 100644 (file)
@@ -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
index da61d4bca5d9fe0397b0037b901810868d501f60..e5aa73eb2579793a17557d33fb635d6bd33f41b2 100644 (file)
@@ -3682,6 +3682,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);
 }