Skip to content

Commit a15dfbb

Browse files
committed
Try to fix the same problem in windows
Previous commit doesn't pass on Windows: it looks like window's floor() has an integer overflow.
1 parent 14fe3ad commit a15dfbb

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

psycopg/typecast_datetime.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ typecast_PYTIME_cast(const char *str, Py_ssize_t len, PyObject *curs)
220220
static PyObject *
221221
typecast_PYINTERVAL_cast(const char *str, Py_ssize_t len, PyObject *curs)
222222
{
223-
long years = 0, months = 0, days = 0, sec;
223+
long years = 0, months = 0, days = 0, lsec;
224224
double hours = 0.0, minutes = 0.0, seconds = 0.0, hundredths = 0.0;
225225
double v = 0.0, sign = 1.0, denominator = 1.0;
226226
int part = 0;
@@ -317,10 +317,10 @@ typecast_PYINTERVAL_cast(const char *str, Py_ssize_t len, PyObject *curs)
317317
/* calculates days */
318318
days += years*365 + months*30;
319319

320-
micro = (seconds - floor(seconds)) * 1000000.0;
321-
sec = (long)floor(seconds);
320+
lsec = (long)seconds;
321+
micro = (seconds - (double)lsec) * 1000000.0;
322322
return PyObject_CallFunction((PyObject*)PyDateTimeAPI->DeltaType, "lli",
323-
days, sec, (int)round(micro));
323+
days, lsec, (int)round(micro));
324324
}
325325

326326
/* psycopg defaults to using python datetime types */

tests/test_dates.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,27 @@ def test_time_24(self):
334334
self.assertEqual(t, time(0, 0, tzinfo=FixedOffsetTimezone(330)))
335335

336336
def test_large_interval(self):
337+
def total_seconds(d):
338+
"""Return total number of seconds of a timedelta as a float."""
339+
return d.days * 24 * 60 * 60 + d.seconds + d.microseconds / 1000000.0
340+
337341
t = self.execute("select '999999:00:00'::interval")
338-
self.assertEqual(t.days * 24 + t.seconds / 60.0 / 60, 999999)
342+
self.assertEqual(total_seconds(t), 999999 * 60 * 60)
343+
344+
t = self.execute("select '-999999:00:00'::interval")
345+
self.assertEqual(total_seconds(t), -999999 * 60 * 60)
346+
347+
t = self.execute("select '999999:00:00.1'::interval")
348+
self.assertEqual(total_seconds(t), 999999 * 60 * 60 + 0.1)
349+
350+
t = self.execute("select '999999:00:00.9'::interval")
351+
self.assertEqual(total_seconds(t), 999999 * 60 * 60 + 0.9)
352+
353+
t = self.execute("select '-999999:00:00.1'::interval")
354+
self.assertEqual(total_seconds(t), -999999 * 60 * 60 - 0.1)
355+
356+
t = self.execute("select '-999999:00:00.9'::interval")
357+
self.assertEqual(total_seconds(t), -999999 * 60 * 60 - 0.9)
339358

340359

341360
# Only run the datetime tests if psycopg was compiled with support.

0 commit comments

Comments
 (0)