summaryrefslogtreecommitdiff
path: root/Modules/timemodule.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-10-25 13:06:09 +0200
committerVictor Stinner <victor.stinner@haypocalc.com>2011-10-25 13:06:09 +0200
commit0153bffba3fca23b8b2d194b1f4508dbcf3460ac (patch)
tree3f65040666abd0e8a0630be06ec82e369fd71dc3 /Modules/timemodule.c
parent0669010b837f98ce796c26c0d8802ec5f0403b3a (diff)
downloadcpython-0153bffba3fca23b8b2d194b1f4508dbcf3460ac.tar.gz
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.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index b7604b0c49..85614a6e58 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},