diff --git a/ChangeLog b/ChangeLog index d80a3536..205c47a4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2010-05-07 Daniele Varrazzo + + * psycopg/adapter_datetime.c: Fixed TimestampFromTicks for second + values > 59.5. + + Bug reported and fixed by Jozsef Szalay on 2010-05-06 at 14:11:59.999920. + 2010-05-04 Daniele Varrazzo * Added typecasters for arrays of specific MX/Py time-related types. diff --git a/psycopg/adapter_datetime.c b/psycopg/adapter_datetime.c index 24d158cf..f6841634 100644 --- a/psycopg/adapter_datetime.c +++ b/psycopg/adapter_datetime.c @@ -435,7 +435,7 @@ psyco_TimestampFromTicks(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "d", &ticks)) return NULL; - t = (time_t)round(ticks); + t = (time_t)floor(ticks); ticks -= (double)t; if (localtime_r(&t, &tm)) { PyObject *value = Py_BuildValue("iiiiidO", diff --git a/tests/test_dates.py b/tests/test_dates.py index 1ffb15ae..e9977c84 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -478,6 +478,17 @@ if not hasattr(psycopg2._psycopg, 'MXDATETIME'): del mxDateTimeTests +class TimestampFromTicksTestCase(unittest.TestCase): + # bug "TimestampFromTicks() throws ValueError (2-2.0.14)" + # reported by Jozsef Szalay on 2010-05-06 + def test_value_error_sec_59_99(self): + from datetime import datetime + s = psycopg2.TimestampFromTicks(1273173119.99992) + self.assertEqual(s.adapted, + datetime(2010, 5, 6, 14, 11, 59, 999920, + tzinfo=FixedOffsetTimezone(-5 * 60))) + + def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__)