From: Daniele Varrazzo Date: Wed, 2 Jun 2021 00:49:23 +0000 (+0100) Subject: Add c datetime text dumpers X-Git-Tag: 3.0.dev0~39^2~17 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2a15309641d2824c6f974ed3f6e82e1ca532c13c;p=thirdparty%2Fpsycopg.git Add c datetime text dumpers --- diff --git a/psycopg3_c/psycopg3_c/types/date.pyx b/psycopg3_c/psycopg3_c/types/date.pyx index 0e3e6fd14..bb539d46b 100644 --- a/psycopg3_c/psycopg3_c/types/date.pyx +++ b/psycopg3_c/psycopg3_c/types/date.pyx @@ -174,6 +174,59 @@ cdef class TimeTzBinaryDumper(_BaseTimeDumper): return sizeof(int64_t) + sizeof(int32_t) +cdef class _BaseDateTimeDumper(CDumper): + + cpdef get_key(self, obj, format): + # Use (cls,) to report the need to upgrade (downgrade, actually) to a + # dumper for naive timestamp. + if obj.tzinfo: + return self.cls + else: + return (self.cls,) + + cpdef upgrade(self, obj: time, format): + raise NotImplementedError + + +cdef class _BaseDateTimeTextDumper(_BaseDateTimeDumper): + + format = PQ_TEXT + + cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1: + cdef Py_ssize_t size; + cdef const char *src + + # NOTE: whatever the PostgreSQL DateStyle input format (DMY, MDY, YMD) + # the YYYY-MM-DD is always understood correctly. + cdef str s = str(obj) + src = PyUnicode_AsUTF8AndSize(s, &size) + + cdef char *buf = CDumper.ensure_size(rv, offset, size) + memcpy(buf, src, size) + return size + + +@cython.final +cdef class DateTimeTzDumper(_BaseDateTimeTextDumper): + + def __cinit__(self): + self.oid = oids.TIMESTAMPTZ_OID + + cpdef upgrade(self, obj, format): + if obj.tzinfo: + return self + else: + return DateTimeDumper(self.cls) + + +@cython.final +cdef class DateTimeDumper(_BaseDateTimeTextDumper): + + def __cinit__(self): + self.oid = oids.TIMESTAMP_OID + + + @cython.final cdef class DateLoader(CLoader):