2010-12-03 16:14:31 -04:00
|
|
|
#ifndef Py_LIMITED_API
|
2010-08-05 14:34:27 -03:00
|
|
|
#ifndef Py_PYTIME_H
|
|
|
|
#define Py_PYTIME_H
|
|
|
|
|
2012-02-08 18:03:19 -04:00
|
|
|
#include "pyconfig.h" /* include for defines */
|
2012-03-02 17:54:03 -04:00
|
|
|
#include "object.h"
|
2010-08-05 14:34:27 -03:00
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
Symbols and macros to supply platform-independent interfaces to time related
|
|
|
|
functions and constants
|
|
|
|
**************************************************************************/
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-03-29 19:25:38 -03:00
|
|
|
/* _PyTime_t: Python timestamp with subsecond precision. It can be used to
|
|
|
|
store a duration, and so indirectly a date (related to another date, like
|
|
|
|
UNIX epoch). */
|
2016-09-06 17:24:00 -03:00
|
|
|
typedef int64_t _PyTime_t;
|
2019-08-23 12:39:09 -03:00
|
|
|
#define _PyTime_MIN INT64_MIN
|
|
|
|
#define _PyTime_MAX INT64_MAX
|
2012-04-28 21:41:27 -03:00
|
|
|
|
2014-02-16 19:02:43 -04:00
|
|
|
typedef enum {
|
2015-03-28 01:02:39 -03:00
|
|
|
/* Round towards minus infinity (-inf).
|
|
|
|
For example, used to read a clock. */
|
2015-03-29 22:57:14 -03:00
|
|
|
_PyTime_ROUND_FLOOR=0,
|
2015-03-29 22:52:49 -03:00
|
|
|
/* Round towards infinity (+inf).
|
|
|
|
For example, used for timeout to wait "at least" N seconds. */
|
2015-09-01 20:43:56 -03:00
|
|
|
_PyTime_ROUND_CEILING=1,
|
2015-09-08 20:02:23 -03:00
|
|
|
/* Round to nearest with ties going to nearest even integer.
|
2015-09-01 20:43:56 -03:00
|
|
|
For example, used to round from a Python float. */
|
2017-10-17 11:14:41 -03:00
|
|
|
_PyTime_ROUND_HALF_EVEN=2,
|
2017-10-26 10:59:40 -03:00
|
|
|
/* Round away from zero
|
2017-10-17 11:14:41 -03:00
|
|
|
For example, used for timeout. _PyTime_ROUND_CEILING rounds
|
|
|
|
-1e-9 to 0 milliseconds which causes bpo-31786 issue.
|
|
|
|
_PyTime_ROUND_UP rounds -1e-9 to -1 millisecond which keeps
|
|
|
|
the timeout sign as expected. select.poll(timeout) must block
|
|
|
|
for negative values." */
|
|
|
|
_PyTime_ROUND_UP=3,
|
2017-10-26 10:59:40 -03:00
|
|
|
/* _PyTime_ROUND_TIMEOUT (an alias for _PyTime_ROUND_UP) should be
|
2017-10-17 11:14:41 -03:00
|
|
|
used for timeouts. */
|
|
|
|
_PyTime_ROUND_TIMEOUT = _PyTime_ROUND_UP
|
2014-02-16 19:02:43 -04:00
|
|
|
} _PyTime_round_t;
|
|
|
|
|
2017-10-17 11:14:41 -03:00
|
|
|
|
2012-04-19 19:07:49 -03:00
|
|
|
/* Convert a time_t to a PyLong. */
|
|
|
|
PyAPI_FUNC(PyObject *) _PyLong_FromTime_t(
|
|
|
|
time_t sec);
|
|
|
|
|
2012-05-03 04:30:07 -03:00
|
|
|
/* Convert a PyLong to a time_t. */
|
|
|
|
PyAPI_FUNC(time_t) _PyLong_AsTime_t(
|
|
|
|
PyObject *obj);
|
|
|
|
|
2015-03-29 19:25:38 -03:00
|
|
|
/* Convert a number of seconds, int or float, to time_t. */
|
|
|
|
PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
|
|
|
|
PyObject *obj,
|
|
|
|
time_t *sec,
|
|
|
|
_PyTime_round_t);
|
|
|
|
|
2012-03-13 09:35:55 -03:00
|
|
|
/* Convert a number of seconds, int or float, to a timeval structure.
|
|
|
|
usec is in the range [0; 999999] and rounded towards zero.
|
|
|
|
For example, -1.2 is converted to (-2, 800000). */
|
|
|
|
PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
|
|
|
|
PyObject *obj,
|
|
|
|
time_t *sec,
|
2014-02-16 19:02:43 -04:00
|
|
|
long *usec,
|
|
|
|
_PyTime_round_t);
|
2012-03-13 09:35:55 -03:00
|
|
|
|
2012-03-02 17:54:03 -04:00
|
|
|
/* Convert a number of seconds, int or float, to a timespec structure.
|
2012-03-13 09:35:55 -03:00
|
|
|
nsec is in the range [0; 999999999] and rounded towards zero.
|
|
|
|
For example, -1.2 is converted to (-2, 800000000). */
|
2012-03-02 17:54:03 -04:00
|
|
|
PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
|
|
|
|
PyObject *obj,
|
|
|
|
time_t *sec,
|
2014-02-16 19:02:43 -04:00
|
|
|
long *nsec,
|
|
|
|
_PyTime_round_t);
|
2012-03-02 17:54:03 -04:00
|
|
|
|
2015-03-27 09:31:18 -03:00
|
|
|
|
2015-04-03 08:10:54 -03:00
|
|
|
/* Create a timestamp from a number of seconds. */
|
2015-04-09 05:23:12 -03:00
|
|
|
PyAPI_FUNC(_PyTime_t) _PyTime_FromSeconds(int seconds);
|
|
|
|
|
|
|
|
/* Macro to create a timestamp from a number of seconds, no integer overflow.
|
|
|
|
Only use the macro for small values, prefer _PyTime_FromSeconds(). */
|
|
|
|
#define _PYTIME_FROMSECONDS(seconds) \
|
|
|
|
((_PyTime_t)(seconds) * (1000 * 1000 * 1000))
|
2015-04-03 08:10:54 -03:00
|
|
|
|
|
|
|
/* Create a timestamp from a number of nanoseconds. */
|
2017-11-02 11:28:27 -03:00
|
|
|
PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(_PyTime_t ns);
|
|
|
|
|
|
|
|
/* Create a timestamp from nanoseconds (Python int). */
|
|
|
|
PyAPI_FUNC(int) _PyTime_FromNanosecondsObject(_PyTime_t *t,
|
|
|
|
PyObject *obj);
|
2015-03-27 18:27:24 -03:00
|
|
|
|
2015-03-30 16:36:10 -03:00
|
|
|
/* Convert a number of seconds (Python float or int) to a timetamp.
|
2015-03-27 09:31:18 -03:00
|
|
|
Raise an exception and return -1 on error, return 0 on success. */
|
2015-03-27 13:12:45 -03:00
|
|
|
PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t,
|
2015-03-27 09:31:18 -03:00
|
|
|
PyObject *obj,
|
|
|
|
_PyTime_round_t round);
|
|
|
|
|
2015-03-30 16:36:10 -03:00
|
|
|
/* Convert a number of milliseconds (Python float or int, 10^-3) to a timetamp.
|
|
|
|
Raise an exception and return -1 on error, return 0 on success. */
|
|
|
|
PyAPI_FUNC(int) _PyTime_FromMillisecondsObject(_PyTime_t *t,
|
|
|
|
PyObject *obj,
|
|
|
|
_PyTime_round_t round);
|
|
|
|
|
2015-03-27 18:27:24 -03:00
|
|
|
/* Convert a timestamp to a number of seconds as a C double. */
|
|
|
|
PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t);
|
|
|
|
|
2015-03-27 09:31:18 -03:00
|
|
|
/* Convert timestamp to a number of milliseconds (10^-3 seconds). */
|
2015-03-27 13:12:45 -03:00
|
|
|
PyAPI_FUNC(_PyTime_t) _PyTime_AsMilliseconds(_PyTime_t t,
|
2015-03-27 23:52:05 -03:00
|
|
|
_PyTime_round_t round);
|
|
|
|
|
|
|
|
/* Convert timestamp to a number of microseconds (10^-6 seconds). */
|
|
|
|
PyAPI_FUNC(_PyTime_t) _PyTime_AsMicroseconds(_PyTime_t t,
|
2015-03-27 09:31:18 -03:00
|
|
|
_PyTime_round_t round);
|
|
|
|
|
2015-03-27 13:12:45 -03:00
|
|
|
/* Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int
|
|
|
|
object. */
|
|
|
|
PyAPI_FUNC(PyObject *) _PyTime_AsNanosecondsObject(_PyTime_t t);
|
|
|
|
|
2017-11-02 11:28:27 -03:00
|
|
|
/* Create a timestamp from a timeval structure.
|
|
|
|
Raise an exception and return -1 on overflow, return 0 on success. */
|
|
|
|
PyAPI_FUNC(int) _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv);
|
|
|
|
|
2015-03-27 18:27:24 -03:00
|
|
|
/* Convert a timestamp to a timeval structure (microsecond resolution).
|
2015-03-27 21:26:47 -03:00
|
|
|
tv_usec is always positive.
|
2015-03-29 21:51:13 -03:00
|
|
|
Raise an exception and return -1 if the conversion overflowed,
|
|
|
|
return 0 on success. */
|
2015-03-27 09:31:18 -03:00
|
|
|
PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t,
|
|
|
|
struct timeval *tv,
|
|
|
|
_PyTime_round_t round);
|
|
|
|
|
2015-03-29 21:51:13 -03:00
|
|
|
/* Similar to _PyTime_AsTimeval(), but don't raise an exception on error. */
|
|
|
|
PyAPI_FUNC(int) _PyTime_AsTimeval_noraise(_PyTime_t t,
|
|
|
|
struct timeval *tv,
|
|
|
|
_PyTime_round_t round);
|
|
|
|
|
2015-09-18 08:23:02 -03:00
|
|
|
/* Convert a timestamp to a number of seconds (secs) and microseconds (us).
|
|
|
|
us is always positive. This function is similar to _PyTime_AsTimeval()
|
|
|
|
except that secs is always a time_t type, whereas the timeval structure
|
|
|
|
uses a C long for tv_sec on Windows.
|
|
|
|
Raise an exception and return -1 if the conversion overflowed,
|
|
|
|
return 0 on success. */
|
|
|
|
PyAPI_FUNC(int) _PyTime_AsTimevalTime_t(
|
|
|
|
_PyTime_t t,
|
|
|
|
time_t *secs,
|
|
|
|
int *us,
|
|
|
|
_PyTime_round_t round);
|
|
|
|
|
2015-03-28 01:07:51 -03:00
|
|
|
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
|
2017-11-02 11:28:27 -03:00
|
|
|
/* Create a timestamp from a timespec structure.
|
|
|
|
Raise an exception and return -1 on overflow, return 0 on success. */
|
|
|
|
PyAPI_FUNC(int) _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts);
|
|
|
|
|
2015-03-27 14:19:03 -03:00
|
|
|
/* Convert a timestamp to a timespec structure (nanosecond resolution).
|
2015-03-27 21:26:47 -03:00
|
|
|
tv_nsec is always positive.
|
2015-03-27 14:19:03 -03:00
|
|
|
Raise an exception and return -1 on error, return 0 on success. */
|
|
|
|
PyAPI_FUNC(int) _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts);
|
|
|
|
#endif
|
|
|
|
|
2017-11-02 11:28:27 -03:00
|
|
|
/* Compute ticks * mul / div.
|
|
|
|
The caller must ensure that ((div - 1) * mul) cannot overflow. */
|
|
|
|
PyAPI_FUNC(_PyTime_t) _PyTime_MulDiv(_PyTime_t ticks,
|
|
|
|
_PyTime_t mul,
|
|
|
|
_PyTime_t div);
|
|
|
|
|
2015-03-29 19:25:38 -03:00
|
|
|
/* Structure used by time.get_clock_info() */
|
|
|
|
typedef struct {
|
|
|
|
const char *implementation;
|
|
|
|
int monotonic;
|
|
|
|
int adjustable;
|
|
|
|
double resolution;
|
|
|
|
} _Py_clock_info_t;
|
|
|
|
|
|
|
|
/* Get the current time from the system clock.
|
2020-11-16 11:08:05 -04:00
|
|
|
|
|
|
|
If the internal clock fails, silently ignore the error and return 0.
|
|
|
|
On integer overflow, silently ignore the overflow and truncated the clock to
|
|
|
|
_PyTime_MIN or _PyTime_MAX.
|
|
|
|
|
|
|
|
Use _PyTime_GetSystemClockWithInfo() to check for failure. */
|
|
|
|
PyAPI_FUNC(_PyTime_t) _PyTime_GetSystemClock(void);
|
|
|
|
|
|
|
|
/* Get the current time from the system clock.
|
|
|
|
* On success, set *t and *info (if not NULL), and return 0.
|
|
|
|
* On error, raise an exception and return -1.
|
2015-03-29 19:25:38 -03:00
|
|
|
*/
|
|
|
|
PyAPI_FUNC(int) _PyTime_GetSystemClockWithInfo(
|
|
|
|
_PyTime_t *t,
|
|
|
|
_Py_clock_info_t *info);
|
|
|
|
|
2020-11-16 11:08:05 -04:00
|
|
|
/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
|
|
|
|
The clock is not affected by system clock updates. The reference point of
|
|
|
|
the returned value is undefined, so that only the difference between the
|
|
|
|
results of consecutive calls is valid.
|
|
|
|
|
|
|
|
If the internal clock fails, silently ignore the error and return 0.
|
|
|
|
On integer overflow, silently ignore the overflow and truncated the clock to
|
|
|
|
_PyTime_MIN or _PyTime_MAX.
|
|
|
|
|
|
|
|
Use _PyTime_GetMonotonicClockWithInfo() to check for failure. */
|
|
|
|
PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void);
|
|
|
|
|
2015-03-27 18:27:24 -03:00
|
|
|
/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
|
|
|
|
The clock is not affected by system clock updates. The reference point of
|
|
|
|
the returned value is undefined, so that only the difference between the
|
|
|
|
results of consecutive calls is valid.
|
|
|
|
|
|
|
|
Fill info (if set) with information of the function used to get the time.
|
|
|
|
|
|
|
|
Return 0 on success, raise an exception and return -1 on error. */
|
|
|
|
PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo(
|
|
|
|
_PyTime_t *t,
|
|
|
|
_Py_clock_info_t *info);
|
|
|
|
|
2015-03-27 09:31:18 -03:00
|
|
|
|
2016-09-28 18:31:35 -03:00
|
|
|
/* Converts a timestamp to the Gregorian time, using the local time zone.
|
|
|
|
Return 0 on success, raise an exception and return -1 on error. */
|
|
|
|
PyAPI_FUNC(int) _PyTime_localtime(time_t t, struct tm *tm);
|
|
|
|
|
|
|
|
/* Converts a timestamp to the Gregorian time, assuming UTC.
|
|
|
|
Return 0 on success, raise an exception and return -1 on error. */
|
|
|
|
PyAPI_FUNC(int) _PyTime_gmtime(time_t t, struct tm *tm);
|
|
|
|
|
2017-10-12 12:51:56 -03:00
|
|
|
/* Get the performance counter: clock with the highest available resolution to
|
|
|
|
measure a short duration.
|
|
|
|
|
2020-11-16 11:08:05 -04:00
|
|
|
If the internal clock fails, silently ignore the error and return 0.
|
|
|
|
On integer overflow, silently ignore the overflow and truncated the clock to
|
|
|
|
_PyTime_MIN or _PyTime_MAX.
|
|
|
|
|
|
|
|
Use _PyTime_GetPerfCounterWithInfo() to check for failure. */
|
2017-10-16 12:44:31 -03:00
|
|
|
PyAPI_FUNC(_PyTime_t) _PyTime_GetPerfCounter(void);
|
2017-10-10 06:51:50 -03:00
|
|
|
|
|
|
|
/* Get the performance counter: clock with the highest available resolution to
|
2017-10-12 12:51:56 -03:00
|
|
|
measure a short duration.
|
2017-10-10 06:51:50 -03:00
|
|
|
|
2017-10-12 12:51:56 -03:00
|
|
|
Fill info (if set) with information of the function used to get the time.
|
|
|
|
|
|
|
|
Return 0 on success, raise an exception and return -1 on error. */
|
2017-10-16 12:44:31 -03:00
|
|
|
PyAPI_FUNC(int) _PyTime_GetPerfCounterWithInfo(
|
|
|
|
_PyTime_t *t,
|
2017-10-10 06:51:50 -03:00
|
|
|
_Py_clock_info_t *info);
|
|
|
|
|
2010-08-05 14:34:27 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* Py_PYTIME_H */
|
2010-12-03 16:14:31 -04:00
|
|
|
#endif /* Py_LIMITED_API */
|