blob: 1648d039116d1be8efc822b400a68f17584e579e [file] [log] [blame]
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001#ifndef Py_LIMITED_API
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +00002#ifndef Py_PYTIME_H
3#define Py_PYTIME_H
4
Victor Stinner4195b5c2012-02-08 23:03:19 +01005#include "pyconfig.h" /* include for defines */
Victor Stinner643cd682012-03-02 22:54:03 +01006#include "object.h"
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +00007
8/**************************************************************************
9Symbols and macros to supply platform-independent interfaces to time related
10functions and constants
11**************************************************************************/
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16#ifdef HAVE_GETTIMEOFDAY
17typedef struct timeval _PyTime_timeval;
18#else
19typedef struct {
20 time_t tv_sec; /* seconds since Jan. 1, 1970 */
21 long tv_usec; /* and microseconds */
22} _PyTime_timeval;
23#endif
24
Victor Stinnerec895392012-04-29 02:41:27 +020025/* Structure used by time.get_clock_info() */
26typedef struct {
27 const char *implementation;
Benjamin Peterson49a69e42012-05-01 09:38:34 -040028 int monotonic;
Victor Stinner2b89fdf2012-06-12 22:46:37 +020029 int adjustable;
Victor Stinnerec895392012-04-29 02:41:27 +020030 double resolution;
31} _Py_clock_info_t;
32
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000033/* Similar to POSIX gettimeofday but cannot fail. If system gettimeofday
34 * fails or is not available, fall back to lower resolution clocks.
35 */
36PyAPI_FUNC(void) _PyTime_gettimeofday(_PyTime_timeval *tp);
37
Victor Stinnerec895392012-04-29 02:41:27 +020038/* Similar to _PyTime_gettimeofday() but retrieve also information on the
39 * clock used to get the current time. */
Victor Stinner00111242014-08-29 16:31:59 +020040PyAPI_FUNC(int) _PyTime_gettimeofday_info(
Victor Stinnerec895392012-04-29 02:41:27 +020041 _PyTime_timeval *tp,
42 _Py_clock_info_t *info);
43
Antoine Pitrou3e1fd272010-09-28 21:23:11 +000044#define _PyTime_INTERVAL(tv_start, tv_end) \
45 ((tv_end.tv_sec - tv_start.tv_sec) + \
46 (tv_end.tv_usec - tv_start.tv_usec) * 0.000001)
47
Victor Stinner3c1b3792014-02-17 00:02:43 +010048typedef enum {
49 /* Round towards zero. */
50 _PyTime_ROUND_DOWN=0,
51 /* Round away from zero. */
52 _PyTime_ROUND_UP
53} _PyTime_round_t;
54
Victor Stinner5d272cc2012-03-13 13:35:55 +010055/* Convert a number of seconds, int or float, to time_t. */
56PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
57 PyObject *obj,
Victor Stinner3c1b3792014-02-17 00:02:43 +010058 time_t *sec,
59 _PyTime_round_t);
Victor Stinner5d272cc2012-03-13 13:35:55 +010060
Larry Hastings6fe20b32012-04-19 15:07:49 -070061/* Convert a time_t to a PyLong. */
62PyAPI_FUNC(PyObject *) _PyLong_FromTime_t(
63 time_t sec);
64
Larry Hastings76ad59b2012-05-03 00:30:07 -070065/* Convert a PyLong to a time_t. */
66PyAPI_FUNC(time_t) _PyLong_AsTime_t(
67 PyObject *obj);
68
Victor Stinner5d272cc2012-03-13 13:35:55 +010069/* Convert a number of seconds, int or float, to a timeval structure.
70 usec is in the range [0; 999999] and rounded towards zero.
71 For example, -1.2 is converted to (-2, 800000). */
72PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
73 PyObject *obj,
74 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010075 long *usec,
76 _PyTime_round_t);
Victor Stinner5d272cc2012-03-13 13:35:55 +010077
Victor Stinner643cd682012-03-02 22:54:03 +010078/* Convert a number of seconds, int or float, to a timespec structure.
Victor Stinner5d272cc2012-03-13 13:35:55 +010079 nsec is in the range [0; 999999999] and rounded towards zero.
80 For example, -1.2 is converted to (-2, 800000000). */
Victor Stinner643cd682012-03-02 22:54:03 +010081PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
82 PyObject *obj,
83 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010084 long *nsec,
85 _PyTime_round_t);
Victor Stinner643cd682012-03-02 22:54:03 +010086
Victor Stinnerae586492014-09-02 23:18:25 +020087/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
88 The clock is not affected by system clock updates. The reference point of
89 the returned value is undefined, so that only the difference between the
90 results of consecutive calls is valid.
91
92 The function never fails. _PyTime_Init() ensures that a monotonic clock
93 is available and works. */
94PyAPI_FUNC(void) _PyTime_monotonic(
95 _PyTime_timeval *tp);
96
97/* Similar to _PyTime_monotonic(), fill also info (if set) with information of
98 the function used to get the time.
99
100 Return 0 on success, raise an exception and return -1 on error. */
101PyAPI_FUNC(int) _PyTime_monotonic_info(
102 _PyTime_timeval *tp,
103 _Py_clock_info_t *info);
104
Victor Stinner9a8089b2015-03-20 01:42:20 +0100105/* Add interval seconds to tv */
106PyAPI_FUNC(void)
107_PyTime_AddDouble(_PyTime_timeval *tv, double interval,
108 _PyTime_round_t round);
109
Victor Stinner00111242014-08-29 16:31:59 +0200110/* Initialize time.
111 Return 0 on success, raise an exception and return -1 on error. */
112PyAPI_FUNC(int) _PyTime_Init(void);
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000113
Victor Stinnercb29f012015-03-27 13:31:18 +0100114/****************** NEW _PyTime_t API **********************/
115
116#ifdef PY_INT64_T
117typedef PY_INT64_T _PyTime_t;
118#else
119# error "_PyTime_t need signed 64-bit integer type"
120#endif
121
Victor Stinner4bfb4602015-03-27 22:27:24 +0100122/* Create a timestamp from a number of nanoseconds (C long). */
123PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(PY_LONG_LONG ns);
124
Victor Stinnercb29f012015-03-27 13:31:18 +0100125/* Convert a Python float or int to a timetamp.
126 Raise an exception and return -1 on error, return 0 on success. */
Victor Stinner992c43f2015-03-27 17:12:45 +0100127PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t,
Victor Stinnercb29f012015-03-27 13:31:18 +0100128 PyObject *obj,
129 _PyTime_round_t round);
130
Victor Stinner4bfb4602015-03-27 22:27:24 +0100131/* Convert a timestamp to a number of seconds as a C double. */
132PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t);
133
Victor Stinnercb29f012015-03-27 13:31:18 +0100134/* Convert timestamp to a number of milliseconds (10^-3 seconds). */
Victor Stinner992c43f2015-03-27 17:12:45 +0100135PyAPI_FUNC(_PyTime_t) _PyTime_AsMilliseconds(_PyTime_t t,
Victor Stinnercb29f012015-03-27 13:31:18 +0100136 _PyTime_round_t round);
137
Victor Stinner992c43f2015-03-27 17:12:45 +0100138/* Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int
139 object. */
140PyAPI_FUNC(PyObject *) _PyTime_AsNanosecondsObject(_PyTime_t t);
141
Victor Stinner4bfb4602015-03-27 22:27:24 +0100142/* Convert a timestamp to a timeval structure (microsecond resolution).
143 Raise an exception and return -1 on error, return 0 on success. */
Victor Stinnercb29f012015-03-27 13:31:18 +0100144PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t,
145 struct timeval *tv,
146 _PyTime_round_t round);
147
Victor Stinnera47b8812015-03-27 18:16:17 +0100148/* Get the current time from the system clock.
149 * Fill clock information if info is not NULL.
150 * Raise an exception and return -1 on error, return 0 on success.
151 */
152PyAPI_FUNC(int) _PyTime_GetSystemClockWithInfo(
153 _PyTime_t *t,
154 _Py_clock_info_t *info);
155
Victor Stinnercb29f012015-03-27 13:31:18 +0100156/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
157 The clock is not affected by system clock updates. The reference point of
158 the returned value is undefined, so that only the difference between the
159 results of consecutive calls is valid.
160
161 The function cannot fail. _PyTime_Init() ensures that a monotonic clock
162 is available and works. */
163PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void);
164
Victor Stinner4bfb4602015-03-27 22:27:24 +0100165/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
166 The clock is not affected by system clock updates. The reference point of
167 the returned value is undefined, so that only the difference between the
168 results of consecutive calls is valid.
169
170 Fill info (if set) with information of the function used to get the time.
171
172 Return 0 on success, raise an exception and return -1 on error. */
173PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo(
174 _PyTime_t *t,
175 _Py_clock_info_t *info);
176
Victor Stinnercb29f012015-03-27 13:31:18 +0100177
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000178#ifdef __cplusplus
179}
180#endif
181
182#endif /* Py_PYTIME_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000183#endif /* Py_LIMITED_API */