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 | |
| 25 | /* Similar to POSIX gettimeofday but cannot fail. If system gettimeofday |
| 26 | * fails or is not available, fall back to lower resolution clocks. |
| 27 | */ |
| 28 | PyAPI_FUNC(void) _PyTime_gettimeofday(_PyTime_timeval *tp); |
| 29 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 30 | #define _PyTime_ADD_SECONDS(tv, interval) \ |
| 31 | do { \ |
| 32 | tv.tv_usec += (long) (((long) interval - interval) * 1000000); \ |
| 33 | tv.tv_sec += (time_t) interval + (time_t) (tv.tv_usec / 1000000); \ |
| 34 | tv.tv_usec %= 1000000; \ |
| 35 | } while (0) |
| 36 | |
| 37 | #define _PyTime_INTERVAL(tv_start, tv_end) \ |
| 38 | ((tv_end.tv_sec - tv_start.tv_sec) + \ |
| 39 | (tv_end.tv_usec - tv_start.tv_usec) * 0.000001) |
| 40 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 41 | #ifndef Py_LIMITED_API |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 42 | /* Convert a number of seconds, int or float, to time_t. */ |
| 43 | PyAPI_FUNC(int) _PyTime_ObjectToTime_t( |
| 44 | PyObject *obj, |
| 45 | time_t *sec); |
| 46 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame^] | 47 | /* Convert a time_t to a PyLong. */ |
| 48 | PyAPI_FUNC(PyObject *) _PyLong_FromTime_t( |
| 49 | time_t sec); |
| 50 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 51 | /* Convert a number of seconds, int or float, to a timeval structure. |
| 52 | usec is in the range [0; 999999] and rounded towards zero. |
| 53 | For example, -1.2 is converted to (-2, 800000). */ |
| 54 | PyAPI_FUNC(int) _PyTime_ObjectToTimeval( |
| 55 | PyObject *obj, |
| 56 | time_t *sec, |
| 57 | long *usec); |
| 58 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 59 | /* Convert a number of seconds, int or float, to a timespec structure. |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 60 | nsec is in the range [0; 999999999] and rounded towards zero. |
| 61 | For example, -1.2 is converted to (-2, 800000000). */ |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 62 | PyAPI_FUNC(int) _PyTime_ObjectToTimespec( |
| 63 | PyObject *obj, |
| 64 | time_t *sec, |
| 65 | long *nsec); |
| 66 | #endif |
| 67 | |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 68 | /* Dummy to force linking. */ |
| 69 | PyAPI_FUNC(void) _PyTime_Init(void); |
| 70 | |
| 71 | #ifdef __cplusplus |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | #endif /* Py_PYTIME_H */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 76 | #endif /* Py_LIMITED_API */ |