]> granicus.if.org Git - python/commitdiff
Expose clock_settime() as time.clock_settime()
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 2 Apr 2012 22:45:07 +0000 (00:45 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Mon, 2 Apr 2012 22:45:07 +0000 (00:45 +0200)
Doc/library/time.rst
Lib/test/test_time.py
Modules/timemodule.c

index f87fa64054e203dbd1aeefeaab3de1ed603b748e..8156b018abde95883d7d57afb85ada8d029134b9 100644 (file)
@@ -151,6 +151,13 @@ The module defines the following functions and data items:
    .. versionadded:: 3.3
 
 
+.. function:: clock_settime(clk_id, time)
+
+   Set the time of the specified clock *clk_id*.
+
+   .. versionadded:: 3.3
+
+
 .. data:: CLOCK_REALTIME
 
    System-wide real-time clock. Setting this clock requires appropriate
index 28d018afe7a3c6a2faf4ffe42e9d195d2bc4597c..fb2489c45325a2db2db7ba550d48a5ef0c62e350 100644 (file)
@@ -47,6 +47,17 @@ class TimeTestCase(unittest.TestCase):
         self.assertGreater(res, 0.0)
         self.assertLessEqual(res, 1.0)
 
+    @unittest.skipUnless(hasattr(time, 'clock_settime'),
+                         'need time.clock_settime()')
+    def test_clock_settime(self):
+        t = time.clock_gettime(time.CLOCK_REALTIME)
+        try:
+            time.clock_settime(time.CLOCK_REALTIME, t)
+        except PermissionError:
+            pass
+
+        self.assertRaises(OSError, time.clock_settime, time.CLOCK_MONOTONIC, 0)
+
     def test_conversions(self):
         self.assertEqual(time.ctime(self.t),
                          time.asctime(time.localtime(self.t)))
index 0fe1b17dd4978325e93900c8dfd0b9bf3e24cee1..23f3ddd765ccb2ad9553b93a66faec400ddc176a 100644 (file)
@@ -158,6 +158,33 @@ PyDoc_STRVAR(clock_gettime_doc,
 "clock_gettime(clk_id) -> floating point number\n\
 \n\
 Return the time of the specified clock clk_id.");
+
+static PyObject *
+time_clock_settime(PyObject *self, PyObject *args)
+{
+    clockid_t clk_id;
+    PyObject *obj;
+    struct timespec tp;
+    int ret;
+
+    if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj))
+        return NULL;
+
+    if (_PyTime_ObjectToTimespec(obj, &tp.tv_sec, &tp.tv_nsec) == -1)
+        return NULL;
+
+    ret = clock_settime((clockid_t)clk_id, &tp);
+    if (ret != 0) {
+        PyErr_SetFromErrno(PyExc_IOError);
+        return NULL;
+    }
+    Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(clock_settime_doc,
+"clock_settime(clk_id, time)\n\
+\n\
+Set the time of the specified clock clk_id.");
 #endif
 
 #ifdef HAVE_CLOCK_GETRES
@@ -983,6 +1010,9 @@ static PyMethodDef time_methods[] = {
 #ifdef HAVE_CLOCK_GETTIME
     {"clock_gettime",   time_clock_gettime, METH_VARARGS, clock_gettime_doc},
 #endif
+#ifdef HAVE_CLOCK_GETTIME
+    {"clock_settime",   time_clock_settime, METH_VARARGS, clock_settime_doc},
+#endif
 #ifdef HAVE_CLOCK_GETRES
     {"clock_getres",    time_clock_getres, METH_VARARGS, clock_getres_doc},
 #endif