From: Daniele Varrazzo Date: Thu, 13 May 2021 09:49:33 +0000 (+0200) Subject: Consider loss of precision in negative binary timestamps too X-Git-Tag: 3.0.dev0~42^2~19 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0f84692f6be1144662aecf5e5070f1ef6d75856b;p=thirdparty%2Fpsycopg.git Consider loss of precision in negative binary timestamps too --- diff --git a/psycopg3/psycopg3/types/date.py b/psycopg3/psycopg3/types/date.py index dfd6c9bce..341ce380c 100644 --- a/psycopg3/psycopg3/types/date.py +++ b/psycopg3/psycopg3/types/date.py @@ -183,7 +183,7 @@ class DateTimeTzBinaryDumper(_BaseDateTimeDumper): def dump(self, obj: datetime) -> bytes: delta = obj - _pg_datetimetz_epoch secs = delta.total_seconds() - if secs < self._delta_prec_loss: + if -self._delta_prec_loss < secs < self._delta_prec_loss: micros = int(1_000_000 * secs) else: micros = delta.microseconds + 1_000_000 * ( @@ -204,7 +204,7 @@ class DateTimeBinaryDumper(DateTimeTzBinaryDumper): def dump(self, obj: datetime) -> bytes: delta = obj - _pg_datetime_epoch secs = delta.total_seconds() - if secs < self._delta_prec_loss: + if -self._delta_prec_loss < secs < self._delta_prec_loss: micros = int(1_000_000 * secs) else: micros = ( diff --git a/tests/types/test_date.py b/tests/types/test_date.py index 2b3d4f816..d37bb85ae 100644 --- a/tests/types/test_date.py +++ b/tests/types/test_date.py @@ -104,6 +104,7 @@ def test_load_date_overflow_binary(conn, val): "val, expr", [ ("min", "0001-01-01 00:00"), + ("258,1,8,1,12,32,358261", "0258-1-8 1:12:32.358261"), ("1000,1,1,0,0", "1000-01-01 00:00"), ("2000,1,1,0,0", "2000-01-01 00:00"), ("2000,1,2,3,4,5,6", "2000-01-02 03:04:05.000006"), @@ -204,6 +205,7 @@ def test_load_datetime_overflow_binary(conn, val): ("min~2", "0001-01-01 00:00"), ("min~-12", "0001-01-01 00:00-12:00"), ("min~+12", "0001-01-01 00:00+12:00"), + ("258,1,8,1,12,32,358261~1:2:3", "0258-1-8 1:12:32.358261+01:02:03"), ("1000,1,1,0,0~2", "1000-01-01 00:00+2"), ("2000,1,1,0,0~2", "2000-01-01 00:00+2"), ("2000,1,1,0,0~12", "2000-01-01 00:00+12"), @@ -211,7 +213,9 @@ def test_load_datetime_overflow_binary(conn, val): ("2000,1,1,0,0~01:02:03", "2000-01-01 00:00+01:02:03"), ("2000,1,1,0,0~-01:02:03", "2000-01-01 00:00-01:02:03"), ("2000,12,31,23,59,59,999999~2", "2000-12-31 23:59:59.999999+2"), + ("2300,1,1,0,0,0,1~1", "2300-01-01 00:00:00.000001+1"), ("3000,1,1,0,0~2", "3000-01-01 00:00+2"), + ("7000,1,1,0,0,0,1~-1:2:3", "7000-01-01 00:00:00.000001-01:02:03"), ("max~2", "9999-12-31 23:59:59.999999"), ], )