blob: 54a0cc4db8ec483a91024103ac9d6b1dbd071768 [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#ifdef PY_INT64_T
17/* _PyTime_t: Python timestamp with subsecond precision. It can be used to
18 store a duration, and so indirectly a date (related to another date, like
19 UNIX epoch). */
20typedef PY_INT64_T _PyTime_t;
21#define _PyTime_MIN PY_LLONG_MIN
22#define _PyTime_MAX PY_LLONG_MAX
23#else
24# error "_PyTime_t need signed 64-bit integer type"
25#endif
Victor Stinnerec895392012-04-29 02:41:27 +020026
Victor Stinner3c1b3792014-02-17 00:02:43 +010027typedef enum {
Victor Stinner02937aa2015-03-28 05:02:39 +010028 /* Round towards minus infinity (-inf).
29 For example, used to read a clock. */
Victor Stinnera695f832015-03-30 03:57:14 +020030 _PyTime_ROUND_FLOOR=0,
Victor Stinnerbcdd7772015-03-30 03:52:49 +020031 /* Round towards infinity (+inf).
32 For example, used for timeout to wait "at least" N seconds. */
Victor Stinner74474232015-09-02 01:43:56 +020033 _PyTime_ROUND_CEILING=1,
Victor Stinner7667f582015-09-09 01:02:23 +020034 /* Round to nearest with ties going to nearest even integer.
Victor Stinner74474232015-09-02 01:43:56 +020035 For example, used to round from a Python float. */
Victor Stinner7667f582015-09-09 01:02:23 +020036 _PyTime_ROUND_HALF_EVEN
Victor Stinner3c1b3792014-02-17 00:02:43 +010037} _PyTime_round_t;
38
Larry Hastings6fe20b32012-04-19 15:07:49 -070039/* Convert a time_t to a PyLong. */
40PyAPI_FUNC(PyObject *) _PyLong_FromTime_t(
41 time_t sec);
42
Larry Hastings76ad59b2012-05-03 00:30:07 -070043/* Convert a PyLong to a time_t. */
44PyAPI_FUNC(time_t) _PyLong_AsTime_t(
45 PyObject *obj);
46
Victor Stinner7667f582015-09-09 01:02:23 +020047/* Round to nearest with ties going to nearest even integer
48 (_PyTime_ROUND_HALF_EVEN) */
49PyAPI_FUNC(double) _PyTime_RoundHalfEven(
Victor Stinner2ec55872015-09-02 19:16:07 +020050 double x);
51
Victor Stinner1bd18ba2015-03-30 00:25:38 +020052/* Convert a number of seconds, int or float, to time_t. */
53PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
54 PyObject *obj,
55 time_t *sec,
56 _PyTime_round_t);
57
Victor Stinner5d272cc2012-03-13 13:35:55 +010058/* Convert a number of seconds, int or float, to a timeval structure.
59 usec is in the range [0; 999999] and rounded towards zero.
60 For example, -1.2 is converted to (-2, 800000). */
61PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
62 PyObject *obj,
63 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010064 long *usec,
65 _PyTime_round_t);
Victor Stinner5d272cc2012-03-13 13:35:55 +010066
Victor Stinner643cd682012-03-02 22:54:03 +010067/* Convert a number of seconds, int or float, to a timespec structure.
Victor Stinner5d272cc2012-03-13 13:35:55 +010068 nsec is in the range [0; 999999999] and rounded towards zero.
69 For example, -1.2 is converted to (-2, 800000000). */
Victor Stinner643cd682012-03-02 22:54:03 +010070PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
71 PyObject *obj,
72 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010073 long *nsec,
74 _PyTime_round_t);
Victor Stinner643cd682012-03-02 22:54:03 +010075
Victor Stinnercb29f012015-03-27 13:31:18 +010076
Victor Stinner13019fd2015-04-03 13:10:54 +020077/* Create a timestamp from a number of seconds. */
Victor Stinner88ed6402015-04-09 10:23:12 +020078PyAPI_FUNC(_PyTime_t) _PyTime_FromSeconds(int seconds);
79
80/* Macro to create a timestamp from a number of seconds, no integer overflow.
81 Only use the macro for small values, prefer _PyTime_FromSeconds(). */
82#define _PYTIME_FROMSECONDS(seconds) \
83 ((_PyTime_t)(seconds) * (1000 * 1000 * 1000))
Victor Stinner13019fd2015-04-03 13:10:54 +020084
85/* Create a timestamp from a number of nanoseconds. */
Victor Stinner4bfb4602015-03-27 22:27:24 +010086PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(PY_LONG_LONG ns);
87
Victor Stinnerfa09beb2015-03-30 21:36:10 +020088/* Convert a number of seconds (Python float or int) to a timetamp.
Victor Stinnercb29f012015-03-27 13:31:18 +010089 Raise an exception and return -1 on error, return 0 on success. */
Victor Stinner992c43f2015-03-27 17:12:45 +010090PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t,
Victor Stinnercb29f012015-03-27 13:31:18 +010091 PyObject *obj,
92 _PyTime_round_t round);
93
Victor Stinnerfa09beb2015-03-30 21:36:10 +020094/* Convert a number of milliseconds (Python float or int, 10^-3) to a timetamp.
95 Raise an exception and return -1 on error, return 0 on success. */
96PyAPI_FUNC(int) _PyTime_FromMillisecondsObject(_PyTime_t *t,
97 PyObject *obj,
98 _PyTime_round_t round);
99
Victor Stinner4bfb4602015-03-27 22:27:24 +0100100/* Convert a timestamp to a number of seconds as a C double. */
101PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t);
102
Victor Stinnercb29f012015-03-27 13:31:18 +0100103/* Convert timestamp to a number of milliseconds (10^-3 seconds). */
Victor Stinner992c43f2015-03-27 17:12:45 +0100104PyAPI_FUNC(_PyTime_t) _PyTime_AsMilliseconds(_PyTime_t t,
Victor Stinnercb29f012015-03-27 13:31:18 +0100105 _PyTime_round_t round);
106
Victor Stinnerf5faad22015-03-28 03:52:05 +0100107/* Convert timestamp to a number of microseconds (10^-6 seconds). */
108PyAPI_FUNC(_PyTime_t) _PyTime_AsMicroseconds(_PyTime_t t,
109 _PyTime_round_t round);
110
Victor Stinner992c43f2015-03-27 17:12:45 +0100111/* Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int
112 object. */
113PyAPI_FUNC(PyObject *) _PyTime_AsNanosecondsObject(_PyTime_t t);
114
Victor Stinner4bfb4602015-03-27 22:27:24 +0100115/* Convert a timestamp to a timeval structure (microsecond resolution).
Victor Stinner95e9cef2015-03-28 01:26:47 +0100116 tv_usec is always positive.
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200117 Raise an exception and return -1 if the conversion overflowed,
118 return 0 on success. */
Victor Stinnercb29f012015-03-27 13:31:18 +0100119PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t,
120 struct timeval *tv,
121 _PyTime_round_t round);
122
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200123/* Similar to _PyTime_AsTimeval(), but don't raise an exception on error. */
124PyAPI_FUNC(int) _PyTime_AsTimeval_noraise(_PyTime_t t,
125 struct timeval *tv,
126 _PyTime_round_t round);
127
Victor Stinnerc3378382015-03-28 05:07:51 +0100128#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
Victor Stinner34dc0f42015-03-27 18:19:03 +0100129/* Convert a timestamp to a timespec structure (nanosecond resolution).
Victor Stinner95e9cef2015-03-28 01:26:47 +0100130 tv_nsec is always positive.
Victor Stinner34dc0f42015-03-27 18:19:03 +0100131 Raise an exception and return -1 on error, return 0 on success. */
132PyAPI_FUNC(int) _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts);
133#endif
134
Victor Stinnera47b8812015-03-27 18:16:17 +0100135/* Get the current time from the system clock.
Victor Stinner09e5cf22015-03-30 00:09:18 +0200136
137 The function cannot fail. _PyTime_Init() ensures that the system clock
138 works. */
139PyAPI_FUNC(_PyTime_t) _PyTime_GetSystemClock(void);
140
Victor Stinner1bd18ba2015-03-30 00:25:38 +0200141/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
142 The clock is not affected by system clock updates. The reference point of
143 the returned value is undefined, so that only the difference between the
144 results of consecutive calls is valid.
145
146 The function cannot fail. _PyTime_Init() ensures that a monotonic clock
147 is available and works. */
148PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void);
149
150
151/* Structure used by time.get_clock_info() */
152typedef struct {
153 const char *implementation;
154 int monotonic;
155 int adjustable;
156 double resolution;
157} _Py_clock_info_t;
158
Victor Stinner09e5cf22015-03-30 00:09:18 +0200159/* Get the current time from the system clock.
Victor Stinnera47b8812015-03-27 18:16:17 +0100160 * Fill clock information if info is not NULL.
161 * Raise an exception and return -1 on error, return 0 on success.
162 */
163PyAPI_FUNC(int) _PyTime_GetSystemClockWithInfo(
164 _PyTime_t *t,
165 _Py_clock_info_t *info);
166
Victor Stinnercb29f012015-03-27 13:31:18 +0100167/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
168 The clock is not affected by system clock updates. The reference point of
169 the returned value is undefined, so that only the difference between the
170 results of consecutive calls is valid.
171
Victor Stinner4bfb4602015-03-27 22:27:24 +0100172 Fill info (if set) with information of the function used to get the time.
173
174 Return 0 on success, raise an exception and return -1 on error. */
175PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo(
176 _PyTime_t *t,
177 _Py_clock_info_t *info);
178
Victor Stinnercb29f012015-03-27 13:31:18 +0100179
Victor Stinner1bd18ba2015-03-30 00:25:38 +0200180/* Initialize time.
181 Return 0 on success, raise an exception and return -1 on error. */
182PyAPI_FUNC(int) _PyTime_Init(void);
183
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000184#ifdef __cplusplus
185}
186#endif
187
188#endif /* Py_PYTIME_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000189#endif /* Py_LIMITED_API */