Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Victor Stinner | 09225b7 | 2012-02-07 23:41:01 +0100 | [diff] [blame] | 2 | #ifdef MS_WINDOWS |
| 3 | #include <windows.h> |
| 4 | #endif |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 5 | |
Victor Stinner | 09225b7 | 2012-02-07 23:41:01 +0100 | [diff] [blame] | 6 | #if defined(__APPLE__) && defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME) |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 7 | /* |
| 8 | * _PyTime_gettimeofday falls back to ftime when getttimeofday fails because the latter |
| 9 | * might fail on some platforms. This fallback is unwanted on MacOSX because |
| 10 | * that makes it impossible to use a binary build on OSX 10.4 on earlier |
| 11 | * releases of the OS. Therefore claim we don't support ftime. |
| 12 | */ |
| 13 | # undef HAVE_FTIME |
| 14 | #endif |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 15 | |
Victor Stinner | 09225b7 | 2012-02-07 23:41:01 +0100 | [diff] [blame] | 16 | #if defined(HAVE_FTIME) && !defined(MS_WINDOWS) |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 17 | #include <sys/timeb.h> |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 18 | extern int ftime(struct timeb *); |
Victor Stinner | 09225b7 | 2012-02-07 23:41:01 +0100 | [diff] [blame] | 19 | #endif |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 20 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 21 | static void |
| 22 | pygettimeofday(_PyTime_timeval *tp, _Py_clock_info_t *info) |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 23 | { |
Victor Stinner | 09225b7 | 2012-02-07 23:41:01 +0100 | [diff] [blame] | 24 | #ifdef MS_WINDOWS |
| 25 | FILETIME system_time; |
| 26 | ULARGE_INTEGER large; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 27 | ULONGLONG microseconds; |
Victor Stinner | 09225b7 | 2012-02-07 23:41:01 +0100 | [diff] [blame] | 28 | |
| 29 | GetSystemTimeAsFileTime(&system_time); |
| 30 | large.u.LowPart = system_time.dwLowDateTime; |
| 31 | large.u.HighPart = system_time.dwHighDateTime; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 32 | /* 11,644,473,600,000,000: number of microseconds between |
Victor Stinner | 09225b7 | 2012-02-07 23:41:01 +0100 | [diff] [blame] | 33 | the 1st january 1601 and the 1st january 1970 (369 years + 89 leap |
| 34 | days). */ |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 35 | microseconds = large.QuadPart / 10 - 11644473600000000; |
| 36 | tp->tv_sec = microseconds / 1000000; |
| 37 | tp->tv_usec = microseconds % 1000000; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 38 | if (info) { |
| 39 | DWORD timeAdjustment, timeIncrement; |
| 40 | BOOL isTimeAdjustmentDisabled; |
| 41 | |
| 42 | info->implementation = "GetSystemTimeAsFileTime()"; |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 43 | info->monotonic = 0; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 44 | (void) GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement, |
| 45 | &isTimeAdjustmentDisabled); |
| 46 | info->resolution = timeIncrement * 1e-7; |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 47 | info->adjustable = 1; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 48 | } |
Victor Stinner | 09225b7 | 2012-02-07 23:41:01 +0100 | [diff] [blame] | 49 | #else |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 50 | /* There are three ways to get the time: |
| 51 | (1) gettimeofday() -- resolution in microseconds |
| 52 | (2) ftime() -- resolution in milliseconds |
| 53 | (3) time() -- resolution in seconds |
| 54 | In all cases the return value in a timeval struct. |
| 55 | Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may |
| 56 | fail, so we fall back on ftime() or time(). |
| 57 | Note: clock resolution does not imply clock accuracy! */ |
Victor Stinner | 09225b7 | 2012-02-07 23:41:01 +0100 | [diff] [blame] | 58 | |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 59 | #ifdef HAVE_GETTIMEOFDAY |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 60 | int err; |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 61 | #ifdef GETTIMEOFDAY_NO_TZ |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 62 | err = gettimeofday(tp); |
| 63 | #else |
| 64 | err = gettimeofday(tp, (struct timezone *)NULL); |
| 65 | #endif |
| 66 | if (err == 0) { |
| 67 | if (info) { |
| 68 | info->implementation = "gettimeofday()"; |
| 69 | info->resolution = 1e-6; |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 70 | info->monotonic = 0; |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 71 | info->adjustable = 1; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 72 | } |
Victor Stinner | ccd5715 | 2012-02-08 14:31:50 +0100 | [diff] [blame] | 73 | return; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 74 | } |
| 75 | #endif /* HAVE_GETTIMEOFDAY */ |
Victor Stinner | 09225b7 | 2012-02-07 23:41:01 +0100 | [diff] [blame] | 76 | |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 77 | #if defined(HAVE_FTIME) |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 78 | { |
| 79 | struct timeb t; |
| 80 | ftime(&t); |
| 81 | tp->tv_sec = t.time; |
| 82 | tp->tv_usec = t.millitm * 1000; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 83 | if (info) { |
| 84 | info->implementation = "ftime()"; |
| 85 | info->resolution = 1e-3; |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 86 | info->monotonic = 0; |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 87 | info->adjustable = 1; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 88 | } |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 89 | } |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 90 | #else /* !HAVE_FTIME */ |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 91 | tp->tv_sec = time(NULL); |
| 92 | tp->tv_usec = 0; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 93 | if (info) { |
| 94 | info->implementation = "time()"; |
| 95 | info->resolution = 1.0; |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 96 | info->monotonic = 0; |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 97 | info->adjustable = 1; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 98 | } |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 99 | #endif /* !HAVE_FTIME */ |
Victor Stinner | 09225b7 | 2012-02-07 23:41:01 +0100 | [diff] [blame] | 100 | |
| 101 | #endif /* MS_WINDOWS */ |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 104 | void |
| 105 | _PyTime_gettimeofday(_PyTime_timeval *tp) |
| 106 | { |
| 107 | pygettimeofday(tp, NULL); |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | _PyTime_gettimeofday_info(_PyTime_timeval *tp, _Py_clock_info_t *info) |
| 112 | { |
| 113 | pygettimeofday(tp, info); |
| 114 | } |
| 115 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 116 | static void |
| 117 | error_time_t_overflow(void) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 118 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 119 | PyErr_SetString(PyExc_OverflowError, |
| 120 | "timestamp out of range for platform time_t"); |
| 121 | } |
| 122 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 123 | time_t |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 124 | _PyLong_AsTime_t(PyObject *obj) |
| 125 | { |
| 126 | #if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG |
| 127 | PY_LONG_LONG val; |
| 128 | val = PyLong_AsLongLong(obj); |
| 129 | #else |
| 130 | long val; |
| 131 | assert(sizeof(time_t) <= sizeof(long)); |
| 132 | val = PyLong_AsLong(obj); |
| 133 | #endif |
| 134 | if (val == -1 && PyErr_Occurred()) { |
| 135 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 136 | error_time_t_overflow(); |
| 137 | return -1; |
| 138 | } |
| 139 | return (time_t)val; |
| 140 | } |
| 141 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 142 | PyObject * |
| 143 | _PyLong_FromTime_t(time_t t) |
| 144 | { |
| 145 | #if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG |
| 146 | return PyLong_FromLongLong((PY_LONG_LONG)t); |
| 147 | #else |
| 148 | assert(sizeof(time_t) <= sizeof(long)); |
| 149 | return PyLong_FromLong((long)t); |
| 150 | #endif |
| 151 | } |
| 152 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 153 | static int |
| 154 | _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator, |
| 155 | double denominator) |
| 156 | { |
| 157 | assert(denominator <= LONG_MAX); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 158 | if (PyFloat_Check(obj)) { |
Victor Stinner | bd273c1 | 2012-03-13 19:12:23 +0100 | [diff] [blame] | 159 | double d, intpart, err; |
| 160 | /* volatile avoids unsafe optimization on float enabled by gcc -O3 */ |
| 161 | volatile double floatpart; |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 162 | |
| 163 | d = PyFloat_AsDouble(obj); |
| 164 | floatpart = modf(d, &intpart); |
| 165 | if (floatpart < 0) { |
| 166 | floatpart = 1.0 + floatpart; |
| 167 | intpart -= 1.0; |
| 168 | } |
| 169 | |
| 170 | *sec = (time_t)intpart; |
| 171 | err = intpart - (double)*sec; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 172 | if (err <= -1.0 || err >= 1.0) { |
| 173 | error_time_t_overflow(); |
| 174 | return -1; |
| 175 | } |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 176 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 177 | floatpart *= denominator; |
| 178 | *numerator = (long)floatpart; |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | else { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 182 | *sec = _PyLong_AsTime_t(obj); |
| 183 | if (*sec == (time_t)-1 && PyErr_Occurred()) |
| 184 | return -1; |
| 185 | *numerator = 0; |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 186 | return 0; |
| 187 | } |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 188 | } |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 189 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 190 | int |
| 191 | _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec) |
| 192 | { |
| 193 | if (PyFloat_Check(obj)) { |
| 194 | double d, intpart, err; |
| 195 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 196 | d = PyFloat_AsDouble(obj); |
| 197 | (void)modf(d, &intpart); |
| 198 | |
| 199 | *sec = (time_t)intpart; |
| 200 | err = intpart - (double)*sec; |
| 201 | if (err <= -1.0 || err >= 1.0) { |
| 202 | error_time_t_overflow(); |
| 203 | return -1; |
| 204 | } |
| 205 | return 0; |
| 206 | } |
| 207 | else { |
| 208 | *sec = _PyLong_AsTime_t(obj); |
| 209 | if (*sec == (time_t)-1 && PyErr_Occurred()) |
| 210 | return -1; |
| 211 | return 0; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | int |
| 216 | _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec) |
| 217 | { |
| 218 | return _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9); |
| 219 | } |
| 220 | |
| 221 | int |
| 222 | _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec) |
| 223 | { |
| 224 | return _PyTime_ObjectToDenominator(obj, sec, usec, 1e6); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 227 | void |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 228 | _PyTime_Init() |
| 229 | { |
| 230 | /* Do nothing. Needed to force linking. */ |
| 231 | } |