From a237209a5e20e1cb557acde1e797107597b2b85f Mon Sep 17 00:00:00 2001 From: Federico Di Gregorio Date: Mon, 17 Oct 2005 16:04:43 +0000 Subject: [PATCH] Default tzinfo_factory for cursors. --- ChangeLog | 3 +++ examples/tz.py | 8 +++++++- psycopg/adapter_datetime.c | 4 ++-- psycopg/cursor_type.c | 9 +++++++-- psycopg/psycopgmodule.c | 9 ++++++--- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index cd6719df..edd53b6d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2005-10-18 Federico Di Gregorio + * psycopg/cursor_type.c: cursors now have a FixedOffsetTimezone + tzinfo_factory by default. + * psycopg/adapter_datetime.c: added tzinfo argument to psycopg2.Time and psycopg2.Timestamp. Also now TimestampFromTicks sets the tzinfo object to psycopg2.tz.LOCAL. diff --git a/examples/tz.py b/examples/tz.py index 0ae9fa36..8fe40af1 100644 --- a/examples/tz.py +++ b/examples/tz.py @@ -51,7 +51,12 @@ curs.execute("INSERT INTO test_tz VALUES (%s)", (d,)) print "Inserted timestamp with timezone:", 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") for d in curs: u = d[0].utcoffset() or ZERO @@ -59,5 +64,6 @@ for d in curs: print "Local time:", d[0] print "Time zone:", d[0].tzinfo.tzname(d[0]), d[0].tzinfo.utcoffset(d[0]) + curs.execute("DROP TABLE test_tz") conn.commit() diff --git a/psycopg/adapter_datetime.c b/psycopg/adapter_datetime.c index 024b7122..8443df9d 100644 --- a/psycopg/adapter_datetime.c +++ b/psycopg/adapter_datetime.c @@ -44,7 +44,7 @@ extern PyObject *pyDateTimeTypeP; extern PyObject *pyDeltaTypeP; extern PyObject *pyPsycopgTzModule; -extern PyObject *pyPsycopgTzLocalTimezone; +extern PyObject *pyPsycopgTzLOCAL; /* datetime_str, datetime_getquoted - return result of quoting */ @@ -401,7 +401,7 @@ psyco_TimestampFromTicks(PyObject *self, PyObject *args) args = Py_BuildValue("iiiiidO", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min, (double)tm.tm_sec, - pyPsycopgTzLocalTimezone); + pyPsycopgTzLOCAL); if (args) { res = psyco_Timestamp(self, args); Py_DECREF(args); diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index dcabf92d..9de563d3 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -35,6 +35,9 @@ #include "psycopg/microprotocols_proto.h" #include "pgversion.h" +extern PyObject *pyPsycopgTzFixedOffsetTimezone; + + /** DBAPI methods **/ /* close method - close the cursor */ @@ -1266,8 +1269,10 @@ cursor_setup(cursorObject *self, connectionObject *conn) Py_INCREF(Py_None); self->tuple_factory = 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", self, ((PyObject *)self)->ob_refcnt); diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 4166cc25..31d16f90 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -57,7 +57,8 @@ PyObject *pyDeltaTypeP = NULL; /* pointers to the psycopg.tz classes */ PyObject *pyPsycopgTzModule = NULL; -PyObject *pyPsycopgTzLocalTimezone = NULL; +PyObject *pyPsycopgTzLOCAL = NULL; +PyObject *pyPsycopgTzFixedOffsetTimezone = NULL; PyObject *psycoEncodings = NULL; PyObject *decimalType = NULL; @@ -485,9 +486,11 @@ init_psycopg(void) /* import psycopg2.tz anyway (TODO: replace with C-level module?) */ pyPsycopgTzModule = PyImport_ImportModule("psycopg2.tz"); - pyPsycopgTzLocalTimezone = + pyPsycopgTzLOCAL = PyObject_GetAttrString(pyPsycopgTzModule, "LOCAL"); - + pyPsycopgTzFixedOffsetTimezone = + PyObject_GetAttrString(pyPsycopgTzModule, "FixedOffsetTimezone"); + /* initialize the module and grab module's dictionary */ module = Py_InitModule("_psycopg", psycopgMethods); dict = PyModule_GetDict(module);