From d3c1ad5945b7bed9e9f2696697768d731a7f80c2 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sun, 8 Feb 2015 00:41:50 +0000 Subject: [PATCH] Convert Postgres time 24:00 into 00:00 Fix ticket #278. --- NEWS | 1 + psycopg/typecast.c | 3 +++ tests/test_dates.py | 12 ++++++++++++ 3 files changed, 16 insertions(+) diff --git a/NEWS b/NEWS index 532540bc..d4ffc36d 100644 --- a/NEWS +++ b/NEWS @@ -21,6 +21,7 @@ What's new in psycopg 2.5.5 - Named cursors used as context manager don't swallow the exception on exit (:ticket:`#262`). +- PostgreSQL time 24:00 is converted to Python 00:00 (:ticket:`#278`). What's new in psycopg 2.5.4 diff --git a/psycopg/typecast.c b/psycopg/typecast.c index 9678a36b..1cae869f 100644 --- a/psycopg/typecast.c +++ b/psycopg/typecast.c @@ -164,6 +164,9 @@ typecast_parse_time(const char* s, const char** t, Py_ssize_t* len, while (usd++ < 6) *us *= 10; } + /* 24:00:00 -> 00:00:00 (ticket #278) */ + if (*hh == 24) { *hh = 0; } + return cz; } diff --git a/tests/test_dates.py b/tests/test_dates.py index 9c4f840a..4ec131ab 100755 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -319,6 +319,18 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin): from datetime import timedelta self._test_type_roundtrip_array(timedelta(seconds=30)) + def test_time_24(self): + from datetime import time + + t = self.execute("select '24:00'::time;") + self.assertEqual(t, time(0, 0)) + + t = self.execute("select '24:00+05'::timetz;") + self.assertEqual(t, time(0, 0, tzinfo=FixedOffsetTimezone(300))) + + t = self.execute("select '24:00+05:30'::timetz;") + self.assertEqual(t, time(0, 0, tzinfo=FixedOffsetTimezone(330))) + # Only run the datetime tests if psycopg was compiled with support. if not hasattr(psycopg2.extensions, 'PYDATETIME'):