Working on TZ issues.

This commit is contained in:
Federico Di Gregorio 2005-10-17 15:01:14 +00:00
parent 5715a74388
commit 206aa79225
5 changed files with 53 additions and 17 deletions

View File

@ -1,5 +1,14 @@
2005-10-18 Federico Di Gregorio <fog@initd.org>
* psycopg/adapter_datetime.c: added tzinfo argument to psycopg2.Time and
psycopg2.Timestamp. Also now TimestampFromTicks sets the tzinfo object
to psycopg2.tz.LOCAL.
2005-10-17 Federico Di Gregorio <fog@initd.org> 2005-10-17 Federico Di Gregorio <fog@initd.org>
* psycopg/adapter_datetime.c: we now use localtime() instead of gmtime()
to accound for the local time-zone in timestamps.
* psycopg/connection_type.c: fixed docstring for .cursor(). * psycopg/connection_type.c: fixed docstring for .cursor().
* psycopg/psycopgmodule.c: added useful docstring for .connect(). * psycopg/psycopgmodule.c: added useful docstring for .connect().

2
NEWS
View File

@ -12,6 +12,8 @@ What's new in psycopg 2.0 rc 1
* Better docstrings for a few functions/methods. * Better docstrings for a few functions/methods.
* Some time-related functions like psycopg2.TimeFromTicks() now take the
local timezone into account.
What's new in psycopg 2.0 beta 4 What's new in psycopg 2.0 beta 4
-------------------------------- --------------------------------

View File

