blob: 52902f5e3ac254116dc4e550f008210ef72dec3d [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 Stinner5d272cc2012-03-13 13:35:55 +010056/* Convert a number of seconds, int or float, to time_t. */
57PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
58 PyObject *obj,
59 time_t *sec);
60
Larry Hastings6fe20b32012-04-19 15:07:49 -070061/* Convert a time_t to a PyLong. */
62PyAPI_FUNC(PyObject *) _PyLong_FromTime_t(
63 time_t sec);
64
Larry Hastings76ad59b2012-05-03 00:30:07 -070065/* Convert a PyLong to a time_t. */
66PyAPI_FUNC(time_t) _PyLong_AsTime_t(
67 PyObject *obj);
68
Victor Stinner5d272cc2012-03-13 13:35:55 +010069/* Convert a number of seconds, int or float, to a timeval structure.
70 usec is in the range [0; 999999] and rounded towards zero.
71 For example, -1.2 is converted to (-2, 800000). */
72PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
73 PyObject *obj,
74 time_t *sec,
75 long *usec);
76
Victor Stinner643cd682012-03-02 22:54:03 +010077/* Convert a number of seconds, int or float, to a timespec structure.
Victor Stinner5d272cc2012-03-13 13:35:55 +010078 nsec is in the range [0; 999999999] and rounded towards zero.
79 For example, -1.2 is converted to (-2, 800000000). */
Victor Stinner643cd682012-03-02 22:54:03 +010080PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
81 PyObject *obj,
82 time_t *sec,
83 long *nsec);
84#endif
85
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000086/* Dummy to force linking. */
87PyAPI_FUNC(void) _PyTime_Init(void);
88
89#ifdef __cplusplus
90}
91#endif
92
93#endif /* Py_PYTIME_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000094#endif /* Py_LIMITED_API */