[Python-checkins] r45793 - in python/branches/release24-maint: Lib/test/test_datetime.py Modules/datetimemodule.c

georg.brandl python-checkins at python.org
Fri Apr 28 21:09:29 CEST 2006


Author: georg.brandl
Date: Fri Apr 28 21:09:29 2006
New Revision: 45793

Modified:
   python/branches/release24-maint/Lib/test/test_datetime.py
   python/branches/release24-maint/Modules/datetimemodule.c
Log:
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
possibly "rounding up" to the next whole second.
 (backport from rev. 45792)

Modified: python/branches/release24-maint/Lib/test/test_datetime.py
==============================================================================
--- python/branches/release24-maint/Lib/test/test_datetime.py	(original)
+++ python/branches/release24-maint/Lib/test/test_datetime.py	Fri Apr 28 21:09:29 2006
@@ -1400,6 +1400,12 @@
         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

Modified: python/branches/release24-maint/Modules/datetimemodule.c
==============================================================================
--- python/branches/release24-maint/Modules/datetimemodule.c	(original)
+++ python/branches/release24-maint/Modules/datetimemodule.c	Fri Apr 28 21:09:29 2006
@@ -3682,6 +3682,13 @@
 		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);
 }
 


More information about the Python-checkins mailing list