Parse interval only using integers

(almost... except for micros rounding)

While this is probably an improvement on the previous implementation,
I am largely waving a dead chicken at windows, which keeps failing to
pass the seconds overflow test. If it doesn't pass now either I'll start
blaming Python's timedelta.
This commit is contained in:
Daniele Varrazzo 2017-02-24 03:48:41 +01:00
parent e351606b69
commit 834e9996da
2 changed files with 57 additions and 35 deletions

View File

@ -220,11 +220,10 @@ typecast_PYTIME_cast(const char *str, Py_ssize_t len, PyObject *curs)
static PyObject * static PyObject *
typecast_PYINTERVAL_cast(const char *str, Py_ssize_t len, PyObject *curs) typecast_PYINTERVAL_cast(const char *str, Py_ssize_t len, PyObject *curs)
{ {
long years = 0, months = 0, days = 0, lsec; long years = 0, months = 0, days = 0;
double hours = 0.0, minutes = 0.0, seconds = 0.0, hundredths = 0.0; long hours = 0, minutes = 0, seconds = 0, micros = 0;
double v = 0.0, sign = 1.0, denominator = 1.0; long v = 0, sign = 1, denom = 1;
int part = 0; int part = 0;
double micro;
if (str == NULL) { Py_RETURN_NONE; } if (str == NULL) { Py_RETURN_NONE; }
@ -234,56 +233,56 @@ typecast_PYINTERVAL_cast(const char *str, Py_ssize_t len, PyObject *curs)
switch (*str) { switch (*str) {
case '-': case '-':
sign = -1.0; sign = -1;
break; break;
case '0': case '1': case '2': case '3': case '4': case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': case '5': case '6': case '7': case '8': case '9':
v = v * 10.0 + (double)(*str - '0'); v = v * 10 + (*str - '0');
if (part == 6){ if (part == 6){
denominator *= 10; denom *= 10;
} }
break; break;
case 'y': case 'y':
if (part == 0) { if (part == 0) {
years = (long)(v*sign); years = v * sign;
v = 0; sign = 1; part = 1;
str = skip_until_space2(str, &len); str = skip_until_space2(str, &len);
v = 0.0; sign = 1.0; part = 1;
} }
break; break;
case 'm': case 'm':
if (part <= 1) { if (part <= 1) {
months = (long)(v*sign); months = v * sign;
v = 0; sign = 1; part = 2;
str = skip_until_space2(str, &len); str = skip_until_space2(str, &len);
v = 0.0; sign = 1.0; part = 2;
} }
break; break;
case 'd': case 'd':
if (part <= 2) { if (part <= 2) {
days = (long)(v*sign); days = v * sign;
v = 0; sign = 1; part = 3;
str = skip_until_space2(str, &len); str = skip_until_space2(str, &len);
v = 0.0; sign = 1.0; part = 3;
} }
break; break;
case ':': case ':':
if (part <= 3) { if (part <= 3) {
hours = v; hours = v;
v = 0.0; part = 4; v = 0; part = 4;
} }
else if (part == 4) { else if (part == 4) {
minutes = v; minutes = v;
v = 0.0; part = 5; v = 0; part = 5;
} }
break; break;
case '.': case '.':
if (part == 5) { if (part == 5) {
seconds = v; seconds = v;
v = 0.0; part = 6; v = 0; part = 6;
} }
break; break;
@ -294,7 +293,7 @@ typecast_PYINTERVAL_cast(const char *str, Py_ssize_t len, PyObject *curs)
str++; str++;
} }
/* manage last value, be it minutes or seconds or hundredths of a second */ /* manage last value, be it minutes or seconds or microseconds */
if (part == 4) { if (part == 4) {
minutes = v; minutes = v;
} }
@ -302,25 +301,31 @@ typecast_PYINTERVAL_cast(const char *str, Py_ssize_t len, PyObject *curs)
seconds = v; seconds = v;
} }
else if (part == 6) { else if (part == 6) {
hundredths = v; micros = v;
hundredths = hundredths/denominator; if (denom < 1000000L) {
do {
micros *= 10;
denom *= 10;
} while (denom < 1000000L);
}
else if (denom > 1000000L) {
micros = (long)((double)micros * 1000000.0 / denom);
}
} }
/* calculates seconds */ /* add hour, minutes, seconds, and include the sign */
if (sign < 0.0) { seconds += 60 * minutes + 3600 * hours;
seconds = - (hundredths + seconds + minutes*60 + hours*3600);
} if (sign < 0) {
else { seconds = -seconds;
seconds += hundredths + minutes*60 + hours*3600; micros = -micros;
} }
/* calculates days */ /* add the days - these items already included their own sign */
days += years*365 + months*30; seconds += (3600 * 24) * (days + 30 * months + 365 * years);
lsec = (long)seconds; return PyObject_CallFunction((PyObject*)PyDateTimeAPI->DeltaType, "lll",
micro = (seconds - (double)lsec) * 1000000.0; 0L, seconds, micros);
return PyObject_CallFunction((PyObject*)PyDateTimeAPI->DeltaType, "lli",
days, lsec, (int)round(micro));
} }
/* psycopg defaults to using python datetime types */ /* psycopg defaults to using python datetime types */

View File

@ -28,6 +28,11 @@ from psycopg2.tz import FixedOffsetTimezone, ZERO
from testutils import unittest, ConnectingTestCase, skip_before_postgres from testutils import unittest, ConnectingTestCase, skip_before_postgres
def total_seconds(d):
"""Return total number of seconds of a timedelta as a float."""
return d.days * 24 * 60 * 60 + d.seconds + d.microseconds / 1000000.0
class CommonDatetimeTestsMixin: class CommonDatetimeTestsMixin:
def execute(self, *args): def execute(self, *args):
@ -334,10 +339,6 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
self.assertEqual(t, time(0, 0, tzinfo=FixedOffsetTimezone(330))) self.assertEqual(t, time(0, 0, tzinfo=FixedOffsetTimezone(330)))
def test_large_interval(self): def test_large_interval(self):
def total_seconds(d):
"""Return total number of seconds of a timedelta as a float."""
return d.days * 24 * 60 * 60 + d.seconds + d.microseconds / 1000000.0
t = self.execute("select '999999:00:00'::interval") t = self.execute("select '999999:00:00'::interval")
self.assertEqual(total_seconds(t), 999999 * 60 * 60) self.assertEqual(total_seconds(t), 999999 * 60 * 60)
@ -356,6 +357,22 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
t = self.execute("select '-999999:00:00.9'::interval") t = self.execute("select '-999999:00:00.9'::interval")
self.assertEqual(total_seconds(t), -999999 * 60 * 60 - 0.9) self.assertEqual(total_seconds(t), -999999 * 60 * 60 - 0.9)
def test_micros_rounding(self):
t = self.execute("select '0.1'::interval")
self.assertEqual(total_seconds(t), 0.1)
t = self.execute("select '0.01'::interval")
self.assertEqual(total_seconds(t), 0.01)
t = self.execute("select '0.000001'::interval")
self.assertEqual(total_seconds(t), 1e-6)
t = self.execute("select '0.0000004'::interval")
self.assertEqual(total_seconds(t), 0)
t = self.execute("select '0.0000006'::interval")
self.assertEqual(total_seconds(t), 1e-6)
# Only run the datetime tests if psycopg was compiled with support. # Only run the datetime tests if psycopg was compiled with support.
if not hasattr(psycopg2.extensions, 'PYDATETIME'): if not hasattr(psycopg2.extensions, 'PYDATETIME'):