Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1 | #ifndef Py_LIMITED_API |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 2 | #ifndef Py_PYTIME_H |
| 3 | #define Py_PYTIME_H |
| 4 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 5 | #include "pyconfig.h" /* include for defines */ |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 6 | #include "object.h" |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 7 | |
| 8 | /************************************************************************** |
| 9 | Symbols and macros to supply platform-independent interfaces to time related |
| 10 | functions and constants |
| 11 | **************************************************************************/ |
| 12 | #ifdef __cplusplus |
| 13 | extern "C" { |
| 14 | #endif |
| 15 | |
Victor Stinner | 1bd18ba | 2015-03-30 00:25:38 +0200 | [diff] [blame] | 16 | /* _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 Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 19 | typedef int64_t _PyTime_t; |
Victor Stinner | 1bd18ba | 2015-03-30 00:25:38 +0200 | [diff] [blame] | 20 | #define _PyTime_MIN PY_LLONG_MIN |
| 21 | #define _PyTime_MAX PY_LLONG_MAX |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 22 | |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 23 | typedef enum { |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 24 | /* Round towards minus infinity (-inf). |
| 25 | For example, used to read a clock. */ |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 26 | _PyTime_ROUND_FLOOR=0, |
Victor Stinner | bcdd777 | 2015-03-30 03:52:49 +0200 | [diff] [blame] | 27 | /* Round towards infinity (+inf). |
| 28 | For example, used for timeout to wait "at least" N seconds. */ |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 29 | _PyTime_ROUND_CEILING=1, |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 30 | /* Round to nearest with ties going to nearest even integer. |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 31 | For example, used to round from a Python float. */ |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 32 | _PyTime_ROUND_HALF_EVEN |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 33 | } _PyTime_round_t; |
| 34 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 35 | /* Convert a time_t to a PyLong. */ |
| 36 | PyAPI_FUNC(PyObject *) _PyLong_FromTime_t( |
| 37 | time_t sec); |
| 38 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 39 | /* Convert a PyLong to a time_t. */ |
| 40 | PyAPI_FUNC(time_t) _PyLong_AsTime_t( |
| 41 | PyObject *obj); |
| 42 | |
Victor Stinner | 1bd18ba | 2015-03-30 00:25:38 +0200 | [diff] [blame] | 43 | /* Convert a number of seconds, int or float, to time_t. */ |
| 44 | PyAPI_FUNC(int) _PyTime_ObjectToTime_t( |
| 45 | PyObject *obj, |
| 46 | time_t *sec, |
| 47 | _PyTime_round_t); |
| 48 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 49 | /* 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). */ |
| 52 | PyAPI_FUNC(int) _PyTime_ObjectToTimeval( |
| 53 | PyObject *obj, |
| 54 | time_t *sec, |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 55 | long *usec, |
| 56 | _PyTime_round_t); |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 57 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 58 | /* Convert a number of seconds, int or float, to a timespec structure. |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 59 | nsec is in the range [0; 999999999] and rounded towards zero. |
| 60 | For example, -1.2 is converted to (-2, 800000000). */ |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 61 | PyAPI_FUNC(int) _PyTime_ObjectToTimespec( |
| 62 | PyObject *obj, |
| 63 | time_t *sec, |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 64 | long *nsec, |
| 65 | _PyTime_round_t); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 66 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 67 | |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 68 | /* Create a timestamp from a number of seconds. */ |
Victor Stinner | 88ed640 | 2015-04-09 10:23:12 +0200 | [diff] [blame] | 69 | PyAPI_FUNC(_PyTime_t) _PyTime_FromSeconds(int seconds); |
| 70 | |
| 71 | /* Macro to create a timestamp from a number of seconds, no integer overflow. |
| 72 | Only use the macro for small values, prefer _PyTime_FromSeconds(). */ |
| 73 | #define _PYTIME_FROMSECONDS(seconds) \ |
| 74 | ((_PyTime_t)(seconds) * (1000 * 1000 * 1000)) |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 75 | |
| 76 | /* Create a timestamp from a number of nanoseconds. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 77 | PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(long long ns); |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 78 | |
Victor Stinner | fa09beb | 2015-03-30 21:36:10 +0200 | [diff] [blame] | 79 | /* Convert a number of seconds (Python float or int) to a timetamp. |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 80 | Raise an exception and return -1 on error, return 0 on success. */ |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 81 | PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t, |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 82 | PyObject *obj, |
| 83 | _PyTime_round_t round); |
| 84 | |
Victor Stinner | fa09beb | 2015-03-30 21:36:10 +0200 | [diff] [blame] | 85 | /* Convert a number of milliseconds (Python float or int, 10^-3) to a timetamp. |
| 86 | Raise an exception and return -1 on error, return 0 on success. */ |
| 87 | PyAPI_FUNC(int) _PyTime_FromMillisecondsObject(_PyTime_t *t, |
| 88 | PyObject *obj, |
| 89 | _PyTime_round_t round); |
| 90 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 91 | /* Convert a timestamp to a number of seconds as a C double. */ |
| 92 | PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t); |
| 93 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 94 | /* Convert timestamp to a number of milliseconds (10^-3 seconds). */ |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 95 | PyAPI_FUNC(_PyTime_t) _PyTime_AsMilliseconds(_PyTime_t t, |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 96 | _PyTime_round_t round); |
| 97 | |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 98 | /* Convert timestamp to a number of microseconds (10^-6 seconds). */ |
| 99 | PyAPI_FUNC(_PyTime_t) _PyTime_AsMicroseconds(_PyTime_t t, |
| 100 | _PyTime_round_t round); |
| 101 | |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 102 | /* Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int |
| 103 | object. */ |
| 104 | PyAPI_FUNC(PyObject *) _PyTime_AsNanosecondsObject(_PyTime_t t); |
| 105 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 106 | /* Convert a timestamp to a timeval structure (microsecond resolution). |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 107 | tv_usec is always positive. |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 108 | Raise an exception and return -1 if the conversion overflowed, |
| 109 | return 0 on success. */ |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 110 | PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t, |
| 111 | struct timeval *tv, |
| 112 | _PyTime_round_t round); |
| 113 | |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 114 | /* Similar to _PyTime_AsTimeval(), but don't raise an exception on error. */ |
| 115 | PyAPI_FUNC(int) _PyTime_AsTimeval_noraise(_PyTime_t t, |
| 116 | struct timeval *tv, |
| 117 | _PyTime_round_t round); |
| 118 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 119 | /* Convert a timestamp to a number of seconds (secs) and microseconds (us). |
| 120 | us is always positive. This function is similar to _PyTime_AsTimeval() |
| 121 | except that secs is always a time_t type, whereas the timeval structure |
| 122 | uses a C long for tv_sec on Windows. |
| 123 | Raise an exception and return -1 if the conversion overflowed, |
| 124 | return 0 on success. */ |
| 125 | PyAPI_FUNC(int) _PyTime_AsTimevalTime_t( |
| 126 | _PyTime_t t, |
| 127 | time_t *secs, |
| 128 | int *us, |
| 129 | _PyTime_round_t round); |
| 130 | |
Victor Stinner | c337838 | 2015-03-28 05:07:51 +0100 | [diff] [blame] | 131 | #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE) |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 132 | /* Convert a timestamp to a timespec structure (nanosecond resolution). |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 133 | tv_nsec is always positive. |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 134 | Raise an exception and return -1 on error, return 0 on success. */ |
| 135 | PyAPI_FUNC(int) _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts); |
| 136 | #endif |
| 137 | |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 138 | /* Get the current time from the system clock. |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 139 | |
| 140 | The function cannot fail. _PyTime_Init() ensures that the system clock |
| 141 | works. */ |
| 142 | PyAPI_FUNC(_PyTime_t) _PyTime_GetSystemClock(void); |
| 143 | |
Victor Stinner | 1bd18ba | 2015-03-30 00:25:38 +0200 | [diff] [blame] | 144 | /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards. |
| 145 | The clock is not affected by system clock updates. The reference point of |
| 146 | the returned value is undefined, so that only the difference between the |
| 147 | results of consecutive calls is valid. |
| 148 | |
| 149 | The function cannot fail. _PyTime_Init() ensures that a monotonic clock |
| 150 | is available and works. */ |
| 151 | PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void); |
| 152 | |
| 153 | |
| 154 | /* Structure used by time.get_clock_info() */ |
| 155 | typedef struct { |
| 156 | const char *implementation; |
| 157 | int monotonic; |
| 158 | int adjustable; |
| 159 | double resolution; |
| 160 | } _Py_clock_info_t; |
| 161 | |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 162 | /* Get the current time from the system clock. |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 163 | * Fill clock information if info is not NULL. |
| 164 | * Raise an exception and return -1 on error, return 0 on success. |
| 165 | */ |
| 166 | PyAPI_FUNC(int) _PyTime_GetSystemClockWithInfo( |
| 167 | _PyTime_t *t, |
| 168 | _Py_clock_info_t *info); |
| 169 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 170 | /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards. |
| 171 | The clock is not affected by system clock updates. The reference point of |
| 172 | the returned value is undefined, so that only the difference between the |
| 173 | results of consecutive calls is valid. |
| 174 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 175 | Fill info (if set) with information of the function used to get the time. |
| 176 | |
| 177 | Return 0 on success, raise an exception and return -1 on error. */ |
| 178 | PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo( |
| 179 | _PyTime_t *t, |
| 180 | _Py_clock_info_t *info); |
| 181 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 182 | |
Victor Stinner | 1bd18ba | 2015-03-30 00:25:38 +0200 | [diff] [blame] | 183 | /* Initialize time. |
| 184 | Return 0 on success, raise an exception and return -1 on error. */ |
| 185 | PyAPI_FUNC(int) _PyTime_Init(void); |
| 186 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 187 | /* Converts a timestamp to the Gregorian time, using the local time zone. |
| 188 | Return 0 on success, raise an exception and return -1 on error. */ |
| 189 | PyAPI_FUNC(int) _PyTime_localtime(time_t t, struct tm *tm); |
| 190 | |
| 191 | /* Converts a timestamp to the Gregorian time, assuming UTC. |
| 192 | Return 0 on success, raise an exception and return -1 on error. */ |
| 193 | PyAPI_FUNC(int) _PyTime_gmtime(time_t t, struct tm *tm); |
| 194 | |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 195 | #ifdef __cplusplus |
| 196 | } |
| 197 | #endif |
| 198 | |
| 199 | #endif /* Py_PYTIME_H */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 200 | #endif /* Py_LIMITED_API */ |