Default tzinfo_factory for cursors.

This commit is contained in:
Federico Di Gregorio 2005-10-17 16:04:43 +00:00
parent 206aa79225
commit a237209a5e
5 changed files with 25 additions and 8 deletions

View File

@ -1,5 +1,8 @@
2005-10-18 Federico Di Gregorio <fog@initd.org> 2005-10-18 Federico Di Gregorio <fog@initd.org>
* psycopg/cursor_type.c: cursors now have a FixedOffsetTimezone
tzinfo_factory by default.
* psycopg/adapter_datetime.c: added tzinfo argument to psycopg2.Time and * psycopg/adapter_datetime.c: added tzinfo argument to psycopg2.Time and
psycopg2.Timestamp. Also now TimestampFromTicks sets the tzinfo object psycopg2.Timestamp. Also now TimestampFromTicks sets the tzinfo object
to psycopg2.tz.LOCAL. to psycopg2.tz.LOCAL.

View File

@ -51,7 +51,12 @@ curs.execute("INSERT INTO test_tz VALUES (%s)", (d,))
print "Inserted timestamp with timezone:", d print "Inserted timestamp with timezone:", d
print "Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d) print "Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d)
curs.tzinfo_factory = FixedOffsetTimezone curs.execute("SELECT * FROM test_tz")
d = curs.fetchone()[0]
curs.execute("INSERT INTO test_tz VALUES (%s)", (d,))
print "Inserted SELECTed timestamp:", d
print "Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d)
curs.execute("SELECT * FROM test_tz") curs.execute("SELECT * FROM test_tz")
for d in curs: for d in curs:
u = d[0].utcoffset() or ZERO u = d[0].utcoffset() or ZERO
@ -59,5 +64,6 @@ for d in curs:
print "Local time:", d[0] print "Local time:", d[0]
print "Time zone:", d[0].tzinfo.tzname(d[0]), d[0].tzinfo.utcoffset(d[0]) print "Time zone:", d[0].tzinfo.tzname(d[0]), d[0].tzinfo.utcoffset(d[0])
curs.execute("DROP TABLE test_tz") curs.execute("DROP TABLE test_tz")
conn.commit() conn.commit()

View File

@ -44,7 +44,7 @@ extern PyObject *pyDateTimeTypeP;
extern PyObject *pyDeltaTypeP; extern PyObject *pyDeltaTypeP;
extern PyObject *pyPsycopgTzModule; extern PyObject *pyPsycopgTzModule;
extern PyObject *pyPsycopgTzLocalTimezone; extern PyObject *pyPsycopgTzLOCAL;
/* datetime_str, datetime_getquoted - return result of quoting */ /* datetime_str, datetime_getquoted - return result of quoting */
@ -401,7 +401,7 @@ psyco_TimestampFromTicks(PyObject *self, PyObject *args)
args = Py_BuildValue("iiiiidO", args = Py_BuildValue("iiiiidO",
tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
tm.tm_hour, tm.tm_min, (double)tm.tm_sec, tm.tm_hour, tm.tm_min, (double)tm.tm_sec,
pyPsycopgTzLocalTimezone); pyPsycopgTzLOCAL);
if (args) { if (args) {
res = psyco_Timestamp(self, args); res = psyco_Timestamp(self, args);
Py_DECREF(args); Py_DECREF(args);

View File

@ -35,6 +35,9 @@
#include "psycopg/microprotocols_proto.h" #include "psycopg/microprotocols_proto.h"
#include "pgversion.h" #include "pgversion.h"
extern PyObject *pyPsycopgTzFixedOffsetTimezone;
/** DBAPI methods **/ /** DBAPI methods **/
/* close method - close the cursor */ /* close method - close the cursor */
@ -1266,8 +1269,10 @@ cursor_setup(cursorObject *self, connectionObject *conn)
Py_INCREF(Py_None); Py_INCREF(Py_None);
self->tuple_factory = Py_None; self->tuple_factory = Py_None;
Py_INCREF(Py_None); Py_INCREF(Py_None);
self->tzinfo_factory = Py_None;
Py_INCREF(Py_None); /* default tzinfo factory */
self->tzinfo_factory = pyPsycopgTzFixedOffsetTimezone;
Py_INCREF(self->tzinfo_factory);
Dprintf("cursor_setup: good cursor object at %p, refcnt = %d", Dprintf("cursor_setup: good cursor object at %p, refcnt = %d",
self, ((PyObject *)self)->ob_refcnt); self, ((PyObject *)self)->ob_refcnt);

View File

@ -57,7 +57,8 @@ PyObject *pyDeltaTypeP = NULL;
/* pointers to the psycopg.tz classes */ /* pointers to the psycopg.tz classes */
PyObject *pyPsycopgTzModule = NULL; PyObject *pyPsycopgTzModule = NULL;
PyObject *pyPsycopgTzLocalTimezone = NULL; PyObject *pyPsycopgTzLOCAL = NULL;
PyObject *pyPsycopgTzFixedOffsetTimezone = NULL;
PyObject *psycoEncodings = NULL; PyObject *psycoEncodings = NULL;
PyObject *decimalType = NULL; PyObject *decimalType = NULL;
@ -485,9 +486,11 @@ init_psycopg(void)
/* import psycopg2.tz anyway (TODO: replace with C-level module?) */ /* import psycopg2.tz anyway (TODO: replace with C-level module?) */
pyPsycopgTzModule = PyImport_ImportModule("psycopg2.tz"); pyPsycopgTzModule = PyImport_ImportModule("psycopg2.tz");
pyPsycopgTzLocalTimezone = pyPsycopgTzLOCAL =
PyObject_GetAttrString(pyPsycopgTzModule, "LOCAL"); PyObject_GetAttrString(pyPsycopgTzModule, "LOCAL");
pyPsycopgTzFixedOffsetTimezone =
PyObject_GetAttrString(pyPsycopgTzModule, "FixedOffsetTimezone");
/* initialize the module and grab module's dictionary */ /* initialize the module and grab module's dictionary */
module = Py_InitModule("_psycopg", psycopgMethods); module = Py_InitModule("_psycopg", psycopgMethods);
dict = PyModule_GetDict(module); dict = PyModule_GetDict(module);