# error "_PyTime_t need signed 64-bit integer type"
#endif
+/* Create a timestamp from a number of nanoseconds (C long). */
+PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(PY_LONG_LONG ns);
+
/* Convert a Python float or int to a timetamp.
Raise an exception and return -1 on error, return 0 on success. */
PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t,
PyObject *obj,
_PyTime_round_t round);
+/* Convert a timestamp to a number of seconds as a C double. */
+PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t);
+
/* Convert timestamp to a number of milliseconds (10^-3 seconds). */
PyAPI_FUNC(_PyTime_t) _PyTime_AsMilliseconds(_PyTime_t t,
_PyTime_round_t round);
object. */
PyAPI_FUNC(PyObject *) _PyTime_AsNanosecondsObject(_PyTime_t t);
-/* Convert a timestamp to a timeval structure. */
+/* Convert a timestamp to a timeval structure (microsecond resolution).
+ Raise an exception and return -1 on error, return 0 on success. */
PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t,
struct timeval *tv,
_PyTime_round_t round);
is available and works. */
PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void);
+/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
+ The clock is not affected by system clock updates. The reference point of
+ the returned value is undefined, so that only the difference between the
+ results of consecutive calls is valid.
+
+ Fill info (if set) with information of the function used to get the time.
+
+ Return 0 on success, raise an exception and return -1 on error. */
+PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo(
+ _PyTime_t *t,
+ _Py_clock_info_t *info);
+
#ifdef __cplusplus
}
TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1
TIME_MINYEAR = -TIME_MAXYEAR - 1
+SEC_TO_NS = 10 ** 9
class _PyTime(enum.IntEnum):
# Round towards zero
@support.cpython_only
class TestPyTime_t(unittest.TestCase):
def test_FromSecondsObject(self):
- from _testcapi import pytime_fromsecondsobject
- SEC_TO_NS = 10 ** 9
- MAX_SEC = 2 ** 63 // 10 ** 9
+ from _testcapi import PyTime_FromSecondsObject
# Conversion giving the same result for all rounding methods
for rnd in ALL_ROUNDING_METHODS:
(2**25 , 33554432000000000),
(2**25 + 1e-9, 33554432000000000),
- # close to 2^63 nanoseconds
+ # close to 2^63 nanoseconds (_PyTime_t limit)
(9223372036, 9223372036 * SEC_TO_NS),
(9223372036.0, 9223372036 * SEC_TO_NS),
(-9223372036, -9223372036 * SEC_TO_NS),
(-9223372036.0, -9223372036 * SEC_TO_NS),
):
with self.subTest(obj=obj, round=rnd, timestamp=ts):
- self.assertEqual(pytime_fromsecondsobject(obj, rnd), ts)
+ self.assertEqual(PyTime_FromSecondsObject(obj, rnd), ts)
with self.subTest(round=rnd):
with self.assertRaises(OverflowError):
- pytime_fromsecondsobject(9223372037, rnd)
- pytime_fromsecondsobject(9223372037.0, rnd)
- pytime_fromsecondsobject(-9223372037, rnd)
- pytime_fromsecondsobject(-9223372037.0, rnd)
+ PyTime_FromSecondsObject(9223372037, rnd)
+ PyTime_FromSecondsObject(9223372037.0, rnd)
+ PyTime_FromSecondsObject(-9223372037, rnd)
+ PyTime_FromSecondsObject(-9223372037.0, rnd)
# Conversion giving different results depending on the rounding method
UP = _PyTime.ROUND_UP
(-0.9999999999, -1000000000, UP),
):
with self.subTest(obj=obj, round=rnd, timestamp=ts):
- self.assertEqual(pytime_fromsecondsobject(obj, rnd), ts)
+ self.assertEqual(PyTime_FromSecondsObject(obj, rnd), ts)
+
+ def test_AsSecondsDouble(self):
+ from _testcapi import PyTime_AsSecondsDouble
+
+ for nanoseconds, seconds in (
+ # near 1 nanosecond
+ ( 0, 0.0),
+ ( 1, 1e-9),
+ (-1, -1e-9),
+
+ # near 1 second
+ (SEC_TO_NS + 1, 1.0 + 1e-9),
+ (SEC_TO_NS, 1.0),
+ (SEC_TO_NS - 1, 1.0 - 1e-9),
+
+ # a few seconds
+ (123 * SEC_TO_NS, 123.0),
+ (-567 * SEC_TO_NS, -567.0),
+
+ # nanosecond are kept for value <= 2^23 seconds
+ (4194303999999999, 2**22 - 1e-9),
+ (4194304000000000, 2**22),
+ (4194304000000001, 2**22 + 1e-9),
+
+ # start loosing precision for value > 2^23 seconds
+ (8388608000000002, 2**23 + 1e-9),
+
+ # nanoseconds are lost for value > 2^23 seconds
+ (16777215999999998, 2**24 - 1e-9),
+ (16777215999999999, 2**24 - 1e-9),
+ (16777216000000000, 2**24 ),
+ (16777216000000001, 2**24 ),
+ (16777216000000002, 2**24 + 2e-9),
+
+ (33554432000000000, 2**25 ),
+ (33554432000000002, 2**25 ),
+ (33554432000000004, 2**25 + 4e-9),
+
+ # close to 2^63 nanoseconds (_PyTime_t limit)
+ (9223372036 * SEC_TO_NS, 9223372036.0),
+ (-9223372036 * SEC_TO_NS, -9223372036.0),
+ ):
+ with self.subTest(nanoseconds=nanoseconds, seconds=seconds):
+ self.assertEqual(PyTime_AsSecondsDouble(nanoseconds),
+ seconds)
if __name__ == "__main__":
return _PyTime_AsNanosecondsObject(ts);
}
+static PyObject *
+test_pytime_assecondsdouble(PyObject *self, PyObject *args)
+{
+ PY_LONG_LONG ns;
+ _PyTime_t ts;
+ double d;
+
+ if (!PyArg_ParseTuple(args, "L", &ns))
+ return NULL;
+ ts = _PyTime_FromNanoseconds(ns);
+ d = _PyTime_AsSecondsDouble(ts);
+ return PyFloat_FromDouble(d);
+}
+
static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS},
return_null_without_error, METH_NOARGS},
{"return_result_with_error",
return_result_with_error, METH_NOARGS},
- {"pytime_fromsecondsobject", test_pytime_fromsecondsobject, METH_VARARGS},
+ {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS},
+ {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
{NULL, NULL} /* sentinel */
};