summaryrefslogtreecommitdiffstats
path: root/Include/pytime.h
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-03-27 12:31:18 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-03-27 12:31:18 (GMT)
commitcb29f0177c91ebb739b89d8cc4ba223785c94d61 (patch)
tree986ec5f0229d996767a5c6492debb14f1507a522 /Include/pytime.h
parenta766ddfa2fa0934a3852d74b0630a7bd3c91958e (diff)
downloadcpython-cb29f0177c91ebb739b89d8cc4ba223785c94d61.zip
cpython-cb29f0177c91ebb739b89d8cc4ba223785c94d61.tar.gz
cpython-cb29f0177c91ebb739b89d8cc4ba223785c94d61.tar.bz2
Issue #22117: Add a new Python timestamp format _PyTime_t to pytime.h
In practice, _PyTime_t is a number of nanoseconds. Its C type is a 64-bit signed number. It's integer value is in the range [-2^63; 2^63-1]. In seconds, the range is around [-292 years; +292 years]. In term of Epoch timestamp (1970-01-01), it can store a date between 1677-09-21 and 2262-04-11. The API has a resolution of 1 nanosecond and use integer number. With a resolution on 1 nanosecond, 64-bit IEEE 754 floating point numbers loose precision after 194 days. It's not the case with this API. The drawback is overflow for values outside [-2^63; 2^63-1], but these values are unlikely for most Python modules, except of the datetime module. New functions: - _PyTime_GetMonotonicClock() - _PyTime_FromObject() - _PyTime_AsMilliseconds() - _PyTime_AsTimeval() This change uses these new functions in time.sleep() to avoid rounding issues. The new API will be extended step by step, and the old API will be removed step by step. Currently, some code is duplicated just to be able to move incrementally, instead of pushing a large change at once.
Diffstat (limited to 'Include/pytime.h')
-rw-r--r--Include/pytime.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/Include/pytime.h b/Include/pytime.h
index d46b17c..9cf1170 100644
--- a/Include/pytime.h
+++ b/Include/pytime.h
@@ -111,6 +111,40 @@ _PyTime_AddDouble(_PyTime_timeval *tv, double interval,
Return 0 on success, raise an exception and return -1 on error. */
PyAPI_FUNC(int) _PyTime_Init(void);
+/****************** NEW _PyTime_t API **********************/
+
+#ifdef PY_INT64_T
+typedef PY_INT64_T _PyTime_t;
+#else
+# error "_PyTime_t need signed 64-bit integer type"
+#endif
+
+/* 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_FromObject(_PyTime_t *t,
+ PyObject *obj,
+ _PyTime_round_t round);
+
+/* Convert timestamp to a number of milliseconds (10^-3 seconds). */
+PyAPI_FUNC(_PyTime_t)
+_PyTime_AsMilliseconds(_PyTime_t t,
+ _PyTime_round_t round);
+
+/* Convert a timestamp to a timeval structure. */
+PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t,
+ struct timeval *tv,
+ _PyTime_round_t round);
+
+/* 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.
+
+ The function cannot fail. _PyTime_Init() ensures that a monotonic clock
+ is available and works. */
+PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void);
+
+
#ifdef __cplusplus
}
#endif