diff options
author | Fred Drake <fdrake@acm.org> | 2004-08-03 17:58:55 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2004-08-03 17:58:55 (GMT) |
commit | f901abdd62c9067f993b85392dbb73a560af6325 (patch) | |
tree | 70cedeab8d5d6b1999a7bfcbd1bb0045c9aa0b90 /Modules/timemodule.c | |
parent | d04573fef0346ee9a131e0c63d18ab9fbd12ea63 (diff) | |
download | cpython-f901abdd62c9067f993b85392dbb73a560af6325.zip cpython-f901abdd62c9067f993b85392dbb73a560af6325.tar.gz cpython-f901abdd62c9067f993b85392dbb73a560af6325.tar.bz2 |
allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
(closes SF bug #658254, patch #663482)
Diffstat (limited to 'Modules/timemodule.c')
-rw-r--r-- | Modules/timemodule.c | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 21745e0..2cd9a57 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -277,13 +277,33 @@ time_convert(double when, struct tm * (*function)(const time_t *)) return tmtotuple(p); } +/* Parse arg tuple that can contain an optional float-or-None value; + format needs to be "|O:name". + Returns non-zero on success (parallels PyArg_ParseTuple). +*/ +static int +parse_time_double_args(PyObject *args, char *format, double *pwhen) +{ + PyObject *ot = NULL; + + if (!PyArg_ParseTuple(args, format, &ot)) + return 0; + if (ot == NULL || ot == Py_None) + *pwhen = floattime(); + else { + double when = PyFloat_AsDouble(ot); + if (PyErr_Occurred()) + return 0; + *pwhen = when; + } + return 1; +} + static PyObject * time_gmtime(PyObject *self, PyObject *args) { double when; - if (PyTuple_Size(args) == 0) - when = floattime(); - if (!PyArg_ParseTuple(args, "|d:gmtime", &when)) + if (!parse_time_double_args(args, "|O:gmtime", &when)) return NULL; return time_convert(when, gmtime); } @@ -299,9 +319,7 @@ static PyObject * time_localtime(PyObject *self, PyObject *args) { double when; - if (PyTuple_Size(args) == 0) - when = floattime(); - if (!PyArg_ParseTuple(args, "|d:localtime", &when)) + if (!parse_time_double_args(args, "|O:localtime", &when)) return NULL; return time_convert(when, localtime); } @@ -502,14 +520,17 @@ is used."); static PyObject * time_ctime(PyObject *self, PyObject *args) { - double dt; + PyObject *ot = NULL; time_t tt; char *p; - if (PyTuple_Size(args) == 0) + if (!PyArg_ParseTuple(args, "|O:ctime", &ot)) + return NULL; + if (ot == NULL || ot == Py_None) tt = time(NULL); else { - if (!PyArg_ParseTuple(args, "|d:ctime", &dt)) + double dt = PyFloat_AsDouble(ot); + if (PyErr_Occurred()) return NULL; tt = _PyTime_DoubleToTimet(dt); if (tt == (time_t)-1 && PyErr_Occurred()) |