pytime.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // The _PyTime_t API is written to use timestamp and timeout values stored in
  2. // various formats and to read clocks.
  3. //
  4. // The _PyTime_t type is an integer to support directly common arithmetic
  5. // operations like t1 + t2.
  6. //
  7. // The _PyTime_t API supports a resolution of 1 nanosecond. The _PyTime_t type
  8. // is signed to support negative timestamps. The supported range is around
  9. // [-292.3 years; +292.3 years]. Using the Unix epoch (January 1st, 1970), the
  10. // supported date range is around [1677-09-21; 2262-04-11].
  11. //
  12. // Formats:
  13. //
  14. // * seconds
  15. // * seconds as a floating pointer number (C double)
  16. // * milliseconds (10^-3 seconds)
  17. // * microseconds (10^-6 seconds)
  18. // * 100 nanoseconds (10^-7 seconds)
  19. // * nanoseconds (10^-9 seconds)
  20. // * timeval structure, 1 microsecond resolution (10^-6 seconds)
  21. // * timespec structure, 1 nanosecond resolution (10^-9 seconds)
  22. //
  23. // Integer overflows are detected and raise OverflowError. Conversion to a
  24. // resolution worse than 1 nanosecond is rounded correctly with the requested
  25. // rounding mode. There are 4 rounding modes: floor (towards -inf), ceiling
  26. // (towards +inf), half even and up (away from zero).
  27. //
  28. // Some functions clamp the result in the range [_PyTime_MIN; _PyTime_MAX], so
  29. // the caller doesn't have to handle errors and doesn't need to hold the GIL.
  30. // For example, _PyTime_Add(t1, t2) computes t1+t2 and clamp the result on
  31. // overflow.
  32. //
  33. // Clocks:
  34. //
  35. // * System clock
  36. // * Monotonic clock
  37. // * Performance counter
  38. //
  39. // Operations like (t * k / q) with integers are implemented in a way to reduce
  40. // the risk of integer overflow. Such operation is used to convert a clock
  41. // value expressed in ticks with a frequency to _PyTime_t, like
  42. // QueryPerformanceCounter() with QueryPerformanceFrequency().
  43. #ifndef Py_LIMITED_API
  44. #ifndef Py_PYTIME_H
  45. #define Py_PYTIME_H
  46. /**************************************************************************
  47. Symbols and macros to supply platform-independent interfaces to time related
  48. functions and constants
  49. **************************************************************************/
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. #ifdef __clang__
  54. struct timeval;
  55. #endif
  56. /* _PyTime_t: Python timestamp with subsecond precision. It can be used to
  57. store a duration, and so indirectly a date (related to another date, like
  58. UNIX epoch). */
  59. typedef int64_t _PyTime_t;
  60. // _PyTime_MIN nanoseconds is around -292.3 years
  61. #define _PyTime_MIN INT64_MIN
  62. // _PyTime_MAX nanoseconds is around +292.3 years
  63. #define _PyTime_MAX INT64_MAX
  64. #define _SIZEOF_PYTIME_T 8
  65. typedef enum {
  66. /* Round towards minus infinity (-inf).
  67. For example, used to read a clock. */
  68. _PyTime_ROUND_FLOOR=0,
  69. /* Round towards infinity (+inf).
  70. For example, used for timeout to wait "at least" N seconds. */
  71. _PyTime_ROUND_CEILING=1,
  72. /* Round to nearest with ties going to nearest even integer.
  73. For example, used to round from a Python float. */
  74. _PyTime_ROUND_HALF_EVEN=2,
  75. /* Round away from zero
  76. For example, used for timeout. _PyTime_ROUND_CEILING rounds
  77. -1e-9 to 0 milliseconds which causes bpo-31786 issue.
  78. _PyTime_ROUND_UP rounds -1e-9 to -1 millisecond which keeps
  79. the timeout sign as expected. select.poll(timeout) must block
  80. for negative values." */
  81. _PyTime_ROUND_UP=3,
  82. /* _PyTime_ROUND_TIMEOUT (an alias for _PyTime_ROUND_UP) should be
  83. used for timeouts. */
  84. _PyTime_ROUND_TIMEOUT = _PyTime_ROUND_UP
  85. } _PyTime_round_t;
  86. /* Convert a time_t to a PyLong. */
  87. PyAPI_FUNC(PyObject *) _PyLong_FromTime_t(
  88. time_t sec);
  89. /* Convert a PyLong to a time_t. */
  90. PyAPI_FUNC(time_t) _PyLong_AsTime_t(
  91. PyObject *obj);
  92. /* Convert a number of seconds, int or float, to time_t. */
  93. PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
  94. PyObject *obj,
  95. time_t *sec,
  96. _PyTime_round_t);
  97. /* Convert a number of seconds, int or float, to a timeval structure.
  98. usec is in the range [0; 999999] and rounded towards zero.
  99. For example, -1.2 is converted to (-2, 800000). */
  100. PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
  101. PyObject *obj,
  102. time_t *sec,
  103. long *usec,
  104. _PyTime_round_t);
  105. /* Convert a number of seconds, int or float, to a timespec structure.
  106. nsec is in the range [0; 999999999] and rounded towards zero.
  107. For example, -1.2 is converted to (-2, 800000000). */
  108. PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
  109. PyObject *obj,
  110. time_t *sec,
  111. long *nsec,
  112. _PyTime_round_t);
  113. /* Create a timestamp from a number of seconds. */
  114. PyAPI_FUNC(_PyTime_t) _PyTime_FromSeconds(int seconds);
  115. /* Macro to create a timestamp from a number of seconds, no integer overflow.
  116. Only use the macro for small values, prefer _PyTime_FromSeconds(). */
  117. #define _PYTIME_FROMSECONDS(seconds) \
  118. ((_PyTime_t)(seconds) * (1000 * 1000 * 1000))
  119. /* Create a timestamp from a number of nanoseconds. */
  120. PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(_PyTime_t ns);
  121. /* Create a timestamp from a number of microseconds.
  122. * Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow. */
  123. PyAPI_FUNC(_PyTime_t) _PyTime_FromMicrosecondsClamp(_PyTime_t us);
  124. /* Create a timestamp from nanoseconds (Python int). */
  125. PyAPI_FUNC(int) _PyTime_FromNanosecondsObject(_PyTime_t *t,
  126. PyObject *obj);
  127. /* Convert a number of seconds (Python float or int) to a timestamp.
  128. Raise an exception and return -1 on error, return 0 on success. */
  129. PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t,
  130. PyObject *obj,
  131. _PyTime_round_t round);
  132. /* Convert a number of milliseconds (Python float or int, 10^-3) to a timestamp.
  133. Raise an exception and return -1 on error, return 0 on success. */
  134. PyAPI_FUNC(int) _PyTime_FromMillisecondsObject(_PyTime_t *t,
  135. PyObject *obj,
  136. _PyTime_round_t round);
  137. /* Convert a timestamp to a number of seconds as a C double. */
  138. PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t);
  139. /* Convert timestamp to a number of milliseconds (10^-3 seconds). */
  140. PyAPI_FUNC(_PyTime_t) _PyTime_AsMilliseconds(_PyTime_t t,
  141. _PyTime_round_t round);
  142. /* Convert timestamp to a number of microseconds (10^-6 seconds). */
  143. PyAPI_FUNC(_PyTime_t) _PyTime_AsMicroseconds(_PyTime_t t,
  144. _PyTime_round_t round);
  145. /* Convert timestamp to a number of nanoseconds (10^-9 seconds). */
  146. PyAPI_FUNC(_PyTime_t) _PyTime_AsNanoseconds(_PyTime_t t);
  147. #ifdef MS_WINDOWS
  148. // Convert timestamp to a number of 100 nanoseconds (10^-7 seconds).
  149. PyAPI_FUNC(_PyTime_t) _PyTime_As100Nanoseconds(_PyTime_t t,
  150. _PyTime_round_t round);
  151. #endif
  152. /* Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int
  153. object. */
  154. PyAPI_FUNC(PyObject *) _PyTime_AsNanosecondsObject(_PyTime_t t);
  155. #ifndef MS_WINDOWS
  156. /* Create a timestamp from a timeval structure.
  157. Raise an exception and return -1 on overflow, return 0 on success. */
  158. PyAPI_FUNC(int) _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv);
  159. #endif
  160. /* Convert a timestamp to a timeval structure (microsecond resolution).
  161. tv_usec is always positive.
  162. Raise an exception and return -1 if the conversion overflowed,
  163. return 0 on success. */
  164. PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t,
  165. struct timeval *tv,
  166. _PyTime_round_t round);
  167. /* Similar to _PyTime_AsTimeval() but don't raise an exception on overflow.
  168. On overflow, clamp tv_sec to _PyTime_t min/max. */
  169. PyAPI_FUNC(void) _PyTime_AsTimeval_clamp(_PyTime_t t,
  170. struct timeval *tv,
  171. _PyTime_round_t round);
  172. /* Convert a timestamp to a number of seconds (secs) and microseconds (us).
  173. us is always positive. This function is similar to _PyTime_AsTimeval()
  174. except that secs is always a time_t type, whereas the timeval structure
  175. uses a C long for tv_sec on Windows.
  176. Raise an exception and return -1 if the conversion overflowed,
  177. return 0 on success. */
  178. PyAPI_FUNC(int) _PyTime_AsTimevalTime_t(
  179. _PyTime_t t,
  180. time_t *secs,
  181. int *us,
  182. _PyTime_round_t round);
  183. #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
  184. /* Create a timestamp from a timespec structure.
  185. Raise an exception and return -1 on overflow, return 0 on success. */
  186. PyAPI_FUNC(int) _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts);
  187. /* Convert a timestamp to a timespec structure (nanosecond resolution).
  188. tv_nsec is always positive.
  189. Raise an exception and return -1 on error, return 0 on success. */
  190. PyAPI_FUNC(int) _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts);
  191. /* Similar to _PyTime_AsTimespec() but don't raise an exception on overflow.
  192. On overflow, clamp tv_sec to _PyTime_t min/max. */
  193. PyAPI_FUNC(void) _PyTime_AsTimespec_clamp(_PyTime_t t, struct timespec *ts);
  194. #endif
  195. // Compute t1 + t2. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
  196. PyAPI_FUNC(_PyTime_t) _PyTime_Add(_PyTime_t t1, _PyTime_t t2);
  197. /* Compute ticks * mul / div.
  198. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
  199. The caller must ensure that ((div - 1) * mul) cannot overflow. */
  200. PyAPI_FUNC(_PyTime_t) _PyTime_MulDiv(_PyTime_t ticks,
  201. _PyTime_t mul,
  202. _PyTime_t div);
  203. /* Structure used by time.get_clock_info() */
  204. typedef struct {
  205. const char *implementation;
  206. int monotonic;
  207. int adjustable;
  208. double resolution;
  209. } _Py_clock_info_t;
  210. /* Get the current time from the system clock.
  211. If the internal clock fails, silently ignore the error and return 0.
  212. On integer overflow, silently ignore the overflow and clamp the clock to
  213. [_PyTime_MIN; _PyTime_MAX].
  214. Use _PyTime_GetSystemClockWithInfo() to check for failure. */
  215. PyAPI_FUNC(_PyTime_t) _PyTime_GetSystemClock(void);
  216. /* Get the current time from the system clock.
  217. * On success, set *t and *info (if not NULL), and return 0.
  218. * On error, raise an exception and return -1.
  219. */
  220. PyAPI_FUNC(int) _PyTime_GetSystemClockWithInfo(
  221. _PyTime_t *t,
  222. _Py_clock_info_t *info);
  223. /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
  224. The clock is not affected by system clock updates. The reference point of
  225. the returned value is undefined, so that only the difference between the
  226. results of consecutive calls is valid.
  227. If the internal clock fails, silently ignore the error and return 0.
  228. On integer overflow, silently ignore the overflow and clamp the clock to
  229. [_PyTime_MIN; _PyTime_MAX].
  230. Use _PyTime_GetMonotonicClockWithInfo() to check for failure. */
  231. PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void);
  232. /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
  233. The clock is not affected by system clock updates. The reference point of
  234. the returned value is undefined, so that only the difference between the
  235. results of consecutive calls is valid.
  236. Fill info (if set) with information of the function used to get the time.
  237. Return 0 on success, raise an exception and return -1 on error. */
  238. PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo(
  239. _PyTime_t *t,
  240. _Py_clock_info_t *info);
  241. /* Converts a timestamp to the Gregorian time, using the local time zone.
  242. Return 0 on success, raise an exception and return -1 on error. */
  243. PyAPI_FUNC(int) _PyTime_localtime(time_t t, struct tm *tm);
  244. /* Converts a timestamp to the Gregorian time, assuming UTC.
  245. Return 0 on success, raise an exception and return -1 on error. */
  246. PyAPI_FUNC(int) _PyTime_gmtime(time_t t, struct tm *tm);
  247. /* Get the performance counter: clock with the highest available resolution to
  248. measure a short duration.
  249. If the internal clock fails, silently ignore the error and return 0.
  250. On integer overflow, silently ignore the overflow and clamp the clock to
  251. [_PyTime_MIN; _PyTime_MAX].
  252. Use _PyTime_GetPerfCounterWithInfo() to check for failure. */
  253. PyAPI_FUNC(_PyTime_t) _PyTime_GetPerfCounter(void);
  254. /* Get the performance counter: clock with the highest available resolution to
  255. measure a short duration.
  256. Fill info (if set) with information of the function used to get the time.
  257. Return 0 on success, raise an exception and return -1 on error. */
  258. PyAPI_FUNC(int) _PyTime_GetPerfCounterWithInfo(
  259. _PyTime_t *t,
  260. _Py_clock_info_t *info);
  261. // Create a deadline.
  262. // Pseudo code: _PyTime_GetMonotonicClock() + timeout.
  263. PyAPI_FUNC(_PyTime_t) _PyDeadline_Init(_PyTime_t timeout);
  264. // Get remaining time from a deadline.
  265. // Pseudo code: deadline - _PyTime_GetMonotonicClock().
  266. PyAPI_FUNC(_PyTime_t) _PyDeadline_Get(_PyTime_t deadline);
  267. #ifdef __cplusplus
  268. }
  269. #endif
  270. #endif /* Py_PYTIME_H */
  271. #endif /* Py_LIMITED_API */