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 | ccd5715 | 2012-02-08 14:31:50 +0100 | [diff] [blame^] | 5 | #include "pyport.h" |
| 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 | ccd5715 | 2012-02-08 14:31:50 +0100 | [diff] [blame^] | 41 | #if defined(HAVE_LONG_LONG) |
| 42 | typedef unsigned PY_LONG_LONG _PyTime_fraction_t; |
| 43 | #else |
| 44 | typedef size_t _PyTime_fraction_t; |
| 45 | #endif |
| 46 | |
| 47 | typedef struct |
| 48 | { |
| 49 | /* timestamp = seconds + numerator / denominator */ |
| 50 | time_t seconds; |
| 51 | _PyTime_fraction_t numerator; |
| 52 | /* denominator cannot be zero */ |
| 53 | _PyTime_fraction_t denominator; |
| 54 | /* the timestamp resolution is 1/divisor */ |
| 55 | } _PyTime_t; |
| 56 | |
| 57 | /* Similar to POSIX gettimeofday. If system gettimeofday |
| 58 | fails or is not available, fall back to lower resolution clocks. */ |
| 59 | PyAPI_FUNC(void) _PyTime_get(_PyTime_t *tp); |
| 60 | |
| 61 | /* Convert a timestamp structure to the specified timestamp type. |
| 62 | |
| 63 | Raise a ValueError if the timestamp type is unknown. */ |
| 64 | PyAPI_FUNC(PyObject*) _PyTime_Convert(_PyTime_t *ts, PyObject *timestamp); |
| 65 | |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 66 | /* Dummy to force linking. */ |
| 67 | PyAPI_FUNC(void) _PyTime_Init(void); |
| 68 | |
| 69 | #ifdef __cplusplus |
| 70 | } |
| 71 | #endif |
| 72 | |
| 73 | #endif /* Py_PYTIME_H */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 74 | #endif /* Py_LIMITED_API */ |