mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Issue #22117: time.monotonic() now uses the new _PyTime_t API
* Add _PyTime_FromNanoseconds() * Add _PyTime_AsSecondsDouble() * Add unit tests for _PyTime_AsSecondsDouble()
This commit is contained in:
parent
52d1493c0c
commit
4bfb460d88
5 changed files with 121 additions and 15 deletions
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue