diff --git a/ChangeLog b/ChangeLog index 89095210..e4ee7bfd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2005-08-23 Federico Di Gregorio + + * psycopg/adapter_mxdatetime.c (mxdatetime_str): ported code from 1.1.x + to convert mxDateTime object preserving the precision of fractional + seconds. + 2005-08-22 Federico Di Gregorio * ZPsycopgDA/*.py: psycopg -> psycopg2. diff --git a/psycopg/adapter_mxdatetime.c b/psycopg/adapter_mxdatetime.c index 3c9dc760..7ae8ee79 100644 --- a/psycopg/adapter_mxdatetime.c +++ b/psycopg/adapter_mxdatetime.c @@ -39,17 +39,43 @@ extern mxDateTimeModule_APIObject *mxDateTimeP; /* mxdatetime_str, mxdatetime_getquoted - return result of quoting */ -static char *mxdatetimeObject_str_conv[] = { - PSYCO_MXDATETIME_TIME_CONV, - PSYCO_MXDATETIME_DATE_CONV, - PSYCO_MXDATETIME_TIMESTAMP_CONV -}; - static PyObject * mxdatetime_str(mxdatetimeObject *self) { - return PyObject_CallMethod(self->wrapped, "strftime", "s", - mxdatetimeObject_str_conv[self->type]); + PyObject *res = NULL; + char *buffer = NULL; + + mxDateTimeObject *obj = (mxDateTimeObject*)self->wrapped; + + switch (self->type) { + + case 0: + asprintf(&buffer, "'%02d:%02d:%.6f'", + (int)obj->hour, (int)obj->minute, (float)obj->second); + if (buffer) res = PyString_FromString(buffer); + break; + + case 1: + asprintf(&buffer, "'%ld-%02d-%02d'", + obj->year, (int)obj->month, (int)obj->day); + if (buffer) res = PyString_FromString(buffer); + break; + + case 2: + asprintf(&buffer, "'%ld-%02d-%02d %02d:%02d:%.6f'", + obj->year, (int)obj->month, (int)obj->day, + (int)obj->hour, (int)obj->minute, (float)obj->second); + if (buffer) res = PyString_FromString(buffer); + break; + + case 3: + res = PyObject_CallMethod(self->wrapped, "strftime", "s", + "'%d:%H:%M:%S'"); + break; + } + + if (buffer) free(buffer); + return res; } PyObject * diff --git a/psycopg/adapter_mxdatetime.h b/psycopg/adapter_mxdatetime.h index b7cd1730..b5e4b130 100644 --- a/psycopg/adapter_mxdatetime.h +++ b/psycopg/adapter_mxdatetime.h @@ -41,12 +41,6 @@ typedef struct { #define PSYCO_MXDATETIME_INTERVAL 3 } mxdatetimeObject; - -/* the conversion strings */ -#define PSYCO_MXDATETIME_TIME_CONV "'%H:%M:%S'" -#define PSYCO_MXDATETIME_DATE_CONV "'%Y-%m-%d'" -#define PSYCO_MXDATETIME_TIMESTAMP_CONV "'%Y-%m-%d %H:%M:%S'" -#define PSYCO_MXDATETIME_INTERVAL_CONV "'%d:%H:%M:%S'" /* functions exported to psycopgmodule.c */ #ifdef PSYCOPG_DEFAULT_MXDATETIME diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index 8cb5f2c6..22201704 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -588,7 +588,7 @@ _psyco_curs_buildrow_fill(cursorObject *self, PyObject *res, len = 0; } else { - str = PQgetvalue(self->pgres, row, i); + str = (unsigned char*)PQgetvalue(self->pgres, row, i); len = PQgetlength(self->pgres, row, i); }