]> granicus.if.org Git - python/commitdiff
Patch #1646728: datetime.fromtimestamp fails with negative
authorGeorg Brandl <georg@python.org>
Tue, 6 Mar 2007 17:46:17 +0000 (17:46 +0000)
committerGeorg Brandl <georg@python.org>
Tue, 6 Mar 2007 17:46:17 +0000 (17:46 +0000)
fractional times.  With unittest.
  (backport from rev. 54167 by Guido)

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

index 436cfcad7508bc055bcf0f69168554df000bacee..f2f3a87478f924fcdf8682c64be1c14ce692b706 100644 (file)
@@ -1425,6 +1425,15 @@ class TestDateTime(TestDate):
             self.assertRaises(ValueError, self.theclass.utcfromtimestamp,
                               insane)
 
+    def test_negative_float_fromtimestamp(self):
+        # The result is tz-dependent; at least test that this doesn't
+        # fail (like it did before bug 1646728 was fixed).
+        self.theclass.fromtimestamp(-1.05)
+
+    def test_negative_float_utcfromtimestamp(self):
+        d = self.theclass.utcfromtimestamp(-1.05)
+        self.assertEquals(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000))
+
     def test_utcnow(self):
         import time
 
index 3cefca9fcf059b2835b7e681d0e3edca01a88789..c51b690d42c4d29b476837d3beb6e35f2de86972 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -118,6 +118,9 @@ Core and builtins
 Extension Modules
 -----------------
 
+- Patch #1646728: datetime.fromtimestamp fails with negative
+  fractional times.  With unittest.
+
 - Patch #1494140: Add documentation for the new struct.Struct object.
 
 - Patch #1657276: Make NETLINK_DNRTMSG conditional.
index 9ae77326b663505ad470ee6101f34c66ff0a84f9..8207ffd0c9b22217b7acb5922426e8622ef7f811 100644 (file)
@@ -3686,6 +3686,12 @@ datetime_from_timestamp(PyObject *cls, TM_FUNC f, double timestamp,
                return NULL;
        fraction = timestamp - (double)timet;
        us = (int)round_to_long(fraction * 1e6);
+       if (us < 0) {
+               /* Truncation towards zero is not what we wanted
+                  for negative numbers (Python's mod semantics) */
+               timet -= 1;
+               us += 1000000;
+       }
        /* If timestamp is less than one microsecond smaller than a
         * full second, round up. Otherwise, ValueErrors are raised
         * for some floats. */