X-Git-Url: https://git.distorted.org.uk/~mdw/getdate-python/blobdiff_plain/162b088e0939d7191cdd0343bc500ce115a659f1..6f110c11249f7545511839cc9c1ee3891feb2c7e:/module.c diff --git a/module.c b/module.c new file mode 100644 index 0000000..8683d7d --- /dev/null +++ b/module.c @@ -0,0 +1,29 @@ +#include +#include "getdate.h" + +static PyObject *meth_getdate(PyObject *me, PyObject *arg, PyObject *kw) +{ + time_t now, t; + int tnow = -1; + char *p; + static char *kwlist[] = { "string", "now", 0 }; + + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|i:getdate", kwlist, + &p, &tnow)) + return (0); + if (tnow != -1) now = tnow; + if ((t = get_date(p, (tnow == -1) ? 0 : &now)) == (time_t)-1) { + PyErr_SetString(PyExc_SyntaxError, "Bad time string"); + return (0); + } + return (PyInt_FromLong(t)); +} + +static PyMethodDef methods[] = { + { "getdate", (PyCFunction)meth_getdate, METH_VARARGS | METH_KEYWORDS, + "getdate(STRING, now = time.time()) -> TIME" }, + { 0 } +}; + +void initgetdate(void) { Py_InitModule("getdate", methods); } +