diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-10-25 11:06:09 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-10-25 11:06:09 (GMT) |
commit | e0be4232971edca23438cc3d79761141f2de124f (patch) | |
tree | 7149c8f35615c64e122de1478900f5e8cb516c04 /Modules/timemodule.c | |
parent | 92b958420e90b5e98c795db75693310cb9317f95 (diff) | |
download | cpython-e0be4232971edca23438cc3d79761141f2de124f.zip cpython-e0be4232971edca23438cc3d79761141f2de124f.tar.gz cpython-e0be4232971edca23438cc3d79761141f2de124f.tar.bz2 |
Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
the time module. time.clock_gettime(time.CLOCK_MONOTONIC) provides a monotonic
clock
Diffstat (limited to 'Modules/timemodule.c')
-rw-r--r-- | Modules/timemodule.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c index b7604b0..85614a6 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -135,6 +135,54 @@ the first call to clock(). This has as much precision as the system\n\ records."); #endif +#ifdef HAVE_CLOCK_GETTIME +static PyObject * +time_clock_gettime(PyObject *self, PyObject *args) +{ + int ret; + clockid_t clk_id; + struct timespec tp; + + if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id)) + return NULL; + + ret = clock_gettime((clockid_t)clk_id, &tp); + if (ret != 0) + PyErr_SetFromErrno(PyExc_IOError); + + return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); +} + +PyDoc_STRVAR(clock_gettime_doc, +"clock_gettime(clk_id) -> floating point number\n\ +\n\ +Return the time of the specified clock clk_id."); +#endif + +#ifdef HAVE_CLOCK_GETRES +static PyObject * +time_clock_getres(PyObject *self, PyObject *args) +{ + int ret; + clockid_t clk_id; + struct timespec tp; + + if (!PyArg_ParseTuple(args, "i:clock_getres", &clk_id)) + return NULL; + + ret = clock_getres((clockid_t)clk_id, &tp); + if (ret != 0) + PyErr_SetFromErrno(PyExc_IOError); + + return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); +} + +PyDoc_STRVAR(clock_getres_doc, +"clock_getres(clk_id) -> floating point number\n\ +\n\ +Return the resolution (precision) of the specified clock clk_id."); +#endif + static PyObject * time_sleep(PyObject *self, PyObject *args) { @@ -786,6 +834,24 @@ PyInit_timezone(PyObject *m) { Py_BuildValue("(zz)", _tzname[0], _tzname[1])); #endif /* __CYGWIN__ */ #endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ + +#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_GETRES) +#ifdef CLOCK_REALTIME + PyModule_AddIntMacro(m, CLOCK_REALTIME); +#endif +#ifdef CLOCK_MONOTONIC + PyModule_AddIntMacro(m, CLOCK_MONOTONIC); +#endif +#ifdef CLOCK_MONOTONIC_RAW + PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW); +#endif +#ifdef CLOCK_PROCESS_CPUTIME_ID + PyModule_AddIntMacro(m, CLOCK_PROCESS_CPUTIME_ID); +#endif +#ifdef CLOCK_THREAD_CPUTIME_ID + PyModule_AddIntMacro(m, CLOCK_THREAD_CPUTIME_ID); +#endif +#endif /* HAVE_CLOCK_GETTIME */ } @@ -794,6 +860,12 @@ static PyMethodDef time_methods[] = { #if (defined(MS_WINDOWS) && !defined(__BORLANDC__)) || defined(HAVE_CLOCK) {"clock", time_clock, METH_NOARGS, clock_doc}, #endif +#ifdef HAVE_CLOCK_GETTIME + {"clock_gettime", time_clock_gettime, METH_VARARGS, clock_gettime_doc}, +#endif +#ifdef HAVE_CLOCK_GETRES + {"clock_getres", time_clock_getres, METH_VARARGS, clock_getres_doc}, +#endif {"sleep", time_sleep, METH_VARARGS, sleep_doc}, {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc}, {"localtime", time_localtime, METH_VARARGS, localtime_doc}, |