diff -r e505ce0514ff Doc/library/time.rst --- a/Doc/library/time.rst Wed Mar 28 02:54:15 2012 +0200 +++ b/Doc/library/time.rst Wed Mar 28 03:11:13 2012 +0200 @@ -207,6 +207,17 @@ The module defines the following functio function. +.. function:: hires() + + .. index:: + single: benchmarking + + Monotonic clock, or fall back to the system clock if the monotonic clock + failed or if no monotonic clock is available. + + .. versionadded:: 3.3 + + .. function:: localtime([secs]) Like :func:`gmtime` but converts to local time. If *secs* is not provided or @@ -226,20 +237,16 @@ The module defines the following functio The earliest date for which it can generate a time is platform-dependent. -.. function:: steady(strict=False) - - .. index:: - single: benchmarking +.. function:: monotonic() Return the current time as a floating point number expressed in seconds. This clock advances at a steady rate relative to real time and it may not be adjusted. The reference point of the returned value is undefined so only the difference of consecutive calls is valid. - If available, a monotonic clock is used. By default, - the function falls back to another clock if the monotonic clock failed or is - not available. If *strict* is True, raise an :exc:`OSError` on error or - :exc:`NotImplementedError` if no monotonic clock is available. + Raise an :exc:`OSError` on error. + + Availability: Windows, Mac OS X, Unix. .. versionadded:: 3.3 diff -r e505ce0514ff Modules/timemodule.c --- a/Modules/timemodule.c Wed Mar 28 02:54:15 2012 +0200 +++ b/Modules/timemodule.c Wed Mar 28 03:11:13 2012 +0200 @@ -762,11 +762,17 @@ the local timezone used by methods such should not be relied on."); #endif /* HAVE_WORKING_TZSET */ +#if ((defined(MS_WINDOWS) && !defined(__BORLANDC__)) \ + || defined(__APPLE__) \ + || defined(HAVE_CLOCK_GETTIME)) +#define PYMONOTONIC +#endif + static PyObject* -steady_clock(int strict) +pymonotonic(int fallback) { #if defined(MS_WINDOWS) && !defined(__BORLANDC__) - return win32_clock(!strict); + return win32_clock(fallback); #elif defined(__APPLE__) static mach_timebase_info_data_t timebase; uint64_t time; @@ -804,7 +810,7 @@ steady_clock(int strict) int ret; struct timespec tp; - if (strict) { + if (!fallback) { clk_index = &monotonic_clk_index; clk_ids = monotonic_clk_ids; clk_ids_len = Py_ARRAY_LENGTH(monotonic_clk_ids); @@ -825,42 +831,43 @@ steady_clock(int strict) if (clk_ids_len <= *clk_index) (*clk_index) = -1; } - if (strict) { + if (!fallback) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } return floattime(); #else - if (strict) { - PyErr_SetString(PyExc_NotImplementedError, - "no steady clock available on your platform"); - return NULL; - } + assert(fallback); return floattime(); #endif } static PyObject * -time_steady(PyObject *self, PyObject *args, PyObject *kwargs) +time_hires(PyObject *self, PyObject *unused) { - static char *kwlist[] = {"strict", NULL}; - int strict = 0; - - if (!PyArg_ParseTupleAndKeywords( - args, kwargs, "|i:steady", kwlist, - &strict)) - return NULL; - - return steady_clock(strict); + return pymonotonic(0); } -PyDoc_STRVAR(steady_doc, -"steady(strict=False) -> float\n\ +PyDoc_STRVAR(hires_doc, +"hires() -> float\n\ +\n\ +Monotonic clock or fallback to the system clock."); + +#ifdef PYMONOTONIC +static PyObject * +time_monotonic(PyObject *self, PyObject *unused) +{ + return pymonotonic(1); +} + +PyDoc_STRVAR(monotonic_doc, +"monotonic() -> float\n\ \n\ Return the current time as a floating point number expressed in seconds.\n\ This clock advances at a steady rate relative to real time and it may not\n\ be adjusted. The reference point of the returned value is undefined so only\n\ the difference of consecutive calls is valid."); +#endif static void @@ -991,8 +998,6 @@ static PyMethodDef time_methods[] = { #ifdef HAVE_MKTIME {"mktime", time_mktime, METH_O, mktime_doc}, #endif - {"steady", (PyCFunction)time_steady, METH_VARARGS|METH_KEYWORDS, - steady_doc}, #ifdef HAVE_STRFTIME {"strftime", time_strftime, METH_VARARGS, strftime_doc}, #endif @@ -1000,6 +1005,10 @@ static PyMethodDef time_methods[] = { #ifdef HAVE_WORKING_TZSET {"tzset", time_tzset, METH_NOARGS, tzset_doc}, #endif +#ifdef PYMONOTONIC + {"monotonic", time_monotonic, METH_NOARGS, monotonic_doc}, +#endif + {"hires", time_hires, METH_NOARGS, hires_doc}, {NULL, NULL} /* sentinel */ };