gh-110850: Add PyTime_t C API (GH-115215)

* gh-110850: Add PyTime_t C API

Add PyTime_t API:

* PyTime_t type.
* PyTime_MIN and PyTime_MAX constants.
* PyTime_AsSecondsDouble(), PyTime_Monotonic(),
  PyTime_PerfCounter() and PyTime_GetSystemClock() functions.

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Petr Viktorin 2024-02-12 18:13:10 +01:00 committed by GitHub
parent c39272e143
commit 879f4546bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 448 additions and 114 deletions

View file

@ -43,8 +43,8 @@ class _PyTime(enum.IntEnum):
ROUND_UP = 3
# _PyTime_t is int64_t
_PyTime_MIN = -2 ** 63
_PyTime_MAX = 2 ** 63 - 1
PyTime_MIN = -2 ** 63
PyTime_MAX = 2 ** 63 - 1
# Rounding modes supported by PyTime
ROUNDING_MODES = (
@ -934,7 +934,7 @@ class TestCPyTime(CPyTimeTestCase, unittest.TestCase):
_PyTime_FromSecondsObject(float('nan'), time_rnd)
def test_AsSecondsDouble(self):
from _testinternalcapi import _PyTime_AsSecondsDouble
from _testcapi import PyTime_AsSecondsDouble
def float_converter(ns):
if abs(ns) % SEC_TO_NS == 0:
@ -942,15 +942,10 @@ class TestCPyTime(CPyTimeTestCase, unittest.TestCase):
else:
return float(ns) / SEC_TO_NS
self.check_int_rounding(lambda ns, rnd: _PyTime_AsSecondsDouble(ns),
self.check_int_rounding(lambda ns, rnd: PyTime_AsSecondsDouble(ns),
float_converter,
NS_TO_SEC)
# test nan
for time_rnd, _ in ROUNDING_MODES:
with self.assertRaises(TypeError):
_PyTime_AsSecondsDouble(float('nan'))
def create_decimal_converter(self, denominator):
denom = decimal.Decimal(denominator)
@ -1009,7 +1004,7 @@ class TestCPyTime(CPyTimeTestCase, unittest.TestCase):
tv_sec_max = self.time_t_max
tv_sec_min = self.time_t_min
for t in (_PyTime_MIN, _PyTime_MAX):
for t in (PyTime_MIN, PyTime_MAX):
ts = _PyTime_AsTimeval_clamp(t, _PyTime.ROUND_CEILING)
with decimal.localcontext() as context:
context.rounding = decimal.ROUND_CEILING
@ -1028,7 +1023,7 @@ class TestCPyTime(CPyTimeTestCase, unittest.TestCase):
def test_AsTimespec_clamp(self):
from _testinternalcapi import _PyTime_AsTimespec_clamp
for t in (_PyTime_MIN, _PyTime_MAX):
for t in (PyTime_MIN, PyTime_MAX):
ts = _PyTime_AsTimespec_clamp(t)
tv_sec, tv_nsec = divmod(t, NS_TO_SEC)
if self.time_t_max < tv_sec: