Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e0be423

Browse files
author
Victor Stinner
committed
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
1 parent 92b9584 commit e0be423

9 files changed

Lines changed: 615 additions & 288 deletions

File tree

Doc/library/time.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,54 @@ The module defines the following functions and data items:
136136
microsecond.
137137

138138

139+
.. function:: clock_getres(clk_id)
140+
141+
Return the resolution (precision) of the specified clock *clk_id*.
142+
143+
.. versionadded:: 3.3
144+
145+
.. function:: clock_gettime(clk_id)
146+
147+
Return the time of the specified clock *clk_id*.
148+
149+
.. versionadded:: 3.3
150+
151+
.. data:: CLOCK_REALTIME
152+
153+
System-wide real-time clock. Setting this clock requires appropriate
154+
privileges.
155+
156+
.. versionadded:: 3.3
157+
158+
.. data:: CLOCK_MONOTONIC
159+
160+
Clock that cannot be set and represents monotonic time since some
161+
unspecified starting point.
162+
163+
.. versionadded:: 3.3
164+
165+
.. data:: CLOCK_MONOTONIC_RAW
166+
167+
Similar to :data:`CLOCK_MONOTONIC`, but provides access to a raw
168+
hardware-based time that is not subject to NTP adjustments.
169+
170+
Availability: Linux 2.6.28 or later.
171+
172+
.. versionadded:: 3.3
173+
174+
.. data:: CLOCK_PROCESS_CPUTIME_ID
175+
176+
High-resolution per-process timer from the CPU.
177+
178+
.. versionadded:: 3.3
179+
180+
.. data:: CLOCK_THREAD_CPUTIME_ID
181+
182+
Thread-specific CPU-time clock.
183+
184+
.. versionadded:: 3.3
185+
186+
139187
.. function:: ctime([secs])
140188

141189
Convert a time expressed in seconds since the epoch to a string representing

Doc/whatsnew/3.3.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,16 @@ New module: :mod:`faulthandler`.
272272
* :envvar:`PYTHONFAULTHANDLER`
273273
* :option:`-X` ``faulthandler``
274274

275+
time
276+
----
277+
278+
* The :mod:`time` module has new :func:`~time.clock_getres` and
279+
:func:`~time.clock_gettime` functions and ``CLOCK_xxx`` constants.
280+
:func:`~time.clock_gettime` can be used with :data:`time.CLOCK_MONOTONIC` to
281+
get a monotonic clock.
282+
283+
(Contributed by Victor Stinner in :issue:`10278`)
284+
275285

276286
ftplib
277287
------

Lib/test/test_time.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@ def test_data_attributes(self):
2020
def test_clock(self):
2121
time.clock()
2222

23+
@unittest.skipUnless(hasattr(time, 'clock_gettime'),
24+
'need time.clock_gettime()')
25+
def test_clock_realtime(self):
26+
time.clock_gettime(time.CLOCK_REALTIME)
27+
28+
@unittest.skipUnless(hasattr(time, 'clock_gettime'),
29+
'need time.clock_gettime()')
30+
@unittest.skipUnless(hasattr(time, 'CLOCK_MONOTONIC'),
31+
'need time.CLOCK_MONOTONIC')
32+
def test_clock_monotonic(self):
33+
a = time.clock_gettime(time.CLOCK_MONOTONIC)
34+
b = time.clock_gettime(time.CLOCK_MONOTONIC)
35+
self.assertLessEqual(a, b)
36+
37+
@unittest.skipUnless(hasattr(time, 'clock_getres'),
38+
'need time.clock_getres()')
39+
def test_clock_getres(self):
40+
res = time.clock_getres(time.CLOCK_REALTIME)
41+
self.assertGreater(res, 0.0)
42+
self.assertLessEqual(res, 1.0)
43+
2344
def test_conversions(self):
2445
self.assertEqual(time.ctime(self.t),
2546
time.asctime(time.localtime(self.t)))

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ Core and Builtins
341341
Library
342342
-------
343343

