From 4bfb460d883bd224b61a0e7403a09ea093947890 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 27 Mar 2015 22:27:24 +0100 Subject: Issue #22117: time.monotonic() now uses the new _PyTime_t API * Add _PyTime_FromNanoseconds() * Add _PyTime_AsSecondsDouble() * Add unit tests for _PyTime_AsSecondsDouble() --- Python/pytime.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'Python/pytime.c') diff --git a/Python/pytime.c b/Python/pytime.c index 2aeeddc943..a4963357df 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -405,6 +405,15 @@ _PyTime_overflow(void) "timestamp too large to convert to C _PyTime_t"); } +_PyTime_t +_PyTime_FromNanoseconds(PY_LONG_LONG ns) +{ + _PyTime_t t; + assert(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t)); + t = Py_SAFE_DOWNCAST(ns, PY_LONG_LONG, _PyTime_t); + return t; +} + #if !defined(MS_WINDOWS) && !defined(__APPLE__) static int _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts) @@ -470,6 +479,17 @@ _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) } } +double +_PyTime_AsSecondsDouble(_PyTime_t t) +{ + _PyTime_t sec, ns; + /* Divide using integers to avoid rounding issues on the integer part. + 1e-9 cannot be stored exactly in IEEE 64-bit. */ + sec = t / SEC_TO_NS; + ns = t % SEC_TO_NS; + return (double)sec + (double)ns * 1e-9; +} + PyObject * _PyTime_AsNanosecondsObject(_PyTime_t t) { @@ -660,6 +680,12 @@ _PyTime_GetMonotonicClock(void) return t; } +int +_PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) +{ + return pymonotonic_new(tp, info, 1); +} + int _PyTime_Init(void) { -- cgit v1.2.1