diff options
author | Guido van Rossum <guido@python.org> | 1998-06-09 16:30:31 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-06-09 16:30:31 (GMT) |
commit | 87ce7bbc396c6eea2d1151ab89ccfeff3830460f (patch) | |
tree | 0d6b7289588bc42e2d515004e426159a12780d37 /Modules | |
parent | 52481484e6183a208184994cb070754a03037e84 (diff) | |
download | cpython-87ce7bbc396c6eea2d1151ab89ccfeff3830460f.zip cpython-87ce7bbc396c6eea2d1151ab89ccfeff3830460f.tar.gz cpython-87ce7bbc396c6eea2d1151ab89ccfeff3830460f.tar.bz2 |
Added strptime, if it exists. Modified after a patch by Jesse Hallio.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/timemodule.c | 63 |
1 files changed, 53 insertions, 10 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 6c938c2..7b411ae 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -33,6 +33,8 @@ PERFORMANCE OF THIS SOFTWARE. #include "Python.h" +#include <ctype.h> + #ifdef HAVE_SELECT #include "mymath.h" #endif @@ -221,6 +223,22 @@ time_sleep(self, args) } static PyObject * +tmtotuple(p) + struct tm *p; +{ + return Py_BuildValue("(iiiiiiiii)", + p->tm_year + 1900, + p->tm_mon + 1, /* Want January == 1 */ + p->tm_mday, + p->tm_hour, + p->tm_min, + p->tm_sec, + (p->tm_wday + 6) % 7, /* Want Monday == 0 */ + p->tm_yday + 1, /* Want January, 1 == 1 */ + p->tm_isdst); +} + +static PyObject * time_convert(when, function) time_t when; struct tm * (*function) Py_PROTO((const time_t *)); @@ -235,16 +253,7 @@ time_convert(when, function) #endif return PyErr_SetFromErrno(PyExc_IOError); } - return Py_BuildValue("(iiiiiiiii)", - p->tm_year + 1900, - p->tm_mon + 1, /* Want January == 1 */ - p->tm_mday, - p->tm_hour, - p->tm_min, - p->tm_sec, - (p->tm_wday + 6) % 7, /* Want Monday == 0 */ - p->tm_yday + 1, /* Want January, 1 == 1 */ - p->tm_isdst); + return tmtotuple(p); } static PyObject * @@ -345,6 +354,37 @@ time_strftime(self, args) } #endif /* HAVE_STRFTIME */ +#ifdef HAVE_STRPTIME +static PyObject * +time_strptime(self, args) + PyObject *self; + PyObject *args; +{ + struct tm tm; + char *fmt = "%a %b %d %H:%M:%S %Y"; + char *buf; + char *s; + + if (!PyArg_ParseTuple(args, "s|s", &buf, &fmt)) { + PyErr_SetString(PyExc_ValueError, "invalid argument"); + return NULL; + } + s = strptime(buf, fmt, &tm); + if (s == NULL) { + PyErr_SetString(PyExc_ValueError, "format mismatch"); + return NULL; + } + while (*s && isspace(*s)) + s++; + if (*s) { + PyErr_Format(PyExc_ValueError, + "unconverted data remains: '%.400s'", s); + return NULL; + } + return tmtotuple(&tm); +} +#endif /* HAVE_STRPTIME */ + static PyObject * time_asctime(self, args) PyObject *self; @@ -419,6 +459,9 @@ static PyMethodDef time_methods[] = { #ifdef HAVE_STRFTIME {"strftime", time_strftime, 1}, #endif +#ifdef HAVE_STRPTIME + {"strptime", time_strptime, 1}, +#endif {NULL, NULL} /* sentinel */ }; |