Datetime adaptation in bytes.

This commit is contained in:
Daniele Varrazzo 2010-12-23 04:08:32 +01:00
parent 87a7ebac10
commit 03dde732f6
2 changed files with 18 additions and 17 deletions

View File

@ -62,30 +62,37 @@ pydatetime_str(pydatetimeObject *self)
if (self->type <= PSYCO_DATETIME_TIMESTAMP) { if (self->type <= PSYCO_DATETIME_TIMESTAMP) {
PyObject *tz; PyObject *tz;
/* Select the right PG type to cast into. /* Select the right PG type to cast into. */
* fmt contains %s in Py2 and %U in Py3,
* So it can be used with the Text_FromFormatS macro */
char *fmt = NULL; char *fmt = NULL;
switch (self->type) { switch (self->type) {
case PSYCO_DATETIME_TIME: case PSYCO_DATETIME_TIME:
fmt = "'" Text_S "'::time"; fmt = "'%s'::time";
break; break;
case PSYCO_DATETIME_DATE: case PSYCO_DATETIME_DATE:
fmt = "'" Text_S "'::date"; fmt = "'%s'::date";
break; break;
case PSYCO_DATETIME_TIMESTAMP: case PSYCO_DATETIME_TIMESTAMP:
tz = PyObject_GetAttrString(self->wrapped, "tzinfo"); tz = PyObject_GetAttrString(self->wrapped, "tzinfo");
if (!tz) { return NULL; } if (!tz) { return NULL; }
fmt = (tz == Py_None) fmt = (tz == Py_None) ? "'%s'::timestamp" : "'%s'::timestamptz";
? "'" Text_S "'::timestamp"
: "'" Text_S "'::timestamptz";
Py_DECREF(tz); Py_DECREF(tz);
break; break;
} }
iso = PyObject_CallMethod(self->wrapped, "isoformat", NULL); iso = PyObject_CallMethod(self->wrapped, "isoformat", NULL);
if (iso) { if (iso) {
res = Text_FromFormatS(fmt, iso); #if PY_MAJOR_VERSION > 2
{
PyObject *biso;
if (!(biso = PyUnicode_AsEncodedString(iso, "ascii", NULL))) {
Py_DECREF(iso);
return NULL;
}
Py_DECREF(iso);
iso = biso;
}
#endif
res = Bytes_FromFormat(fmt, Bytes_AsString(iso));
Py_DECREF(iso); Py_DECREF(iso);
} }
return res; return res;
@ -103,8 +110,8 @@ pydatetime_str(pydatetimeObject *self)
} }
buffer[6] = '\0'; buffer[6] = '\0';
return PyString_FromFormat("'%d days %d.%s seconds'::interval", return Bytes_FromFormat("'%d days %d.%s seconds'::interval",
obj->days, obj->seconds, buffer); obj->days, obj->seconds, buffer);
} }
} }

View File

@ -81,18 +81,12 @@
#define Text_Format(f,a) PyString_Format(f,a) #define Text_Format(f,a) PyString_Format(f,a)
#define Text_FromUTF8(s) PyString_FromString(s) #define Text_FromUTF8(s) PyString_FromString(s)
#define Text_FromUTF8AndSize(s,n) PyString_FromStringAndSize(s,n) #define Text_FromUTF8AndSize(s,n) PyString_FromStringAndSize(s,n)
/* f must contain exactly a %s placeholder */
#define Text_FromFormatS(f,s) PyString_FromFormat(f, PyString_AsString(s))
#define Text_S "%s"
#else #else
#define Text_Type PyUnicode_Type #define Text_Type PyUnicode_Type
#define Text_Check(s) PyUnicode_Check(s) #define Text_Check(s) PyUnicode_Check(s)
#define Text_Format(f,a) PyUnicode_Format(f,a) #define Text_Format(f,a) PyUnicode_Format(f,a)
#define Text_FromUTF8(s) PyUnicode_FromString(s) #define Text_FromUTF8(s) PyUnicode_FromString(s)
#define Text_FromUTF8AndSize(s,n) PyUnicode_FromStringAndSize(s,n) #define Text_FromUTF8AndSize(s,n) PyUnicode_FromStringAndSize(s,n)
/* f must contain exactly a %U placeholder */
#define Text_FromFormatS(f,s) PyUnicode_FromFormat(f, s)
#define Text_S "%U"
#endif #endif
#if PY_MAJOR_VERSION > 2 #if PY_MAJOR_VERSION > 2