diff -r c2217813dbab Modules/timemodule.c --- a/Modules/timemodule.c Fri Feb 23 12:05:41 2007 +0100 +++ b/Modules/timemodule.c Sat Feb 24 00:24:12 2007 +0100 @@ -241,12 +241,39 @@ static PyStructSequence_Desc struct_time 9, }; +static PyStructSequence_Field struct_time_tz_type_fields[] = { + {"tm_year", NULL}, + {"tm_mon", NULL}, + {"tm_mday", NULL}, + {"tm_hour", NULL}, + {"tm_min", NULL}, + {"tm_sec", NULL}, + {"tm_wday", NULL}, + {"tm_yday", NULL}, + {"tm_isdst", NULL}, + {"tm_gmtoff", NULL}, + {0} +}; + +static PyStructSequence_Desc struct_time_tz_type_desc = { + "time.struct_time_tz", + NULL, + struct_time_tz_type_fields, + 10, +}; + static PyTypeObject StructTimeType; - -static PyObject * -tmtotuple(struct tm *p) -{ - PyObject *v = PyStructSequence_New(&StructTimeType); +static PyTypeObject StructTimeTZType; + +static PyObject * +tmtotuple(struct tm *p, int withtz) +{ + PyObject *v; + + if (withtz) + v = PyStructSequence_New(&StructTimeTZType); + else + v = PyStructSequence_New(&StructTimeType); if (v == NULL) return NULL; @@ -261,6 +288,9 @@ tmtotuple(struct tm *p) SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */ SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */ SET(8, p->tm_isdst); + if (withtz) { + SET(9, p->tm_gmtoff); + } #undef SET if (PyErr_Occurred()) { Py_XDECREF(v); @@ -271,7 +301,7 @@ tmtotuple(struct tm *p) } static PyObject * -time_convert(double when, struct tm * (*function)(const time_t *)) +time_convert(double when, struct tm * (*function)(const time_t *), int withtz) { struct tm *p; time_t whent = _PyTime_DoubleToTimet(when); @@ -287,7 +317,7 @@ time_convert(double when, struct tm * (* #endif return PyErr_SetFromErrno(PyExc_ValueError); } - return tmtotuple(p); + return tmtotuple(p, withtz); } /* Parse arg tuple that can contain an optional float-or-None value; @@ -318,7 +348,7 @@ time_gmtime(PyObject *self, PyObject *ar double when; if (!parse_time_double_args(args, "|O:gmtime", &when)) return NULL; - return time_convert(when, gmtime); + return time_convert(when, gmtime, 0); } PyDoc_STRVAR(gmtime_doc, @@ -334,7 +364,7 @@ time_localtime(PyObject *self, PyObject double when; if (!parse_time_double_args(args, "|O:localtime", &when)) return NULL; - return time_convert(when, localtime); + return time_convert(when, localtime, 0); } PyDoc_STRVAR(localtime_doc, @@ -343,13 +373,44 @@ Convert seconds since the Epoch to a tim Convert seconds since the Epoch to a time tuple expressing local time.\n\ When 'seconds' is not passed in, convert the current time instead."); +static PyObject * +time_localtime_tz(PyObject *self, PyObject *args) +{ + double when; + if (!parse_time_double_args(args, "|O:localtime", &when)) + return NULL; + return time_convert(when, localtime, 1); +} + +PyDoc_STRVAR(localtime_tz_doc, +"localtime_tz([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst,tm_gmtoff)\n\ +\n\ +Convert seconds since the Epoch to a time tuple expressing local time.\n\ +Unlike the localtime function, an extra tuple element is provided,\n\ +specifying the offset from UTC (a.k.a. GMT) in seconds.\n\ +When 'seconds' is not passed in, convert the current time instead."); + static int gettmarg(PyObject *args, struct tm *p) { int y; memset((void *) p, '\0', sizeof(struct tm)); - if (!PyArg_Parse(args, "(iiiiiiiii)", + if (PyTuple_GET_SIZE(args) == 10) { + if (!PyArg_Parse(args, "(iiiiiiiiii)", + &y, + &p->tm_mon, + &p->tm_mday, + &p->tm_hour, + &p->tm_min, + &p->tm_sec, + &p->tm_wday, + &p->tm_yday, + &p->tm_isdst, + &p->tm_gmtoff)) + return 0; + } else { + if (!PyArg_Parse(args, "(iiiiiiiii)", &y, &p->tm_mon, &p->tm_mday, @@ -359,7 +420,8 @@ gettmarg(PyObject *args, struct tm *p) &p->tm_wday, &p->tm_yday, &p->tm_isdst)) - return 0; + return 0; + } if (y < 1900) { PyObject *accept = PyDict_GetItemString(moddict, "accept2dyear"); @@ -730,6 +792,7 @@ static PyMethodDef time_methods[] = { {"sleep", time_sleep, METH_VARARGS, sleep_doc}, {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc}, {"localtime", time_localtime, METH_VARARGS, localtime_doc}, + {"localtime_tz",time_localtime_tz, METH_VARARGS, localtime_tz_doc}, {"asctime", time_asctime, METH_VARARGS, asctime_doc}, {"ctime", time_ctime, METH_VARARGS, ctime_doc}, #ifdef HAVE_MKTIME @@ -821,8 +884,11 @@ inittime(void) SetConsoleCtrlHandler( PyCtrlHandler, TRUE); #endif /* MS_WINDOWS */ PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc); + PyStructSequence_InitType(&StructTimeTZType, &struct_time_tz_type_desc); Py_INCREF(&StructTimeType); + Py_INCREF(&StructTimeTZType); PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType); + PyModule_AddObject(m, "struct_time_tz", (PyObject*) &StructTimeTZType); }