344+
- Issue #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
345+
the time module. time.clock_gettime(time.CLOCK_MONOTONIC) provides a
346+
monotonic clock
347+
344348
- Issue #10332: multiprocessing: fix a race condition when a Pool is closed
345349
before all tasks have completed.
346350

Modules/timemodule.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,54 @@ the first call to clock(). This has as much precision as the system\n\
135135
records.");
136136
#endif
137137

138+
#ifdef HAVE_CLOCK_GETTIME
139+
static PyObject *
140+
time_clock_gettime(PyObject *self, PyObject *args)
141+
{
142+
int ret;
143+
clockid_t clk_id;
144+
struct timespec tp;
145+
146+
if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id))
147+
return NULL;
148+
149+
ret = clock_gettime((clockid_t)clk_id, &tp);
150+
if (ret != 0)
151+
PyErr_SetFromErrno(PyExc_IOError);
152+
153+
return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
154+
}
155+
156+
PyDoc_STRVAR(clock_gettime_doc,
157+
"clock_gettime(clk_id) -> floating point number\n\
158+
\n\
159+
Return the time of the specified clock clk_id.");
160+
#endif
161+
162+
#ifdef HAVE_CLOCK_GETRES
163+
static PyObject *
164+
time_clock_getres(PyObject *self, PyObject *args)
165+
{
166+
int ret;
167+
clockid_t clk_id;
168+
struct timespec tp;
169+
170+
if (!PyArg_ParseTuple(args, "i:clock_getres", &clk_id))
171+
return NULL;
172+
173+
ret = clock_getres((clockid_t)clk_id, &tp);
174+
if (ret != 0)
175+
PyErr_SetFromErrno(PyExc_IOError);
176+
177+
return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
178+
}
179+
180+
PyDoc_STRVAR(clock_getres_doc,
181+
"clock_getres(clk_id) -> floating point number\n\
182+
\n\
183+
Return the resolution (precision) of the specified clock clk_id.");
184+
#endif
185+
138186
static PyObject *
139187
time_sleep(PyObject *self, PyObject *args)
140188
{
@@ -786,13 +834,37 @@ PyInit_timezone(PyObject *m) {
786834
Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
787835
#endif /* __CYGWIN__ */
788836
#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
837+
838+
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_GETRES)
839+
#ifdef CLOCK_REALTIME
840+
PyModule_AddIntMacro(m, CLOCK_REALTIME);
841+
#endif
842+
#ifdef CLOCK_MONOTONIC
843+
PyModule_AddIntMacro(m, CLOCK_MONOTONIC);
844+
#endif
845+
#ifdef CLOCK_MONOTONIC_RAW
846+
PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW);
847+
#endif
848+
#ifdef CLOCK_PROCESS_CPUTIME_ID
849+
PyModule_AddIntMacro(m, CLOCK_PROCESS_CPUTIME_ID);
850+
#endif
851+
#ifdef CLOCK_THREAD_CPUTIME_ID
852+
PyModule_AddIntMacro(m, CLOCK_THREAD_CPUTIME_ID);
853+
#endif
854+
#endif /* HAVE_CLOCK_GETTIME */
789855
}
790856

791857

792858
static PyMethodDef time_methods[] = {
793859
{"time", time_time, METH_NOARGS, time_doc},
794860
#if (defined(MS_WINDOWS) && !defined(__BORLANDC__)) || defined(HAVE_CLOCK)
795861
{"clock", time_clock, METH_NOARGS, clock_doc},
862+
#endif
863+
#ifdef HAVE_CLOCK_GETTIME
864+
{"clock_gettime", time_clock_gettime, METH_VARARGS, clock_gettime_doc},
865+
#endif
866+
#ifdef HAVE_CLOCK_GETRES
867+
{"clock_getres", time_clock_getres, METH_VARARGS, clock_getres_doc},
796868
#endif
797869
{"sleep", time_sleep, METH_VARARGS, sleep_doc},
798870
{"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},

0 commit comments

Comments
 (0)