File: timeutils.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (40 lines) | stat: -rw-r--r-- 1,349 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
Access to the time module's high-resolution monotonic clock
"""
import math
from rpython.rlib.rarithmetic import (
    r_longlong, ovfcheck_float_to_longlong)
from rpython.rlib import rfloat
from pypy.interpreter.error import oefmt

SECS_TO_NS = 10 ** 9
MS_TO_NS = 10 ** 6
US_TO_NS = 10 ** 3

def monotonic(space):
    from pypy.module.time import interp_time
    if interp_time.HAS_MONOTONIC:
        w_res = interp_time.monotonic(space)
    else:
        w_res = interp_time.gettimeofday(space)
    return space.float_w(w_res)   # xxx back and forth

def timestamp_w(space, w_secs):
    if space.isinstance_w(w_secs, space.w_float):
        secs = space.float_w(w_secs)
        if math.isnan(secs):
            raise oefmt(space.w_ValueError, "timestamp is nan")
        result_float = math.ceil(secs * SECS_TO_NS)
        try:
            return ovfcheck_float_to_longlong(result_float)
        except OverflowError:
            raise oefmt(space.w_OverflowError,
                "timestamp %R too large to convert to C _PyTime_t", w_secs)
    else:
        try:
            sec = space.bigint_w(w_secs).tolonglong()
            result = sec * r_longlong(SECS_TO_NS)
        except OverflowError:
            raise oefmt(space.w_OverflowError,
                "timestamp %R too large to convert to C _PyTime_t", w_secs)
        return result