]> granicus.if.org Git - python/commitdiff
Patch #1646728: datetime.fromtimestamp fails with negative
authorGuido van Rossum <guido@python.org>
Tue, 6 Mar 2007 15:50:01 +0000 (15:50 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 6 Mar 2007 15:50:01 +0000 (15:50 +0000)
fractional times.  With unittest.

Somebody please backport to 2.5.

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

index 3aa0468837e60f287a5abfa9eb92b616d235f396..b0341a40da94092734bde6561de78e57c40d34b5 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 f97676166df7d898138d2c9adda5c4394b3597c5..cda539e63e267b2af676aea2b46fd6df9aa6e360 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -404,6 +404,9 @@ Library
 Extension Modules
 -----------------
 
+- Patch #1646728: datetime.fromtimestamp fails with negative
+  fractional times.  With unittest.
+
 - Patch #1490190: posixmodule now includes os.chflags() and os.lchflags()
   functions on platforms where the underlying system calls are available.
 
index cf8a68ba226e2e97f6d40d03022e93b1829a32ef..7487c507790cd2fdce8cb55e2f512ed57b177b21 100644 (file)
@@ -3683,6 +3683,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. */