Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1 | #ifndef Py_LIMITED_API |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 2 | #ifndef Py_PYTIME_H |
| 3 | #define Py_PYTIME_H |
| 4 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 5 | #include "pyconfig.h" /* include for defines */ |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 6 | #include "object.h" |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 7 | |
| 8 | /************************************************************************** |
| 9 | Symbols and macros to supply platform-independent interfaces to time related |
| 10 | functions and constants |
| 11 | **************************************************************************/ |
| 12 | #ifdef __cplusplus |
| 13 | extern "C" { |
| 14 | #endif |
| 15 | |
| 16 | #ifdef HAVE_GETTIMEOFDAY |
| 17 | typedef struct timeval _PyTime_timeval; |
| 18 | #else |
| 19 | typedef struct { |
| 20 | time_t tv_sec; /* seconds since Jan. 1, 1970 */ |
| 21 | long tv_usec; /* and microseconds */ |
| 22 | } _PyTime_timeval; |
| 23 | #endif |
| 24 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 25 | /* Structure used by time.get_clock_info() */ |
| 26 | typedef struct { |
| 27 | const char *implementation; |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 28 | int monotonic; |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 29 | int adjustable; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 30 | double resolution; |
| 31 | } _Py_clock_info_t; |
| 32 | |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 33 | /* Similar to POSIX gettimeofday but cannot fail. If system gettimeofday |
| 34 | * fails or is not available, fall back to lower resolution clocks. |
| 35 | */ |
| 36 | PyAPI_FUNC(void) _PyTime_gettimeofday(_PyTime_timeval *tp); |
| 37 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 38 | /* Similar to _PyTime_gettimeofday() but retrieve also information on the |
| 39 | * clock used to get the current time. */ |
| 40 | PyAPI_FUNC(void) _PyTime_gettimeofday_info( |
| 41 | _PyTime_timeval *tp, |
| 42 | _Py_clock_info_t *info); |
| 43 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 44 | #define _PyTime_ADD_SECONDS(tv, interval) \ |
| 45 | do { \ |
| 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 Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 55 | #ifndef Py_LIMITED_API |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 56 | /* Convert a number of seconds, int or float, to time_t. */ |
| 57 | PyAPI_FUNC(int) _PyTime_ObjectToTime_t( |
| 58 | PyObject *obj, |
| 59 | time_t *sec); |
| 60 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 61 | /* Convert a time_t to a PyLong. */ |
| 62 | PyAPI_FUNC(PyObject *) _PyLong_FromTime_t( |
| 63 | time_t sec); |
| 64 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 65 | /* Convert a PyLong to a time_t. */ |
| 66 | PyAPI_FUNC(time_t) _PyLong_AsTime_t( |
| 67 | PyObject *obj); |
| 68 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 69 | /* 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). */ |
| 72 | PyAPI_FUNC(int) _PyTime_ObjectToTimeval( |
| 73 | PyObject *obj, |
| 74 | time_t *sec, |
| 75 | long *usec); |
| 76 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 77 | /* Convert a number of seconds, int or float, to a timespec structure. |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 78 | nsec is in the range [0; 999999999] and rounded towards zero. |
| 79 | For example, -1.2 is converted to (-2, 800000000). */ |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 80 | PyAPI_FUNC(int) _PyTime_ObjectToTimespec( |
| 81 | PyObject *obj, |
| 82 | time_t *sec, |
| 83 | long *nsec); |
| 84 | #endif |
| 85 | |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 86 | /* Dummy to force linking. */ |
| 87 | PyAPI_FUNC(void) _PyTime_Init(void); |
| 88 | |
| 89 | #ifdef __cplusplus |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | #endif /* Py_PYTIME_H */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 94 | #endif /* Py_LIMITED_API */ |