blob: 919ba30e5e5ba7153277e4e32157977c27585898 [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 Stinnerec895392012-04-29 02:41:27 +020016/* Structure used by time.get_clock_info() */
17typedef struct {
18 const char *implementation;
Benjamin Peterson49a69e42012-05-01 09:38:34 -040019 int monotonic;
Victor Stinner2b89fdf2012-06-12 22:46:37 +020020 int adjustable;
Victor Stinnerec895392012-04-29 02:41:27 +020021 double resolution;
22} _Py_clock_info_t;
23
Victor Stinner3c1b3792014-02-17 00:02:43 +010024typedef enum {
25 /* Round towards zero. */
26 _PyTime_ROUND_DOWN=0,
Victor Stinner02937aa2015-03-28 05:02:39 +010027 /* Round away from zero.
28 For example, used for timeout to wait "at least" N seconds. */
29 _PyTime_ROUND_UP,
30 /* Round towards minus infinity (-inf).
31 For example, used to read a clock. */
32 _PyTime_ROUND_FLOOR
Victor Stinner3c1b3792014-02-17 00:02:43 +010033} _PyTime_round_t;
34
Victor Stinner5d272cc2012-03-13 13:35:55 +010035/* Convert a number of seconds, int or float, to time_t. */
36PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
37 PyObject *obj,
Victor Stinner3c1b3792014-02-17 00:02:43 +010038 time_t *sec,
39 _PyTime_round_t);
Victor Stinner5d272cc2012-03-13 13:35:55 +010040
Larry Hastings6fe20b32012-04-19 15:07:49 -070041/* Convert a time_t to a PyLong. */
42PyAPI_FUNC(PyObject *) _PyLong_FromTime_t(
43 time_t sec);
44
Larry Hastings76ad59b2012-05-03 00:30:07 -070045/* Convert a PyLong to a time_t. */
46PyAPI_FUNC(time_t) _PyLong_AsTime_t(
47 PyObject *obj);
48
Victor Stinner5d272cc2012-03-13 13:35:55 +010049/* Convert a number of seconds, int or float, to a timeval structure.
50 usec is in the range [0; 999999] and rounded towards zero.
51 For example, -1.2 is converted to (-2, 800000). */
52PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
53 PyObject *obj,
54 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010055 long *usec,
56 _PyTime_round_t);
Victor Stinner5d272cc2012-03-13 13:35:55 +010057
Victor Stinner643cd682012-03-02 22:54:03 +010058/* Convert a number of seconds, int or float, to a timespec structure.
Victor Stinner5d272cc2012-03-13 13:35:55 +010059 nsec is in the range [0; 999999999] and rounded towards zero.
60 For example, -1.2 is converted to (-2, 800000000). */
Victor Stinner643cd682012-03-02 22:54:03 +010061PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
62 PyObject *obj,
63 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010064 long *nsec,
65 _PyTime_round_t);
Victor Stinner643cd682012-03-02 22:54:03 +010066
Victor Stinner00111242014-08-29 16:31:59 +020067/* Initialize time.
68 Return 0 on success, raise an exception and return -1 on error. */
69PyAPI_FUNC(int) _PyTime_Init(void);
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000070
Victor Stinnercb29f012015-03-27 13:31:18 +010071/****************** NEW _PyTime_t API **********************/
72
73#ifdef PY_INT64_T
Victor Stinner02937aa2015-03-28 05:02:39 +010074/* _PyTime_t: Python timestamp with subsecond precision. It can be used to
75 store a duration, and so indirectly a date (related to another date, like
76 UNIX epoch). */
Victor Stinnercb29f012015-03-27 13:31:18 +010077typedef PY_INT64_T _PyTime_t;
Victor Stinnerf5faad22015-03-28 03:52:05 +010078#define _PyTime_MIN PY_LLONG_MIN
79#define _PyTime_MAX PY_LLONG_MAX
Victor Stinnercb29f012015-03-27 13:31:18 +010080#else
81# error "_PyTime_t need signed 64-bit integer type"
82#endif
83
Victor Stinner4bfb4602015-03-27 22:27:24 +010084/* Create a timestamp from a number of nanoseconds (C long). */
85PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(PY_LONG_LONG ns);
86
Victor Stinnercb29f012015-03-27 13:31:18 +010087/* Convert a Python float or int to a timetamp.
88 Raise an exception and return -1 on error, return 0 on success. */
Victor Stinner992c43f2015-03-27 17:12:45 +010089PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t,
Victor Stinnercb29f012015-03-27 13:31:18 +010090 PyObject *obj,
91 _PyTime_round_t round);
92
Victor Stinner4bfb4602015-03-27 22:27:24 +010093/* Convert a timestamp to a number of seconds as a C double. */
94PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t);
95
Victor Stinnercb29f012015-03-27 13:31:18 +010096/* Convert timestamp to a number of milliseconds (10^-3 seconds). */
Victor Stinner992c43f2015-03-27 17:12:45 +010097PyAPI_FUNC(_PyTime_t) _PyTime_AsMilliseconds(_PyTime_t t,
Victor Stinnercb29f012015-03-27 13:31:18 +010098 _PyTime_round_t round);
99
Victor Stinnerf5faad22015-03-28 03:52:05 +0100100/* Convert timestamp to a number of microseconds (10^-6 seconds). */
101PyAPI_FUNC(_PyTime_t) _PyTime_AsMicroseconds(_PyTime_t t,
102 _PyTime_round_t round);
103
Victor Stinner992c43f2015-03-27 17:12:45 +0100104/* Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int
105 object. */
106PyAPI_FUNC(PyObject *) _PyTime_AsNanosecondsObject(_PyTime_t t);
107
Victor Stinner4bfb4602015-03-27 22:27:24 +0100108/* Convert a timestamp to a timeval structure (microsecond resolution).
Victor Stinner95e9cef2015-03-28 01:26:47 +0100109 tv_usec is always positive.
110 Return -1 if the conversion overflowed, return 0 on success. */
Victor Stinnercb29f012015-03-27 13:31:18 +0100111PyAPI_FUNC(int) _PyTime_AsTimeval(_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
128/* Get the current time from the system clock.
Victor Stinnera47b8812015-03-27 18:16:17 +0100129 * Fill clock information if info is not NULL.
130 * Raise an exception and return -1 on error, return 0 on success.
131 */
132PyAPI_FUNC(int) _PyTime_GetSystemClockWithInfo(
133 _PyTime_t *t,
134 _Py_clock_info_t *info);
135
Victor Stinnercb29f012015-03-27 13:31:18 +0100136/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
137 The clock is not affected by system clock updates. The reference point of
138 the returned value is undefined, so that only the difference between the
139 results of consecutive calls is valid.
140
141 The function cannot fail. _PyTime_Init() ensures that a monotonic clock
142 is available and works. */
143PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void);
144
Victor Stinner4bfb4602015-03-27 22:27:24 +0100145/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
146 The clock is not affected by system clock updates. The reference point of
147 the returned value is undefined, so that only the difference between the
148 results of consecutive calls is valid.
149
150 Fill info (if set) with information of the function used to get the time.
151
152 Return 0 on success, raise an exception and return -1 on error. */
153PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo(
154 _PyTime_t *t,
155 _Py_clock_info_t *info);
156
Victor Stinnercb29f012015-03-27 13:31:18 +0100157
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000158#ifdef __cplusplus
159}
160#endif
161
162#endif /* Py_PYTIME_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000163#endif /* Py_LIMITED_API */