blob: a0cedb20b2a633afbb21e41f0834a21a93038391 [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;
28 int is_monotonic;
29 int is_adjusted;
30 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
Victor Stinner5d272cc2012-03-13 13:35:55 +010065/* Convert a number of seconds, int or float, to a timeval structure.
66 usec is in the range [0; 999999] and rounded towards zero.
67 For example, -1.2 is converted to (-2, 800000). */
68PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
69 PyObject *obj,
70 time_t *sec,
71 long *usec);
72
Victor Stinner643cd682012-03-02 22:54:03 +010073/* Convert a number of seconds, int or float, to a timespec structure.
Victor Stinner5d272cc2012-03-13 13:35:55 +010074 nsec is in the range [0; 999999999] and rounded towards zero.
75 For example, -1.2 is converted to (-2, 800000000). */
Victor Stinner643cd682012-03-02 22:54:03 +010076PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
77 PyObject *obj,
78 time_t *sec,
79 long *nsec);
80#endif
81
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000082/* Dummy to force linking. */
83PyAPI_FUNC(void) _PyTime_Init(void);
84
85#ifdef __cplusplus
86}
87#endif
88
89#endif /* Py_PYTIME_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000090#endif /* Py_LIMITED_API */