blob: 7a14456d3f5958194f2151feabfd2c1ea8dfd2d7 [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
16#ifdef HAVE_GETTIMEOFDAY
17typedef struct timeval _PyTime_timeval;
18#else
19typedef struct {
20 time_t tv_sec; /* seconds since Jan. 1, 1970 */
21 long tv_usec; /* and microseconds */
22} _PyTime_timeval;
23#endif
24
Victor Stinnerec895392012-04-29 02:41:27 +020025/* Structure used by time.get_clock_info() */
26typedef struct {
27 const char *implementation;
Benjamin Peterson49a69e42012-05-01 09:38:34 -040028 int monotonic;
Victor Stinner2b89fdf2012-06-12 22:46:37 +020029 int adjustable;
Victor Stinnerec895392012-04-29 02:41:27 +020030 double resolution;
31} _Py_clock_info_t;
32
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000033/* Similar to POSIX gettimeofday but cannot fail. If system gettimeofday
34 * fails or is not available, fall back to lower resolution clocks.
35 */
36PyAPI_FUNC(void) _PyTime_gettimeofday(_PyTime_timeval *tp);
37
Victor Stinnerec895392012-04-29 02:41:27 +020038/* Similar to _PyTime_gettimeofday() but retrieve also information on the
39 * clock used to get the current time. */
Victor Stinner00111242014-08-29 16:31:59 +020040PyAPI_FUNC(int) _PyTime_gettimeofday_info(
Victor Stinnerec895392012-04-29 02:41:27 +020041 _PyTime_timeval *tp,
42 _Py_clock_info_t *info);
43
Antoine Pitrou3e1fd272010-09-28 21:23:11 +000044#define _PyTime_ADD_SECONDS(tv, interval) \
45do { \
46 tv.tv_usec += (long) (((long) interval - interval) * 1000000); \
47 tv.tv_sec += (time_t) interval + (time_t) (tv.tv_usec / 1000000); \
48 tv.tv_usec %= 1000000; \
49} while (0)
50
51#define _PyTime_INTERVAL(tv_start, tv_end) \
52 ((tv_end.tv_sec - tv_start.tv_sec) + \
53 (tv_end.tv_usec - tv_start.tv_usec) * 0.000001)
54
Victor Stinner3c1b3792014-02-17 00:02:43 +010055typedef enum {
56 /* Round towards zero. */
57 _PyTime_ROUND_DOWN=0,
58 /* Round away from zero. */
59 _PyTime_ROUND_UP
60} _PyTime_round_t;
61
Victor Stinner5d272cc2012-03-13 13:35:55 +010062/* Convert a number of seconds, int or float, to time_t. */
63PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
64 PyObject *obj,
Victor Stinner3c1b3792014-02-17 00:02:43 +010065 time_t *sec,
66 _PyTime_round_t);
Victor Stinner5d272cc2012-03-13 13:35:55 +010067
Larry Hastings6fe20b32012-04-19 15:07:49 -070068/* Convert a time_t to a PyLong. */
69PyAPI_FUNC(PyObject *) _PyLong_FromTime_t(
70 time_t sec);
71
Larry Hastings76ad59b2012-05-03 00:30:07 -070072/* Convert a PyLong to a time_t. */
73PyAPI_FUNC(time_t) _PyLong_AsTime_t(
74 PyObject *obj);
75
Victor Stinner5d272cc2012-03-13 13:35:55 +010076/* Convert a number of seconds, int or float, to a timeval structure.
77 usec is in the range [0; 999999] and rounded towards zero.
78 For example, -1.2 is converted to (-2, 800000). */
79PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
80 PyObject *obj,
81 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010082 long *usec,
83 _PyTime_round_t);
Victor Stinner5d272cc2012-03-13 13:35:55 +010084
Victor Stinner643cd682012-03-02 22:54:03 +010085/* Convert a number of seconds, int or float, to a timespec structure.
Victor Stinner5d272cc2012-03-13 13:35:55 +010086 nsec is in the range [0; 999999999] and rounded towards zero.
87 For example, -1.2 is converted to (-2, 800000000). */
Victor Stinner643cd682012-03-02 22:54:03 +010088PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
89 PyObject *obj,
90 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010091 long *nsec,
92 _PyTime_round_t);
Victor Stinner643cd682012-03-02 22:54:03 +010093
Victor Stinnerae586492014-09-02 23:18:25 +020094/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
95 The clock is not affected by system clock updates. The reference point of
96 the returned value is undefined, so that only the difference between the
97 results of consecutive calls is valid.
98
99 The function never fails. _PyTime_Init() ensures that a monotonic clock
100 is available and works. */
101PyAPI_FUNC(void) _PyTime_monotonic(
102 _PyTime_timeval *tp);
103
104/* Similar to _PyTime_monotonic(), fill also info (if set) with information of
105 the function used to get the time.
106
107 Return 0 on success, raise an exception and return -1 on error. */
108PyAPI_FUNC(int) _PyTime_monotonic_info(
109 _PyTime_timeval *tp,
110 _Py_clock_info_t *info);
111
Victor Stinner00111242014-08-29 16:31:59 +0200112/* Initialize time.
113 Return 0 on success, raise an exception and return -1 on error. */
114PyAPI_FUNC(int) _PyTime_Init(void);
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000115
116#ifdef __cplusplus
117}
118#endif
119
120#endif /* Py_PYTIME_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000121#endif /* Py_LIMITED_API */