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,4 +1,13 @@
2005-10-17 Federico Di Gregorio <fog@initd.org>
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>
* 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().

2
NEWS
View File

@ -12,6 +12,8 @@ What's new in psycopg 2.0 rc 1
* 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
--------------------------------

View File

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

View File

@ -55,6 +55,10 @@ PyObject *pyDateTimeTypeP = NULL;
PyObject *pyDeltaTypeP = NULL;
#endif
/* pointers to the psycopg.tz classes */
PyObject *pyPsycopgTzModule = NULL;
PyObject *pyPsycopgTzLocalTimezone = NULL;
PyObject *psycoEncodings = NULL;
PyObject *decimalType = NULL;
@ -354,7 +358,6 @@ psyco_decimal_init(void)
#endif
}
/** method table and module initialization **/
@ -480,6 +483,11 @@ init_psycopg(void)
pyDeltaTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "timedelta");
#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 */
module = Py_InitModule("_psycopg", psycopgMethods);
dict = PyModule_GetDict(module);