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 | |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 5 | /************************************************************************** |
| 6 | Symbols and macros to supply platform-independent interfaces to time related |
| 7 | functions and constants |
| 8 | **************************************************************************/ |
| 9 | #ifdef __cplusplus |
| 10 | extern "C" { |
| 11 | #endif |
| 12 | |
Victor Stinner | 1bd18ba | 2015-03-30 00:25:38 +0200 | [diff] [blame] | 13 | /* _PyTime_t: Python timestamp with subsecond precision. It can be used to |
| 14 | store a duration, and so indirectly a date (related to another date, like |
| 15 | UNIX epoch). */ |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 16 | typedef int64_t _PyTime_t; |
Sergey Fedoseev | 8e76c45 | 2019-08-23 20:39:09 +0500 | [diff] [blame] | 17 | #define _PyTime_MIN INT64_MIN |
| 18 | #define _PyTime_MAX INT64_MAX |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 19 | |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 20 | typedef enum { |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 21 | /* Round towards minus infinity (-inf). |
| 22 | For example, used to read a clock. */ |
Victor Stinner | a695f83 | 2015-03-30 03:57:14 +0200 | [diff] [blame] | 23 | _PyTime_ROUND_FLOOR=0, |
Victor Stinner | bcdd777 | 2015-03-30 03:52:49 +0200 | [diff] [blame] | 24 | /* Round towards infinity (+inf). |
| 25 | For example, used for timeout to wait "at least" N seconds. */ |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 26 | _PyTime_ROUND_CEILING=1, |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 27 | /* Round to nearest with ties going to nearest even integer. |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 28 | For example, used to round from a Python float. */ |
Pablo Galindo | 2c15b29 | 2017-10-17 15:14:41 +0100 | [diff] [blame] | 29 | _PyTime_ROUND_HALF_EVEN=2, |
Serhiy Storchaka | bdf4298 | 2017-10-26 16:59:40 +0300 | [diff] [blame] | 30 | /* Round away from zero |
Pablo Galindo | 2c15b29 | 2017-10-17 15:14:41 +0100 | [diff] [blame] | 31 | For example, used for timeout. _PyTime_ROUND_CEILING rounds |
| 32 | -1e-9 to 0 milliseconds which causes bpo-31786 issue. |
| 33 | _PyTime_ROUND_UP rounds -1e-9 to -1 millisecond which keeps |
| 34 | the timeout sign as expected. select.poll(timeout) must block |
| 35 | for negative values." */ |
| 36 | _PyTime_ROUND_UP=3, |
Serhiy Storchaka | bdf4298 | 2017-10-26 16:59:40 +0300 | [diff] [blame] | 37 | /* _PyTime_ROUND_TIMEOUT (an alias for _PyTime_ROUND_UP) should be |
Pablo Galindo | 2c15b29 | 2017-10-17 15:14:41 +0100 | [diff] [blame] | 38 | used for timeouts. */ |
| 39 | _PyTime_ROUND_TIMEOUT = _PyTime_ROUND_UP |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 40 | } _PyTime_round_t; |
| 41 | |
Pablo Galindo | 2c15b29 | 2017-10-17 15:14:41 +0100 | [diff] [blame] | 42 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 43 | /* Convert a time_t to a PyLong. */ |
| 44 | PyAPI_FUNC(PyObject *) _PyLong_FromTime_t( |
| 45 | time_t sec); |
| 46 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 47 | /* Convert a PyLong to a time_t. */ |
| 48 | PyAPI_FUNC(time_t) _PyLong_AsTime_t( |
| 49 | PyObject *obj); |
| 50 | |
Victor Stinner | 1bd18ba | 2015-03-30 00:25:38 +0200 | [diff] [blame] | 51 | /* Convert a number of seconds, int or float, to time_t. */ |
| 52 | PyAPI_FUNC(int) _PyTime_ObjectToTime_t( |
| 53 | PyObject *obj, |
| 54 | time_t *sec, |
| 55 | _PyTime_round_t); |
| 56 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 57 | /* Convert a number of seconds, int or float, to a timeval structure. |
| 58 | usec is in the range [0; 999999] and rounded towards zero. |
| 59 | For example, -1.2 is converted to (-2, 800000). */ |
| 60 | PyAPI_FUNC(int) _PyTime_ObjectToTimeval( |
| 61 | PyObject *obj, |
| 62 | time_t *sec, |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 63 | long *usec, |
| 64 | _PyTime_round_t); |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 65 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 66 | /* Convert a number of seconds, int or float, to a timespec structure. |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 67 | nsec is in the range [0; 999999999] and rounded towards zero. |
| 68 | For example, -1.2 is converted to (-2, 800000000). */ |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 69 | PyAPI_FUNC(int) _PyTime_ObjectToTimespec( |
| 70 | PyObject *obj, |
| 71 | time_t *sec, |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 72 | long *nsec, |
| 73 | _PyTime_round_t); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 74 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 75 | |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 76 | /* Create a timestamp from a number of seconds. */ |
Victor Stinner | 88ed640 | 2015-04-09 10:23:12 +0200 | [diff] [blame] | 77 | PyAPI_FUNC(_PyTime_t) _PyTime_FromSeconds(int seconds); |
| 78 | |
| 79 | /* Macro to create a timestamp from a number of seconds, no integer overflow. |
| 80 | Only use the macro for small values, prefer _PyTime_FromSeconds(). */ |
| 81 | #define _PYTIME_FROMSECONDS(seconds) \ |
| 82 | ((_PyTime_t)(seconds) * (1000 * 1000 * 1000)) |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 83 | |
| 84 | /* Create a timestamp from a number of nanoseconds. */ |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 85 | PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(_PyTime_t ns); |
| 86 | |
| 87 | /* Create a timestamp from nanoseconds (Python int). */ |
| 88 | PyAPI_FUNC(int) _PyTime_FromNanosecondsObject(_PyTime_t *t, |
| 89 | PyObject *obj); |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 90 | |
Victor Stinner | fa09beb | 2015-03-30 21:36:10 +0200 | [diff] [blame] | 91 | /* Convert a number of seconds (Python float or int) to a timetamp. |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 92 | Raise an exception and return -1 on error, return 0 on success. */ |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 93 | PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t, |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 94 | PyObject *obj, |
| 95 | _PyTime_round_t round); |
| 96 | |
Victor Stinner | fa09beb | 2015-03-30 21:36:10 +0200 | [diff] [blame] | 97 | /* Convert a number of milliseconds (Python float or int, 10^-3) to a timetamp. |
| 98 | Raise an exception and return -1 on error, return 0 on success. */ |
| 99 | PyAPI_FUNC(int) _PyTime_FromMillisecondsObject(_PyTime_t *t, |
| 100 | PyObject *obj, |
| 101 | _PyTime_round_t round); |
| 102 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 103 | /* Convert a timestamp to a number of seconds as a C double. */ |
| 104 | PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t); |
| 105 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 106 | /* Convert timestamp to a number of milliseconds (10^-3 seconds). */ |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 107 | PyAPI_FUNC(_PyTime_t) _PyTime_AsMilliseconds(_PyTime_t t, |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 108 | _PyTime_round_t round); |
| 109 | |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 110 | /* Convert timestamp to a number of microseconds (10^-6 seconds). */ |
| 111 | PyAPI_FUNC(_PyTime_t) _PyTime_AsMicroseconds(_PyTime_t t, |
| 112 | _PyTime_round_t round); |
| 113 | |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 114 | /* Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int |
| 115 | object. */ |
| 116 | PyAPI_FUNC(PyObject *) _PyTime_AsNanosecondsObject(_PyTime_t t); |
| 117 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 118 | /* Create a timestamp from a timeval structure. |
| 119 | Raise an exception and return -1 on overflow, return 0 on success. */ |
| 120 | PyAPI_FUNC(int) _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv); |
| 121 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 122 | /* Convert a timestamp to a timeval structure (microsecond resolution). |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 123 | tv_usec is always positive. |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 124 | Raise an exception and return -1 if the conversion overflowed, |
| 125 | return 0 on success. */ |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 126 | PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t, |
| 127 | struct timeval *tv, |
| 128 | _PyTime_round_t round); |
| 129 | |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 130 | /* Similar to _PyTime_AsTimeval(), but don't raise an exception on error. */ |
| 131 | PyAPI_FUNC(int) _PyTime_AsTimeval_noraise(_PyTime_t t, |
| 132 | struct timeval *tv, |
| 133 | _PyTime_round_t round); |
| 134 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 135 | /* Convert a timestamp to a number of seconds (secs) and microseconds (us). |
| 136 | us is always positive. This function is similar to _PyTime_AsTimeval() |
| 137 | except that secs is always a time_t type, whereas the timeval structure |
| 138 | uses a C long for tv_sec on Windows. |
| 139 | Raise an exception and return -1 if the conversion overflowed, |
| 140 | return 0 on success. */ |
| 141 | PyAPI_FUNC(int) _PyTime_AsTimevalTime_t( |
| 142 | _PyTime_t t, |
| 143 | time_t *secs, |
| 144 | int *us, |
| 145 | _PyTime_round_t round); |
| 146 | |
Victor Stinner | c337838 | 2015-03-28 05:07:51 +0100 | [diff] [blame] | 147 | #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE) |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 148 | /* Create a timestamp from a timespec structure. |
| 149 | Raise an exception and return -1 on overflow, return 0 on success. */ |
| 150 | PyAPI_FUNC(int) _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts); |
| 151 | |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 152 | /* Convert a timestamp to a timespec structure (nanosecond resolution). |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 153 | tv_nsec is always positive. |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 154 | Raise an exception and return -1 on error, return 0 on success. */ |
| 155 | PyAPI_FUNC(int) _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts); |
| 156 | #endif |
| 157 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 158 | /* Compute ticks * mul / div. |
| 159 | The caller must ensure that ((div - 1) * mul) cannot overflow. */ |
| 160 | PyAPI_FUNC(_PyTime_t) _PyTime_MulDiv(_PyTime_t ticks, |
| 161 | _PyTime_t mul, |
| 162 | _PyTime_t div); |
| 163 | |
Victor Stinner | 1bd18ba | 2015-03-30 00:25:38 +0200 | [diff] [blame] | 164 | /* Structure used by time.get_clock_info() */ |
| 165 | typedef struct { |
| 166 | const char *implementation; |
| 167 | int monotonic; |
| 168 | int adjustable; |
| 169 | double resolution; |
| 170 | } _Py_clock_info_t; |
| 171 | |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 172 | /* Get the current time from the system clock. |
Victor Stinner | ae6cd7c | 2020-11-16 16:08:05 +0100 | [diff] [blame] | 173 | |
| 174 | If the internal clock fails, silently ignore the error and return 0. |
| 175 | On integer overflow, silently ignore the overflow and truncated the clock to |
| 176 | _PyTime_MIN or _PyTime_MAX. |
| 177 | |
| 178 | Use _PyTime_GetSystemClockWithInfo() to check for failure. */ |
| 179 | PyAPI_FUNC(_PyTime_t) _PyTime_GetSystemClock(void); |
| 180 | |
| 181 | /* Get the current time from the system clock. |
| 182 | * On success, set *t and *info (if not NULL), and return 0. |
| 183 | * On error, raise an exception and return -1. |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 184 | */ |
| 185 | PyAPI_FUNC(int) _PyTime_GetSystemClockWithInfo( |
| 186 | _PyTime_t *t, |
| 187 | _Py_clock_info_t *info); |
| 188 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 189 | /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards. |
| 190 | The clock is not affected by system clock updates. The reference point of |
| 191 | the returned value is undefined, so that only the difference between the |
| 192 | results of consecutive calls is valid. |
| 193 | |
Victor Stinner | ae6cd7c | 2020-11-16 16:08:05 +0100 | [diff] [blame] | 194 | If the internal clock fails, silently ignore the error and return 0. |
| 195 | On integer overflow, silently ignore the overflow and truncated the clock to |
| 196 | _PyTime_MIN or _PyTime_MAX. |
| 197 | |
| 198 | Use _PyTime_GetMonotonicClockWithInfo() to check for failure. */ |
| 199 | PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void); |
| 200 | |
| 201 | /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards. |
| 202 | The clock is not affected by system clock updates. The reference point of |
| 203 | the returned value is undefined, so that only the difference between the |
| 204 | results of consecutive calls is valid. |
| 205 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 206 | Fill info (if set) with information of the function used to get the time. |
| 207 | |
| 208 | Return 0 on success, raise an exception and return -1 on error. */ |
| 209 | PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo( |
| 210 | _PyTime_t *t, |
| 211 | _Py_clock_info_t *info); |
| 212 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 213 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 214 | /* Converts a timestamp to the Gregorian time, using the local time zone. |
| 215 | Return 0 on success, raise an exception and return -1 on error. */ |
| 216 | PyAPI_FUNC(int) _PyTime_localtime(time_t t, struct tm *tm); |
| 217 | |
| 218 | /* Converts a timestamp to the Gregorian time, assuming UTC. |
| 219 | Return 0 on success, raise an exception and return -1 on error. */ |
| 220 | PyAPI_FUNC(int) _PyTime_gmtime(time_t t, struct tm *tm); |
| 221 | |
Victor Stinner | cba9a0c | 2017-10-12 08:51:56 -0700 | [diff] [blame] | 222 | /* Get the performance counter: clock with the highest available resolution to |
| 223 | measure a short duration. |
| 224 | |
Victor Stinner | ae6cd7c | 2020-11-16 16:08:05 +0100 | [diff] [blame] | 225 | If the internal clock fails, silently ignore the error and return 0. |
| 226 | On integer overflow, silently ignore the overflow and truncated the clock to |
| 227 | _PyTime_MIN or _PyTime_MAX. |
| 228 | |
| 229 | Use _PyTime_GetPerfCounterWithInfo() to check for failure. */ |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 230 | PyAPI_FUNC(_PyTime_t) _PyTime_GetPerfCounter(void); |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 231 | |
| 232 | /* Get the performance counter: clock with the highest available resolution to |
Victor Stinner | cba9a0c | 2017-10-12 08:51:56 -0700 | [diff] [blame] | 233 | measure a short duration. |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 234 | |
Victor Stinner | cba9a0c | 2017-10-12 08:51:56 -0700 | [diff] [blame] | 235 | Fill info (if set) with information of the function used to get the time. |
| 236 | |
| 237 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 238 | PyAPI_FUNC(int) _PyTime_GetPerfCounterWithInfo( |
| 239 | _PyTime_t *t, |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 240 | _Py_clock_info_t *info); |
| 241 | |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 242 | #ifdef __cplusplus |
| 243 | } |
| 244 | #endif |
| 245 | |
| 246 | #endif /* Py_PYTIME_H */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 247 | #endif /* Py_LIMITED_API */ |