]> granicus.if.org Git - python/commitdiff
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
authorGeorg Brandl <georg@python.org>
Fri, 28 Apr 2006 19:09:24 +0000 (19:09 +0000)
committerGeorg Brandl <georg@python.org>
Fri, 28 Apr 2006 19:09:24 +0000 (19:09 +0000)
possibly "rounding up" to the next whole second.

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

index 2528b4a0299b8372b35e2bd2ebec7e7e0e393638..203bea150044e37ed11045f5312bd9accde16c44 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 b7bddff1c56f750fd02db40eea1447b614423990..648ebe5af346dcb3be99d4a8d5d70d23cdaa56ec 100644 (file)
@@ -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);
 }