blob: b0fc6d0354377deca062a6667561a6cd6a3eeb95 [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. */
40PyAPI_FUNC(void) _PyTime_gettimeofday_info(
41 _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 Stinner643cd682012-03-02 22:54:03 +010055#ifndef Py_LIMITED_API
Victor Stinner3c1b3792014-02-17 00:02:43 +010056
57typedef enum {
58 /* Round towards zero. */
59 _PyTime_ROUND_DOWN=0,
60 /* Round away from zero. */
61 _PyTime_ROUND_UP
62} _PyTime_round_t;
63
Victor Stinner5d272cc2012-03-13 13:35:55 +010064/* Convert a number of seconds, int or float, to time_t. */
65PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
66 PyObject *obj,
Victor Stinner3c1b3792014-02-17 00:02:43 +010067 time_t *sec,
68 _PyTime_round_t);
Victor Stinner5d272cc2012-03-13 13:35:55 +010069
Larry Hastings6fe20b32012-04-19 15:07:49 -070070/* Convert a time_t to a PyLong. */
71PyAPI_FUNC(PyObject *) _PyLong_FromTime_t(
72 time_t sec);
73
Larry Hastings76ad59b2012-05-03 00:30:07 -070074/* Convert a PyLong to a time_t. */
75PyAPI_FUNC(time_t) _PyLong_AsTime_t(
76 PyObject *obj);
77
Victor Stinner5d272cc2012-03-13 13:35:55 +010078/* Convert a number of seconds, int or float, to a timeval structure.
79 usec is in the range [0; 999999] and rounded towards zero.
80 For example, -1.2 is converted to (-2, 800000). */
81PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
82 PyObject *obj,
83 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010084 long *usec,
85 _PyTime_round_t);
Victor Stinner5d272cc2012-03-13 13:35:55 +010086
Victor Stinner643cd682012-03-02 22:54:03 +010087/* Convert a number of seconds, int or float, to a timespec structure.
Victor Stinner5d272cc2012-03-13 13:35:55 +010088 nsec is in the range [0; 999999999] and rounded towards zero.
89 For example, -1.2 is converted to (-2, 800000000). */
Victor Stinner643cd682012-03-02 22:54:03 +010090PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
91 PyObject *obj,
92 time_t *sec,
Victor Stinner3c1b3792014-02-17 00:02:43 +010093 long *nsec,
94 _PyTime_round_t);
Victor Stinner643cd682012-03-02 22:54:03 +010095#endif
96
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000097/* Dummy to force linking. */
98PyAPI_FUNC(void) _PyTime_Init(void);
99
100#ifdef __cplusplus
101}
102#endif
103
104#endif /* Py_PYTIME_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000105#endif /* Py_LIMITED_API */