[Python-checkins] r54167 - in python/trunk: Lib/test/test_datetime.py Misc/NEWS Modules/datetimemodule.c

guido.van.rossum python-checkins at python.org
Tue Mar 6 16:50:05 CET 2007


Author: guido.van.rossum
Date: Tue Mar  6 16:50:01 2007
New Revision: 54167

Modified:
   python/trunk/Lib/test/test_datetime.py
   python/trunk/Misc/NEWS
   python/trunk/Modules/datetimemodule.c
Log:
Patch #1646728: datetime.fromtimestamp fails with negative
fractional times.  With unittest.

Somebody please backport to 2.5.


Modified: python/trunk/Lib/test/test_datetime.py
==============================================================================
--- python/trunk/Lib/test/test_datetime.py	(original)
+++ python/trunk/Lib/test/test_datetime.py	Tue Mar  6 16:50:01 2007
@@ -1425,6 +1425,15 @@
             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
 

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Tue Mar  6 16:50:01 2007
@@ -404,6 +404,9 @@
 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.
 

Modified: python/trunk/Modules/datetimemodule.c
==============================================================================
--- python/trunk/Modules/datetimemodule.c	(original)
+++ python/trunk/Modules/datetimemodule.c	Tue Mar  6 16:50:01 2007
@@ -3683,6 +3683,12 @@
 		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. */


More information about the Python-checkins mailing list