blob: 9f489180ffff74fe4f1beca89cba03373fb7e329 [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
Victor Stinner1bd18ba2015-03-30 00:25:38 +020016/* _PyTime_t: Python timestamp with subsecond precision. It can be used to
17 store a duration, and so indirectly a date (related to another date, like
18 UNIX epoch). */
Benjamin Peterson9b3d7702016-09-06 13:24:00 -070019typedef int64_t _PyTime_t;
Victor Stinner1bd18ba2015-03-30 00:25:38 +020020#define _PyTime_MIN PY_LLONG_MIN
21#define _PyTime_MAX PY_LLONG_MAX
Victor Stinnerec895392012-04-29 02:41:27 +020022
Victor Stinner3c1b3792014-02-17 00:02:43 +010023typedef enum {
Victor Stinner02937aa2015-03-28 05:02:39 +010024 /* Round towards minus infinity (-inf).
25 For example, used to read a clock. */
Victor Stinnera695f832015-03-30 03:57:14 +020026 _PyTime_ROUND_FLOOR=0,
Victor Stinnerbcdd7772015-03-30 03:52:49 +020027 /* Round towards infinity (+inf).
28 For example, used for timeout to wait "at least" N seconds. */
Victor Stinner74474232015-09-02 01:43:56 +020029 _PyTime_ROUND_CEILING=1,
Victor Stinner7667f582015-09-09 01:02:23 +020030 /* Round to nearest with ties going to nearest even integer.
Victor Stinner74474232015-09-02 01:43:56 +020031 For example, used to round from a Python float. */
Pablo Galindo2c15b292017-10-17 15:14:41 +010032 _PyTime_ROUND_HALF_EVEN=2,
Serhiy Storchakabdf42982017-10-26 16:59:40 +030033 /* Round away from zero
Pablo Galindo2c15b292017-10-17 15:14:41 +010034 For example, used for timeout. _PyTime_ROUND_CEILING rounds
35 -1e-9 to 0 milliseconds which causes bpo-31786 issue.
36 _PyTime_ROUND_UP rounds -1e-9 to -1 millisecond which keeps
37 the timeout sign as expected. select.poll(timeout) must block
38 for negative values." */
39 _PyTime_ROUND_UP=3,
Serhiy Storchakabdf42982017-10-26 16:59:40 +030040 /* _PyTime_ROUND_TIMEOUT (an alias for _PyTime_ROUND_UP) should be
Pablo Galindo2c15b292017-10-17 15:14:41 +010041 used for timeouts. */
42 _PyTime_ROUND_TIMEOUT = _PyTime_ROUND_UP
Victor Stinner3c1b3792014-02-17 00:02:43 +010043} _PyTime_round_t;
44
Pablo Galindo2c15b292017-10-17 15:14:41 +010045
Larry Hastings6fe20b32012-04-19 15:07:49 -070046/* Convert a time_t to a PyLong. */
47PyAPI_FUNC(PyObject *) _PyLong_FromTime_t(
48 time_t sec);
49
Larry Hastings76ad59b2012-05-03 00:30:07 -070050/* Convert a PyLong to a time_t. */
51PyAPI_FUNC(time_t) _PyLong_AsTime_t(
52 PyObject *obj);
53
Victor Stinner1bd18ba2015-03-30 00:25:38 +020054/* Convert a number of seconds, int or float, to time_t. */
55PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
56 PyObject *obj,
57 time_t *sec,
58 _PyTime_round_t);
59
Victor Stinner5d272cc2012-03-13 13:35:55 +010060/* Convert a number of seconds, int or float, to a timeval structure.
61 usec is in the range [0; 999999] and rounded towards zero.
62 For example, -1.2 is converted to (-2, 800000). */
63PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
64 PyObject *obj,
65 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010066 long *usec,
67 _PyTime_round_t);
Victor Stinner5d272cc2012-03-13 13:35:55 +010068
Victor Stinner643cd682012-03-02 22:54:03 +010069/* Convert a number of seconds, int or float, to a timespec structure.
Victor Stinner5d272cc2012-03-13 13:35:55 +010070 nsec is in the range [0; 999999999] and rounded towards zero.
71 For example, -1.2 is converted to (-2, 800000000). */
Victor Stinner643cd682012-03-02 22:54:03 +010072PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
73 PyObject *obj,
74 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010075 long *nsec,
76 _PyTime_round_t);
Victor Stinner643cd682012-03-02 22:54:03 +010077
Victor Stinnercb29f012015-03-27 13:31:18 +010078
Victor Stinner13019fd2015-04-03 13:10:54 +020079/* Create a timestamp from a number of seconds. */
Victor Stinner88ed6402015-04-09 10:23:12 +020080PyAPI_FUNC(_PyTime_t) _PyTime_FromSeconds(int seconds);
81
82/* Macro to create a timestamp from a number of seconds, no integer overflow.
83 Only use the macro for small values, prefer _PyTime_FromSeconds(). */
84#define _PYTIME_FROMSECONDS(seconds) \
85 ((_PyTime_t)(seconds) * (1000 * 1000 * 1000))
Victor Stinner13019fd2015-04-03 13:10:54 +020086
87/* Create a timestamp from a number of nanoseconds. */
Benjamin Petersonaf580df2016-09-06 10:46:49 -070088PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(long long ns);
Victor Stinner4bfb4602015-03-27 22:27:24 +010089
Victor Stinnerfa09beb2015-03-30 21:36:10 +020090/* Convert a number of seconds (Python float or int) to a timetamp.
Victor Stinnercb29f012015-03-27 13:31:18 +010091 Raise an exception and return -1 on error, return 0 on success. */
Victor Stinner992c43f2015-03-27 17:12:45 +010092PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t,
Victor Stinnercb29f012015-03-27 13:31:18 +010093 PyObject *obj,
94 _PyTime_round_t round);
95
Victor Stinnerfa09beb2015-03-30 21:36:10 +020096/* Convert a number of milliseconds (Python float or int, 10^-3) to a timetamp.
97 Raise an exception and return -1 on error, return 0 on success. */
98PyAPI_FUNC(int) _PyTime_FromMillisecondsObject(_PyTime_t *t,
99 PyObject *obj,
100 _PyTime_round_t round);
101
Victor Stinner4bfb4602015-03-27 22:27:24 +0100102/* Convert a timestamp to a number of seconds as a C double. */
103PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t);
104
Victor Stinnercb29f012015-03-27 13:31:18 +0100105/* Convert timestamp to a number of milliseconds (10^-3 seconds). */
Victor Stinner992c43f2015-03-27 17:12:45 +0100106PyAPI_FUNC(_PyTime_t) _PyTime_AsMilliseconds(_PyTime_t t,
Victor Stinnercb29f012015-03-27 13:31:18 +0100107 _PyTime_round_t round);
108
Victor Stinnerf5faad22015-03-28 03:52:05 +0100109/* Convert timestamp to a number of microseconds (10^-6 seconds). */
110PyAPI_FUNC(_PyTime_t) _PyTime_AsMicroseconds(_PyTime_t t,
111 _PyTime_round_t round);
112
Victor Stinner992c43f2015-03-27 17:12:45 +0100113/* Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int
114 object. */
115PyAPI_FUNC(PyObject *) _PyTime_AsNanosecondsObject(_PyTime_t t);
116
Victor Stinner4bfb4602015-03-27 22:27:24 +0100117/* Convert a timestamp to a timeval structure (microsecond resolution).
Victor Stinner95e9cef2015-03-28 01:26:47 +0100118 tv_usec is always positive.
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200119 Raise an exception and return -1 if the conversion overflowed,
120 return 0 on success. */
Victor Stinnercb29f012015-03-27 13:31:18 +0100121PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t,
122 struct timeval *tv,
123 _PyTime_round_t round);
124
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200125/* Similar to _PyTime_AsTimeval(), but don't raise an exception on error. */
126PyAPI_FUNC(int) _PyTime_AsTimeval_noraise(_PyTime_t t,
127 struct timeval *tv,
128 _PyTime_round_t round);
129
Victor Stinner1e2b6882015-09-18 13:23:02 +0200130/* Convert a timestamp to a number of seconds (secs) and microseconds (us).
131 us is always positive. This function is similar to _PyTime_AsTimeval()
132 except that secs is always a time_t type, whereas the timeval structure
133 uses a C long for tv_sec on Windows.
134 Raise an exception and return -1 if the conversion overflowed,
135 return 0 on success. */
136PyAPI_FUNC(int) _PyTime_AsTimevalTime_t(
137 _PyTime_t t,
138 time_t *secs,
139 int *us,
140 _PyTime_round_t round);
141
Victor Stinnerc3378382015-03-28 05:07:51 +0100142#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
Victor Stinner34dc0f42015-03-27 18:19:03 +0100143/* Convert a timestamp to a timespec structure (nanosecond resolution).
Victor Stinner95e9cef2015-03-28 01:26:47 +0100144 tv_nsec is always positive.
Victor Stinner34dc0f42015-03-27 18:19:03 +0100145 Raise an exception and return -1 on error, return 0 on success. */
146PyAPI_FUNC(int) _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts);
147#endif
148
Victor Stinnera47b8812015-03-27 18:16:17 +0100149/* Get the current time from the system clock.
Victor Stinner09e5cf22015-03-30 00:09:18 +0200150
151 The function cannot fail. _PyTime_Init() ensures that the system clock
152 works. */
153PyAPI_FUNC(_PyTime_t) _PyTime_GetSystemClock(void);
154
Victor Stinner1bd18ba2015-03-30 00:25:38 +0200155/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
156 The clock is not affected by system clock updates. The reference point of
157 the returned value is undefined, so that only the difference between the
158 results of consecutive calls is valid.
159
160 The function cannot fail. _PyTime_Init() ensures that a monotonic clock
161 is available and works. */
162PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void);
163
164
165/* Structure used by time.get_clock_info() */
166typedef struct {
167 const char *implementation;
168 int monotonic;
169 int adjustable;
170 double resolution;
171} _Py_clock_info_t;
172
Victor Stinner09e5cf22015-03-30 00:09:18 +0200173/* Get the current time from the system clock.
Victor Stinnera47b8812015-03-27 18:16:17 +0100174 * Fill clock information if info is not NULL.
175 * Raise an exception and return -1 on error, return 0 on success.
176 */
177PyAPI_FUNC(int) _PyTime_GetSystemClockWithInfo(
178 _PyTime_t *t,
179 _Py_clock_info_t *info);
180
Victor Stinnercb29f012015-03-27 13:31:18 +0100181/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
182 The clock is not affected by system clock updates. The reference point of
183 the returned value is undefined, so that only the difference between the
184 results of consecutive calls is valid.
185
Victor Stinner4bfb4602015-03-27 22:27:24 +0100186 Fill info (if set) with information of the function used to get the time.
187
188 Return 0 on success, raise an exception and return -1 on error. */
189PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo(
190 _PyTime_t *t,
191 _Py_clock_info_t *info);
192
Victor Stinnercb29f012015-03-27 13:31:18 +0100193
Victor Stinner1bd18ba2015-03-30 00:25:38 +0200194/* Initialize time.
195 Return 0 on success, raise an exception and return -1 on error. */
196PyAPI_FUNC(int) _PyTime_Init(void);
197
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400198/* Converts a timestamp to the Gregorian time, using the local time zone.
199 Return 0 on success, raise an exception and return -1 on error. */
200PyAPI_FUNC(int) _PyTime_localtime(time_t t, struct tm *tm);
201
202/* Converts a timestamp to the Gregorian time, assuming UTC.
203 Return 0 on success, raise an exception and return -1 on error. */
204PyAPI_FUNC(int) _PyTime_gmtime(time_t t, struct tm *tm);
205
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700206/* Get the performance counter: clock with the highest available resolution to
207 measure a short duration.
208
209 The function cannot fail. _PyTime_Init() ensures that the system clock
210 works. */
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700211PyAPI_FUNC(_PyTime_t) _PyTime_GetPerfCounter(void);
Victor Stinnera997c7b2017-10-10 02:51:50 -0700212
213/* Get the performance counter: clock with the highest available resolution to
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700214 measure a short duration.
Victor Stinnera997c7b2017-10-10 02:51:50 -0700215
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700216 Fill info (if set) with information of the function used to get the time.
217
218 Return 0 on success, raise an exception and return -1 on error. */
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700219PyAPI_FUNC(int) _PyTime_GetPerfCounterWithInfo(
220 _PyTime_t *t,
Victor Stinnera997c7b2017-10-10 02:51:50 -0700221 _Py_clock_info_t *info);
222
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000223#ifdef __cplusplus
224}
225#endif
226
227#endif /* Py_PYTIME_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000228#endif /* Py_LIMITED_API */