summaryrefslogtreecommitdiffstats
path: root/Modules/timemodule.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2012-03-12 23:25:42 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2012-03-12 23:25:42 (GMT)
commita8ec5ea923c86415835ddecae795071804dca69d (patch)
treec22774acfa0c7f4036b94fec410e67d492c7bbf0 /Modules/timemodule.c
parentcad1a07becec983d35c440c63150a1e73cacd7c7 (diff)
downloadcpython-a8ec5ea923c86415835ddecae795071804dca69d.zip
cpython-a8ec5ea923c86415835ddecae795071804dca69d.tar.gz
cpython-a8ec5ea923c86415835ddecae795071804dca69d.tar.bz2
Issue #14104: Implement time.monotonic() on Mac OS X,
patch written by Nicholas Riley.
Diffstat (limited to 'Modules/timemodule.c')
-rw-r--r--Modules/timemodule.c18
1 files changed, 17 insertions, 1 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index 59fe3ee..6ebd3ef 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -40,6 +40,10 @@
#include <sys/time.h>
#endif
+#if defined(__APPLE__)
+#include <mach/mach_time.h>
+#endif
+
/* Forward declarations */
static int floatsleep(double);
static double floattime(void);
@@ -816,7 +820,8 @@ of the returned value is undefined so only the difference of consecutive\n\
calls is valid.");
#if (defined(MS_WINDOWS) && !defined(__BORLANDC__)) \
- || (defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC))
+ || (defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)) \
+ || (defined(__APPLE__))
# define HAVE_PYTIME_MONOTONIC
#endif
@@ -826,6 +831,17 @@ time_monotonic(PyObject *self, PyObject *unused)
{
#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
return win32_clock(0);
+#elif defined(__APPLE__)
+ uint64_t time = mach_absolute_time();
+ double secs;
+
+ static mach_timebase_info_data_t timebase;
+ if (timebase.denom == 0)
+ mach_timebase_info(&timebase);
+
+ secs = (double)time * timebase.numer / timebase.denom * 1e-9;
+
+ return PyFloat_FromDouble(secs);
#else
static int clk_index = 0;
clockid_t clk_ids[] = {