| 1 |
""" |
| 2 |
Access to the time module's high-resolution monotonic clock |
| 3 |
""" |
| 4 |
import math |
| 5 |
from rpython.rlib.rarithmetic import ( |
| 6 |
r_longlong, ovfcheck_float_to_longlong) |
| 7 |
from rpython.rlib import rfloat |
| 8 |
from pypy.interpreter.error import oefmt |
| 9 |
|
| 10 |
SECS_TO_NS = 10 ** 9 |
| 11 |
MS_TO_NS = 10 ** 6 |
| 12 |
US_TO_NS = 10 ** 3 |
| 13 |
|
| 14 |
def monotonic(space): |
| 15 |
"""Call time.monotonic and return a unwrapped float""" |
| 16 |
|
| 17 |
from pypy.module.time import interp_time |
| 18 |
return interp_time._monotonic(space) |
| 19 |
|
| 20 |
def timestamp_w(space, w_secs): |
| 21 |
if space.isinstance_w(w_secs, space.w_float): |
| 22 |
secs = space.float_w(w_secs) |
| 23 |
if math.isnan(secs): |
| 24 |
raise oefmt(space.w_ValueError, "timestamp is nan") |
| 25 |
result_float = math.ceil(secs * SECS_TO_NS) |
| 26 |
try: |
| 27 |
return ovfcheck_float_to_longlong(result_float) |
| 28 |
except OverflowError: |
| 29 |
raise oefmt(space.w_OverflowError, |
| 30 |
"timestamp %R too large to convert to C _PyTime_t", w_secs) |
| 31 |
else: |
| 32 |
try: |
| 33 |
sec = space.bigint_w(w_secs).tolonglong() |
| 34 |
result = sec * r_longlong(SECS_TO_NS) |
| 35 |
except OverflowError: |
| 36 |
raise oefmt(space.w_OverflowError, |
| 37 |
"timestamp %R too large to convert to C _PyTime_t", w_secs) |
| 38 |
return result |