@ -43,6 +43,9 @@ extern PyObject *pyTimeTypeP;
extern PyObject *pyDateTimeTypeP; extern PyObject *pyDateTimeTypeP;
extern PyObject *pyDeltaTypeP; extern PyObject *pyDeltaTypeP;
extern PyObject *pyPsycopgTzModule;
extern PyObject *pyPsycopgTzLocalTimezone;
/* datetime_str, datetime_getquoted - return result of quoting */ /* datetime_str, datetime_getquoted - return result of quoting */
static PyObject * static PyObject *
@ -275,18 +278,24 @@ PyObject *
psyco_Time(PyObject *self, PyObject *args) psyco_Time(PyObject *self, PyObject *args)
{ {
PyObject *res = NULL; PyObject *res = NULL;
PyObject *tzinfo = NULL;
int hours, minutes=0; int hours, minutes=0;
double micro, seconds=0.0; double micro, seconds=0.0;
PyObject* obj = NULL; PyObject* obj = NULL;
if (!PyArg_ParseTuple(args, "iid", &hours, &minutes, &seconds)) if (!PyArg_ParseTuple(args, "iid|O", &hours, &minutes, &seconds,
&tzinfo))
return NULL; return NULL;
micro = (seconds - floor(seconds)) * 1000000.0; micro = (seconds - floor(seconds)) * 1000000.0;
if (tzinfo == NULL)
obj = PyObject_CallFunction(pyTimeTypeP, "iiii", obj = PyObject_CallFunction(pyTimeTypeP, "iiii",
hours, minutes, (int)round(seconds), (int)round(micro)); hours, minutes, (int)round(seconds), (int)round(micro));
else
obj = PyObject_CallFunction(pyTimeTypeP, "iiiiO",
hours, minutes, (int)round(seconds), (int)round(micro), tzinfo);
if (obj) { if (obj) {
res = PyObject_CallFunction((PyObject *)&pydatetimeType, res = PyObject_CallFunction((PyObject *)&pydatetimeType,
@ -301,20 +310,27 @@ PyObject *
psyco_Timestamp(PyObject *self, PyObject *args) psyco_Timestamp(PyObject *self, PyObject *args)
{ {
PyObject *res = NULL; PyObject *res = NULL;
PyObject *tzinfo = NULL;
int year, month, day; int year, month, day;
int hour=0, minute=0; /* default to midnight */ int hour=0, minute=0; /* default to midnight */
double micro, second=0.0; double micro, second=0.0;
PyObject* obj = NULL; PyObject* obj = NULL;
if (!PyArg_ParseTuple(args, "lii|iid", &year, &month, &day, if (!PyArg_ParseTuple(args, "lii|iidO", &year, &month, &day,
&hour, &minute, &second)) &hour, &minute, &second, &tzinfo))
return NULL; return NULL;
micro = (second - floor(second)) * 1000000.0; micro = (second - floor(second)) * 1000000.0;
if (tzinfo == NULL)
obj = PyObject_CallFunction(pyDateTimeTypeP, "iiiiiii", obj = PyObject_CallFunction(pyDateTimeTypeP, "iiiiiii",
year, month, day, hour, minute, (int)round(second), (int)round(micro)); year, month, day, hour, minute, (int)round(second),
(int)round(micro));
else
obj = PyObject_CallFunction(pyDateTimeTypeP, "iiiiiiiO",
year, month, day, hour, minute, (int)round(second),
(int)round(micro), tzinfo);
if (obj) { if (obj) {
res = PyObject_CallFunction((PyObject *)&pydatetimeType, res = PyObject_CallFunction((PyObject *)&pydatetimeType,
@ -337,7 +353,7 @@ psyco_DateFromTicks(PyObject *self, PyObject *args)
return NULL; return NULL;
t = (time_t)round(ticks); t = (time_t)round(ticks);
if (gmtime_r(&t, &tm)) { if (localtime_r(&t, &tm)) {
args = Py_BuildValue("iii", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday); args = Py_BuildValue("iii", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday);
if (args) { if (args) {
res = psyco_Date(self, args); res = psyco_Date(self, args);
@ -359,7 +375,7 @@ psyco_TimeFromTicks(PyObject *self, PyObject *args)
return NULL; return NULL;
t = (time_t)round(ticks); t = (time_t)round(ticks);
if (gmtime_r(&t, &tm)) { if (localtime_r(&t, &tm)) {
args = Py_BuildValue("iid", tm.tm_hour, tm.tm_min, (double)tm.tm_sec); args = Py_BuildValue("iid", tm.tm_hour, tm.tm_min, (double)tm.tm_sec);
if (args) { if (args) {
res = psyco_Time(self, args); res = psyco_Time(self, args);
@ -381,10 +397,11 @@ psyco_TimestampFromTicks(PyObject *self, PyObject *args)
return NULL; return NULL;
t = (time_t)round(ticks); t = (time_t)round(ticks);
if (gmtime_r(&t, &tm)) { if (localtime_r(&t, &tm)) {
args = Py_BuildValue("iiiiid", 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);
if (args) { if (args) {
res = psyco_Timestamp(self, args); res = psyco_Timestamp(self, args);
Py_DECREF(args); Py_DECREF(args);

View File

@ -52,11 +52,11 @@ extern PyObject *psyco_Date(PyObject *module, PyObject *args);
extern PyObject *psyco_Time(PyObject *module, PyObject *args); extern PyObject *psyco_Time(PyObject *module, PyObject *args);
#define psyco_Time_doc \ #define psyco_Time_doc \
"psycopg.Time(hour, minutes, seconds) -> new time" "psycopg.Time(hour, minutes, seconds, tzinfo=None) -> new time"
extern PyObject *psyco_Timestamp(PyObject *module, PyObject *args); extern PyObject *psyco_Timestamp(PyObject *module, PyObject *args);
#define psyco_Timestamp_doc \ #define psyco_Timestamp_doc \
"psycopg.Time(year, month, day, hour, minutes, seconds) -> new timestamp" "psycopg.Time(year, month, day, hour, minutes, seconds, tzinfo=None) -> new timestamp"
extern PyObject *psyco_DateFromTicks(PyObject *module, PyObject *args); extern PyObject *psyco_DateFromTicks(PyObject *module, PyObject *args);
#define psyco_DateFromTicks_doc \ #define psyco_DateFromTicks_doc \

View File

@ -55,6 +55,10 @@ PyObject *pyDateTimeTypeP = NULL;
PyObject *pyDeltaTypeP = NULL; PyObject *pyDeltaTypeP = NULL;
#endif #endif
/* pointers to the psycopg.tz classes */
PyObject *pyPsycopgTzModule = NULL;
PyObject *pyPsycopgTzLocalTimezone = NULL;
PyObject *psycoEncodings = NULL; PyObject *psycoEncodings = NULL;
PyObject *decimalType = NULL; PyObject *decimalType = NULL;
@ -354,7 +358,6 @@ psyco_decimal_init(void)
#endif #endif
} }
/** method table and module initialization **/ /** method table and module initialization **/
@ -480,6 +483,11 @@ init_psycopg(void)
pyDeltaTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "timedelta"); pyDeltaTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "timedelta");
#endif #endif
/* import psycopg2.tz anyway (TODO: replace with C-level module?) */
pyPsycopgTzModule = PyImport_ImportModule("psycopg2.tz");
pyPsycopgTzLocalTimezone =
PyObject_GetAttrString(pyPsycopgTzModule, "LOCAL");
/* 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);