From 234a534825e761aff3f29f33abaafd0b5740287f Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 2 Jun 2021 03:44:48 +0100 Subject: [PATCH] Add c binary time loader --- psycopg3_c/psycopg3_c/types/date.pyx | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/psycopg3_c/psycopg3_c/types/date.pyx b/psycopg3_c/psycopg3_c/types/date.pyx index 6a9360de2..f6dbb71a1 100644 --- a/psycopg3_c/psycopg3_c/types/date.pyx +++ b/psycopg3_c/psycopg3_c/types/date.pyx @@ -458,6 +458,33 @@ cdef class TimeLoader(CLoader): raise e.DataError(f"can't parse date {s!r}: {ex}") from None +@cython.final +cdef class TimeBinaryLoader(CLoader): + + format = PQ_BINARY + + cdef object cload(self, const char *data, size_t length): + cdef int64_t val = endian.be64toh((data)[0]) + cdef int h, m, s, ms + + with cython.cdivision(True): + ms = val % 1_000_000 + val //= 1_000_000 + + s = val % 60 + val //= 60 + + m = val % 60 + h = val // 60 + + try: + return cdt.time_new(h, m, s, ms, None) + except ValueError: + raise e.DataError( + f"time not supported by Python: hour={h}" + ) from None + + cdef const char *_get_datestyle(pq.PGconn pgconn): cdef const char *ds if pgconn is not None: -- 2.47.3