blob: bf0dcd86577fda0a549f5a15d2a5a508e41d2069 [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. */
33 _PyTime_ROUND_CEILING
Victor Stinner3c1b3792014-02-17 00:02:43 +010034} _PyTime_round_t;
35
Larry Hastings6fe20b32012-04-19 15:07:49 -070036/* Convert a time_t to a PyLong. */
37PyAPI_FUNC(PyObject *) _PyLong_FromTime_t(
38 time_t sec);
39
Larry Hastings76ad59b2012-05-03 00:30:07 -070040/* Convert a PyLong to a time_t. */
41PyAPI_FUNC(time_t) _PyLong_AsTime_t(
42 PyObject *obj);
43
Victor Stinner1bd18ba2015-03-30 00:25:38 +020044/* Convert a number of seconds, int or float, to time_t. */
45PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
46 PyObject *obj,
47 time_t *sec,
48 _PyTime_round_t);
49
Victor Stinner5d272cc2012-03-13 13:35:55 +010050/* Convert a number of seconds, int or float, to a timeval structure.
51 usec is in the range [0; 999999] and rounded towards zero.
52 For example, -1.2 is converted to (-2, 800000). */
53PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
54 PyObject *obj,
55 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010056 long *usec,
57 _PyTime_round_t);
Victor Stinner5d272cc2012-03-13 13:35:55 +010058
Victor Stinner643cd682012-03-02 22:54:03 +010059/* Convert a number of seconds, int or float, to a timespec structure.
Victor Stinner5d272cc2012-03-13 13:35:55 +010060 nsec is in the range [0; 999999999] and rounded towards zero.
61 For example, -1.2 is converted to (-2, 800000000). */
Victor Stinner643cd682012-03-02 22:54:03 +010062PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
63 PyObject *obj,
64 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010065 long *nsec,
66 _PyTime_round_t);
Victor Stinner643cd682012-03-02 22:54:03 +010067
Victor Stinnercb29f012015-03-27 13:31:18 +010068
Victor Stinner13019fd2015-04-03 13:10:54 +020069/* Create a timestamp from a number of seconds. */
70PyAPI_FUNC(_PyTime_t) _PyTime_FromSeconds(int ns);
71
72/* Create a timestamp from a number of nanoseconds. */
Victor Stinner4bfb4602015-03-27 22:27:24 +010073PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(PY_LONG_LONG ns);
74
Victor Stinnerfa09beb2015-03-30 21:36:10 +020075/* Convert a number of seconds (Python float or int) to a timetamp.
Victor Stinnercb29f012015-03-27 13:31:18 +010076 Raise an exception and return -1 on error, return 0 on success. */
Victor Stinner992c43f2015-03-27 17:12:45 +010077PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t,
Victor Stinnercb29f012015-03-27 13:31:18 +010078 PyObject *obj,
79 _PyTime_round_t round);
80
Victor Stinnerfa09beb2015-03-30 21:36:10 +020081/* Convert a number of milliseconds (Python float or int, 10^-3) to a timetamp.
82 Raise an exception and return -1 on error, return 0 on success. */
83PyAPI_FUNC(int) _PyTime_FromMillisecondsObject(_PyTime_t *t,
84 PyObject *obj,
85 _PyTime_round_t round);
86
Victor Stinner4bfb4602015-03-27 22:27:24 +010087/* Convert a timestamp to a number of seconds as a C double. */
88PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t);
89
Victor Stinnercb29f012015-03-27 13:31:18 +010090/* Convert timestamp to a number of milliseconds (10^-3 seconds). */
Victor Stinner992c43f2015-03-27 17:12:45 +010091PyAPI_FUNC(_PyTime_t) _PyTime_AsMilliseconds(_PyTime_t t,
Victor Stinnercb29f012015-03-27 13:31:18 +010092 _PyTime_round_t round);
93
Victor Stinnerf5faad22015-03-28 03:52:05 +010094/* Convert timestamp to a number of microseconds (10^-6 seconds). */
95PyAPI_FUNC(_PyTime_t) _PyTime_AsMicroseconds(_PyTime_t t,
96 _PyTime_round_t round);
97
Victor Stinner992c43f2015-03-27 17:12:45 +010098/* Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int
99 object. */
100PyAPI_FUNC(PyObject *) _PyTime_AsNanosecondsObject(_PyTime_t t);
101
Victor Stinner4bfb4602015-03-27 22:27:24 +0100102/* Convert a timestamp to a timeval structure (microsecond resolution).
Victor Stinner95e9cef2015-03-28 01:26:47 +0100103 tv_usec is always positive.
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200104 Raise an exception and return -1 if the conversion overflowed,
105 return 0 on success. */
Victor Stinnercb29f012015-03-27 13:31:18 +0100106PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t,
107 struct timeval *tv,
108 _PyTime_round_t round);
109
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200110/* Similar to _PyTime_AsTimeval(), but don't raise an exception on error. */
111PyAPI_FUNC(int) _PyTime_AsTimeval_noraise(_PyTime_t t,
112 struct timeval *tv,
113 _PyTime_round_t round);
114
Victor Stinnerc3378382015-03-28 05:07:51 +0100115#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
Victor Stinner34dc0f42015-03-27 18:19:03 +0100116/* Convert a timestamp to a timespec structure (nanosecond resolution).
Victor Stinner95e9cef2015-03-28 01:26:47 +0100117 tv_nsec is always positive.
Victor Stinner34dc0f42015-03-27 18:19:03 +0100118 Raise an exception and return -1 on error, return 0 on success. */
119PyAPI_FUNC(int) _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts);
120#endif
121
Victor Stinnera47b8812015-03-27 18:16:17 +0100122/* Get the current time from the system clock.
Victor Stinner09e5cf22015-03-30 00:09:18 +0200123
124 The function cannot fail. _PyTime_Init() ensures that the system clock
125 works. */
126PyAPI_FUNC(_PyTime_t) _PyTime_GetSystemClock(void);
127
Victor Stinner1bd18ba2015-03-30 00:25:38 +0200128/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
129 The clock is not affected by system clock updates. The reference point of
130 the returned value is undefined, so that only the difference between the
131 results of consecutive calls is valid.
132
133 The function cannot fail. _PyTime_Init() ensures that a monotonic clock
134 is available and works. */
135PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void);
136
137
138/* Structure used by time.get_clock_info() */
139typedef struct {
140 const char *implementation;
141 int monotonic;
142 int adjustable;
143 double resolution;
144} _Py_clock_info_t;
145
Victor Stinner09e5cf22015-03-30 00:09:18 +0200146/* Get the current time from the system clock.
Victor Stinnera47b8812015-03-27 18:16:17 +0100147 * Fill clock information if info is not NULL.
148 * Raise an exception and return -1 on error, return 0 on success.
149 */
150PyAPI_FUNC(int) _PyTime_GetSystemClockWithInfo(
151 _PyTime_t *t,
152 _Py_clock_info_t *info);
153
Victor Stinnercb29f012015-03-27 13:31:18 +0100154/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
155 The clock is not affected by system clock updates. The reference point of
156 the returned value is undefined, so that only the difference between the
157 results of consecutive calls is valid.
158
Victor Stinner4bfb4602015-03-27 22:27:24 +0100159 Fill info (if set) with information of the function used to get the time.
160
161 Return 0 on success, raise an exception and return -1 on error. */
162PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo(
163 _PyTime_t *t,
164 _Py_clock_info_t *info);
165
Victor Stinnercb29f012015-03-27 13:31:18 +0100166
Victor Stinner1bd18ba2015-03-30 00:25:38 +0200167/* Initialize time.
168 Return 0 on success, raise an exception and return -1 on error. */
169PyAPI_FUNC(int) _PyTime_Init(void);
170
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000171#ifdef __cplusplus
172}
173#endif
174
175#endif /* Py_PYTIME_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000176#endif /* Py_LIMITED_API */