File: timeutils.py

package info (click to toggle)
pypy3 7.3.11%2Bdfsg-2%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 201,024 kB
  • sloc: python: 1,950,308; ansic: 517,580; sh: 21,417; asm: 14,419; cpp: 4,263; makefile: 4,228; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 11; 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