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 | ae58649 | 2014-09-02 23:18:25 +0200 | [diff] [blame] | 6 | #if defined(__APPLE__) |
| 7 | #include <mach/mach_time.h> /* mach_absolute_time(), mach_timebase_info() */ |
| 8 | #endif |
| 9 | |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 10 | #define _PyTime_check_mul_overflow(a, b) \ |
| 11 | (assert(b > 0), \ |
| 12 | (_PyTime_t)(a) < _PyTime_MIN / (_PyTime_t)(b) \ |
| 13 | || _PyTime_MAX / (_PyTime_t)(b) < (_PyTime_t)(a)) |
| 14 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 15 | /* To millisecond (10^-3) */ |
Victor Stinner | 580ef13 | 2015-03-20 01:55:04 +0100 | [diff] [blame] | 16 | #define SEC_TO_MS 1000 |
Victor Stinner | 580ef13 | 2015-03-20 01:55:04 +0100 | [diff] [blame] | 17 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 18 | /* To microseconds (10^-6) */ |
| 19 | #define MS_TO_US 1000 |
Victor Stinner | 580ef13 | 2015-03-20 01:55:04 +0100 | [diff] [blame] | 20 | #define SEC_TO_US (SEC_TO_MS * MS_TO_US) |
| 21 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 22 | /* To nanoseconds (10^-9) */ |
| 23 | #define US_TO_NS 1000 |
| 24 | #define MS_TO_NS (MS_TO_US * US_TO_NS) |
| 25 | #define SEC_TO_NS (SEC_TO_MS * MS_TO_NS) |
| 26 | |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 27 | /* Conversion from nanoseconds */ |
| 28 | #define NS_TO_MS (1000 * 1000) |
| 29 | #define NS_TO_US (1000) |
| 30 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 31 | static void |
| 32 | error_time_t_overflow(void) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 33 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 34 | PyErr_SetString(PyExc_OverflowError, |
| 35 | "timestamp out of range for platform time_t"); |
| 36 | } |
| 37 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 38 | time_t |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 39 | _PyLong_AsTime_t(PyObject *obj) |
| 40 | { |
| 41 | #if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG |
| 42 | PY_LONG_LONG val; |
| 43 | val = PyLong_AsLongLong(obj); |
| 44 | #else |
| 45 | long val; |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 46 | Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long)); |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 47 | val = PyLong_AsLong(obj); |
| 48 | #endif |
| 49 | if (val == -1 && PyErr_Occurred()) { |
| 50 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 51 | error_time_t_overflow(); |
| 52 | return -1; |
| 53 | } |
| 54 | return (time_t)val; |
| 55 | } |
| 56 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 57 | PyObject * |
| 58 | _PyLong_FromTime_t(time_t t) |
| 59 | { |
| 60 | #if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG |
| 61 | return PyLong_FromLongLong((PY_LONG_LONG)t); |
| 62 | #else |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 63 | Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long)); |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 64 | return PyLong_FromLong((long)t); |
| 65 | #endif |
| 66 | } |
| 67 | |
Victor Stinner | ce6aa74 | 2015-09-09 22:28:09 +0200 | [diff] [blame] | 68 | /* Round to nearest with ties going to nearest even integer |
| 69 | (_PyTime_ROUND_HALF_EVEN) */ |
| 70 | static double |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 71 | _PyTime_RoundHalfEven(double x) |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 72 | { |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 73 | double rounded = round(x); |
| 74 | if (fabs(x-rounded) == 0.5) |
| 75 | /* halfway case: round to even */ |
| 76 | rounded = 2.0*round(x/2.0); |
| 77 | return rounded; |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 80 | static double |
| 81 | _PyTime_Round(double x, _PyTime_round_t round) |
| 82 | { |
Victor Stinner | 1efbeba | 2015-09-10 11:48:00 +0200 | [diff] [blame] | 83 | /* volatile avoids optimization changing how numbers are rounded */ |
| 84 | volatile double d; |
| 85 | |
| 86 | d = x; |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 87 | if (round == _PyTime_ROUND_HALF_EVEN) |
Victor Stinner | 1efbeba | 2015-09-10 11:48:00 +0200 | [diff] [blame] | 88 | d = _PyTime_RoundHalfEven(d); |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 89 | else if (round == _PyTime_ROUND_CEILING) |
Victor Stinner | 1efbeba | 2015-09-10 11:48:00 +0200 | [diff] [blame] | 90 | d = ceil(d); |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 91 | else |
Victor Stinner | 1efbeba | 2015-09-10 11:48:00 +0200 | [diff] [blame] | 92 | d = floor(d); |
| 93 | return d; |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 96 | static int |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 97 | _PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator, |
| 98 | double denominator, _PyTime_round_t round) |
| 99 | { |
| 100 | double intpart, err; |
Victor Stinner | 24b822e | 2015-09-02 11:58:56 +0200 | [diff] [blame] | 101 | /* volatile avoids optimization changing how numbers are rounded */ |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 102 | volatile double floatpart; |
| 103 | |
| 104 | floatpart = modf(d, &intpart); |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 105 | |
| 106 | floatpart *= denominator; |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 107 | floatpart = _PyTime_Round(floatpart, round); |
Victor Stinner | 67edcc9 | 2015-09-02 10:37:46 +0200 | [diff] [blame] | 108 | if (floatpart >= denominator) { |
| 109 | floatpart -= denominator; |
| 110 | intpart += 1.0; |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 111 | } |
Victor Stinner | adfefa5 | 2015-09-04 23:57:25 +0200 | [diff] [blame] | 112 | else if (floatpart < 0) { |
| 113 | floatpart += denominator; |
| 114 | intpart -= 1.0; |
| 115 | } |
Victor Stinner | 67edcc9 | 2015-09-02 10:37:46 +0200 | [diff] [blame] | 116 | assert(0.0 <= floatpart && floatpart < denominator); |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 117 | |
| 118 | *sec = (time_t)intpart; |
Victor Stinner | 67edcc9 | 2015-09-02 10:37:46 +0200 | [diff] [blame] | 119 | *numerator = (long)floatpart; |
| 120 | |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 121 | err = intpart - (double)*sec; |
| 122 | if (err <= -1.0 || err >= 1.0) { |
| 123 | error_time_t_overflow(); |
| 124 | return -1; |
| 125 | } |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 130 | _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator, |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 131 | double denominator, _PyTime_round_t round) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 132 | { |
Victor Stinner | bbdda21 | 2015-09-02 00:50:43 +0200 | [diff] [blame] | 133 | assert(denominator <= (double)LONG_MAX); |
| 134 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 135 | if (PyFloat_Check(obj)) { |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 136 | double d = PyFloat_AsDouble(obj); |
| 137 | return _PyTime_DoubleToDenominator(d, sec, numerator, |
| 138 | denominator, round); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 139 | } |
| 140 | else { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 141 | *sec = _PyLong_AsTime_t(obj); |
Victor Stinner | 67edcc9 | 2015-09-02 10:37:46 +0200 | [diff] [blame] | 142 | *numerator = 0; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 143 | if (*sec == (time_t)-1 && PyErr_Occurred()) |
| 144 | return -1; |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 145 | return 0; |
| 146 | } |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 147 | } |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 148 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 149 | int |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 150 | _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 151 | { |
| 152 | if (PyFloat_Check(obj)) { |
Victor Stinner | 5786aef | 2015-09-03 16:33:16 +0200 | [diff] [blame] | 153 | double intpart, err; |
Victor Stinner | 24b822e | 2015-09-02 11:58:56 +0200 | [diff] [blame] | 154 | /* volatile avoids optimization changing how numbers are rounded */ |
Victor Stinner | 5786aef | 2015-09-03 16:33:16 +0200 | [diff] [blame] | 155 | volatile double d; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 156 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 157 | d = PyFloat_AsDouble(obj); |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 158 | d = _PyTime_Round(d, round); |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 159 | (void)modf(d, &intpart); |
| 160 | |
| 161 | *sec = (time_t)intpart; |
| 162 | err = intpart - (double)*sec; |
| 163 | if (err <= -1.0 || err >= 1.0) { |
| 164 | error_time_t_overflow(); |
| 165 | return -1; |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | else { |
| 170 | *sec = _PyLong_AsTime_t(obj); |
| 171 | if (*sec == (time_t)-1 && PyErr_Occurred()) |
| 172 | return -1; |
| 173 | return 0; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | int |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 178 | _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec, |
| 179 | _PyTime_round_t round) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 180 | { |
Victor Stinner | bbdda21 | 2015-09-02 00:50:43 +0200 | [diff] [blame] | 181 | int res; |
| 182 | res = _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round); |
Victor Stinner | 67edcc9 | 2015-09-02 10:37:46 +0200 | [diff] [blame] | 183 | assert(0 <= *nsec && *nsec < SEC_TO_NS); |
Victor Stinner | bbdda21 | 2015-09-02 00:50:43 +0200 | [diff] [blame] | 184 | return res; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | int |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 188 | _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec, |
| 189 | _PyTime_round_t round) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 190 | { |
Victor Stinner | bbdda21 | 2015-09-02 00:50:43 +0200 | [diff] [blame] | 191 | int res; |
| 192 | res = _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round); |
Victor Stinner | 67edcc9 | 2015-09-02 10:37:46 +0200 | [diff] [blame] | 193 | assert(0 <= *usec && *usec < SEC_TO_US); |
Victor Stinner | bbdda21 | 2015-09-02 00:50:43 +0200 | [diff] [blame] | 194 | return res; |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 197 | static void |
| 198 | _PyTime_overflow(void) |
| 199 | { |
| 200 | PyErr_SetString(PyExc_OverflowError, |
| 201 | "timestamp too large to convert to C _PyTime_t"); |
| 202 | } |
| 203 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 204 | _PyTime_t |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 205 | _PyTime_FromSeconds(int seconds) |
| 206 | { |
| 207 | _PyTime_t t; |
Victor Stinner | bbdda21 | 2015-09-02 00:50:43 +0200 | [diff] [blame] | 208 | t = (_PyTime_t)seconds; |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 209 | /* ensure that integer overflow cannot happen, int type should have 32 |
| 210 | bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30 |
| 211 | bits). */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 212 | Py_BUILD_ASSERT(INT_MAX <= _PyTime_MAX / SEC_TO_NS); |
| 213 | Py_BUILD_ASSERT(INT_MIN >= _PyTime_MIN / SEC_TO_NS); |
Victor Stinner | bbdda21 | 2015-09-02 00:50:43 +0200 | [diff] [blame] | 214 | assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS) |
| 215 | || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS)); |
| 216 | t *= SEC_TO_NS; |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 217 | return t; |
| 218 | } |
| 219 | |
| 220 | _PyTime_t |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 221 | _PyTime_FromNanoseconds(PY_LONG_LONG ns) |
| 222 | { |
| 223 | _PyTime_t t; |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 224 | Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t)); |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 225 | t = Py_SAFE_DOWNCAST(ns, PY_LONG_LONG, _PyTime_t); |
| 226 | return t; |
| 227 | } |
| 228 | |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 229 | #ifdef HAVE_CLOCK_GETTIME |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 230 | static int |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 231 | _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 232 | { |
| 233 | _PyTime_t t; |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 234 | int res = 0; |
| 235 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 236 | Py_BUILD_ASSERT(sizeof(ts->tv_sec) <= sizeof(_PyTime_t)); |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 237 | t = (_PyTime_t)ts->tv_sec; |
| 238 | |
| 239 | if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) { |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 240 | if (raise) |
| 241 | _PyTime_overflow(); |
| 242 | res = -1; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 243 | } |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 244 | t = t * SEC_TO_NS; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 245 | |
| 246 | t += ts->tv_nsec; |
| 247 | |
| 248 | *tp = t; |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 249 | return res; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 250 | } |
Victor Stinner | 1bd18ba | 2015-03-30 00:25:38 +0200 | [diff] [blame] | 251 | #elif !defined(MS_WINDOWS) |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 252 | static int |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 253 | _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise) |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 254 | { |
| 255 | _PyTime_t t; |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 256 | int res = 0; |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 257 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 258 | Py_BUILD_ASSERT(sizeof(tv->tv_sec) <= sizeof(_PyTime_t)); |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 259 | t = (_PyTime_t)tv->tv_sec; |
| 260 | |
| 261 | if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) { |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 262 | if (raise) |
| 263 | _PyTime_overflow(); |
| 264 | res = -1; |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 265 | } |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 266 | t = t * SEC_TO_NS; |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 267 | |
| 268 | t += (_PyTime_t)tv->tv_usec * US_TO_NS; |
| 269 | |
| 270 | *tp = t; |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 271 | return res; |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 272 | } |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 273 | #endif |
| 274 | |
Victor Stinner | fa09beb | 2015-03-30 21:36:10 +0200 | [diff] [blame] | 275 | static int |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 276 | _PyTime_FromFloatObject(_PyTime_t *t, double value, _PyTime_round_t round, |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 277 | long unit_to_ns) |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 278 | { |
Victor Stinner | 5786aef | 2015-09-03 16:33:16 +0200 | [diff] [blame] | 279 | double err; |
Victor Stinner | 24b822e | 2015-09-02 11:58:56 +0200 | [diff] [blame] | 280 | /* volatile avoids optimization changing how numbers are rounded */ |
Victor Stinner | 5786aef | 2015-09-03 16:33:16 +0200 | [diff] [blame] | 281 | volatile double d; |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 282 | |
| 283 | /* convert to a number of nanoseconds */ |
| 284 | d = value; |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 285 | d *= (double)unit_to_ns; |
| 286 | d = _PyTime_Round(d, round); |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 287 | |
| 288 | *t = (_PyTime_t)d; |
| 289 | err = d - (double)*t; |
| 290 | if (fabs(err) >= 1.0) { |
| 291 | _PyTime_overflow(); |
| 292 | return -1; |
| 293 | } |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int |
Victor Stinner | fa09beb | 2015-03-30 21:36:10 +0200 | [diff] [blame] | 298 | _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round, |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 299 | long unit_to_ns) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 300 | { |
| 301 | if (PyFloat_Check(obj)) { |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 302 | double d; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 303 | d = PyFloat_AsDouble(obj); |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 304 | return _PyTime_FromFloatObject(t, d, round, unit_to_ns); |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 305 | } |
| 306 | else { |
| 307 | #ifdef HAVE_LONG_LONG |
| 308 | PY_LONG_LONG sec; |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 309 | Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t)); |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 310 | |
| 311 | sec = PyLong_AsLongLong(obj); |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 312 | #else |
| 313 | long sec; |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 314 | Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t)); |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 315 | |
| 316 | sec = PyLong_AsLong(obj); |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 317 | #endif |
| 318 | if (sec == -1 && PyErr_Occurred()) { |
| 319 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 320 | _PyTime_overflow(); |
| 321 | return -1; |
| 322 | } |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 323 | |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 324 | if (_PyTime_check_mul_overflow(sec, unit_to_ns)) { |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 325 | _PyTime_overflow(); |
| 326 | return -1; |
| 327 | } |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 328 | *t = sec * unit_to_ns; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | } |
| 332 | |
Victor Stinner | fa09beb | 2015-03-30 21:36:10 +0200 | [diff] [blame] | 333 | int |
| 334 | _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) |
| 335 | { |
| 336 | return _PyTime_FromObject(t, obj, round, SEC_TO_NS); |
| 337 | } |
| 338 | |
| 339 | int |
| 340 | _PyTime_FromMillisecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) |
| 341 | { |
| 342 | return _PyTime_FromObject(t, obj, round, MS_TO_NS); |
| 343 | } |
| 344 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 345 | double |
| 346 | _PyTime_AsSecondsDouble(_PyTime_t t) |
| 347 | { |
Victor Stinner | ff0ed3e | 2015-09-10 13:25:17 +0200 | [diff] [blame] | 348 | /* volatile avoids optimization changing how numbers are rounded */ |
| 349 | volatile double d; |
| 350 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 351 | if (t % SEC_TO_NS == 0) { |
| 352 | _PyTime_t secs; |
| 353 | /* Divide using integers to avoid rounding issues on the integer part. |
| 354 | 1e-9 cannot be stored exactly in IEEE 64-bit. */ |
| 355 | secs = t / SEC_TO_NS; |
Victor Stinner | ff0ed3e | 2015-09-10 13:25:17 +0200 | [diff] [blame] | 356 | d = (double)secs; |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 357 | } |
| 358 | else { |
Victor Stinner | ff0ed3e | 2015-09-10 13:25:17 +0200 | [diff] [blame] | 359 | d = (double)t; |
| 360 | d /= 1e9; |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 361 | } |
Victor Stinner | ff0ed3e | 2015-09-10 13:25:17 +0200 | [diff] [blame] | 362 | return d; |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 363 | } |
| 364 | |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 365 | PyObject * |
| 366 | _PyTime_AsNanosecondsObject(_PyTime_t t) |
| 367 | { |
| 368 | #ifdef HAVE_LONG_LONG |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 369 | Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) >= sizeof(_PyTime_t)); |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 370 | return PyLong_FromLongLong((PY_LONG_LONG)t); |
| 371 | #else |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 372 | Py_BUILD_ASSERT(sizeof(long) >= sizeof(_PyTime_t)); |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 373 | return PyLong_FromLong((long)t); |
| 374 | #endif |
| 375 | } |
| 376 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 377 | static _PyTime_t |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 378 | _PyTime_Divide(const _PyTime_t t, const _PyTime_t k, |
| 379 | const _PyTime_round_t round) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 380 | { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 381 | assert(k > 1); |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 382 | if (round == _PyTime_ROUND_HALF_EVEN) { |
| 383 | _PyTime_t x, r, abs_r; |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 384 | x = t / k; |
| 385 | r = t % k; |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 386 | abs_r = Py_ABS(r); |
| 387 | if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) { |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 388 | if (t >= 0) |
| 389 | x++; |
| 390 | else |
| 391 | x--; |
| 392 | } |
| 393 | return x; |
| 394 | } |
| 395 | else if (round == _PyTime_ROUND_CEILING) { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 396 | if (t >= 0) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 397 | return (t + k - 1) / k; |
| 398 | else |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 399 | return t / k; |
| 400 | } |
| 401 | else { |
| 402 | if (t >= 0) |
| 403 | return t / k; |
| 404 | else |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 405 | return (t - (k - 1)) / k; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 406 | } |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | _PyTime_t |
| 410 | _PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round) |
| 411 | { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 412 | return _PyTime_Divide(t, NS_TO_MS, round); |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 415 | _PyTime_t |
| 416 | _PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round) |
| 417 | { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 418 | return _PyTime_Divide(t, NS_TO_US, round); |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 421 | static int |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 422 | _PyTime_AsTimeval_impl(_PyTime_t t, _PyTime_t *p_secs, int *p_us, |
| 423 | _PyTime_round_t round) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 424 | { |
| 425 | _PyTime_t secs, ns; |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 426 | int usec; |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 427 | int res = 0; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 428 | |
| 429 | secs = t / SEC_TO_NS; |
| 430 | ns = t % SEC_TO_NS; |
| 431 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 432 | usec = (int)_PyTime_Divide(ns, US_TO_NS, round); |
| 433 | if (usec < 0) { |
| 434 | usec += SEC_TO_US; |
| 435 | if (secs != _PyTime_MIN) |
| 436 | secs -= 1; |
| 437 | else |
| 438 | res = -1; |
| 439 | } |
| 440 | else if (usec >= SEC_TO_US) { |
| 441 | usec -= SEC_TO_US; |
| 442 | if (secs != _PyTime_MAX) |
| 443 | secs += 1; |
| 444 | else |
| 445 | res = -1; |
| 446 | } |
| 447 | assert(0 <= usec && usec < SEC_TO_US); |
| 448 | |
| 449 | *p_secs = secs; |
| 450 | *p_us = usec; |
| 451 | |
| 452 | return res; |
| 453 | } |
| 454 | |
| 455 | static int |
| 456 | _PyTime_AsTimevalStruct_impl(_PyTime_t t, struct timeval *tv, |
| 457 | _PyTime_round_t round, int raise) |
| 458 | { |
Victor Stinner | b7a8af2 | 2015-10-01 08:44:03 +0200 | [diff] [blame] | 459 | _PyTime_t secs, secs2; |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 460 | int us; |
| 461 | int res; |
| 462 | |
| 463 | res = _PyTime_AsTimeval_impl(t, &secs, &us, round); |
| 464 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 465 | #ifdef MS_WINDOWS |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 466 | tv->tv_sec = (long)secs; |
| 467 | #else |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 468 | tv->tv_sec = secs; |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 469 | #endif |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 470 | tv->tv_usec = us; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 471 | |
Victor Stinner | b7a8af2 | 2015-10-01 08:44:03 +0200 | [diff] [blame] | 472 | secs2 = (_PyTime_t)tv->tv_sec; |
| 473 | if (res < 0 || secs2 != secs) { |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 474 | if (raise) |
| 475 | error_time_t_overflow(); |
| 476 | return -1; |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 477 | } |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 478 | return 0; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 481 | int |
| 482 | _PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round) |
| 483 | { |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 484 | return _PyTime_AsTimevalStruct_impl(t, tv, round, 1); |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | int |
| 488 | _PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round) |
| 489 | { |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 490 | return _PyTime_AsTimevalStruct_impl(t, tv, round, 0); |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 491 | } |
| 492 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 493 | int |
| 494 | _PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us, |
| 495 | _PyTime_round_t round) |
| 496 | { |
| 497 | _PyTime_t secs; |
| 498 | int res; |
| 499 | |
| 500 | res = _PyTime_AsTimeval_impl(t, &secs, us, round); |
| 501 | |
| 502 | *p_secs = secs; |
| 503 | |
| 504 | if (res < 0 || (_PyTime_t)*p_secs != secs) { |
| 505 | error_time_t_overflow(); |
| 506 | return -1; |
| 507 | } |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | |
Victor Stinner | c337838 | 2015-03-28 05:07:51 +0100 | [diff] [blame] | 512 | #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE) |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 513 | int |
| 514 | _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts) |
| 515 | { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 516 | _PyTime_t secs, nsec; |
| 517 | |
| 518 | secs = t / SEC_TO_NS; |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 519 | nsec = t % SEC_TO_NS; |
| 520 | if (nsec < 0) { |
| 521 | nsec += SEC_TO_NS; |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 522 | secs -= 1; |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 523 | } |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 524 | ts->tv_sec = (time_t)secs; |
Victor Stinner | 29ee674 | 2015-09-03 16:25:45 +0200 | [diff] [blame] | 525 | assert(0 <= nsec && nsec < SEC_TO_NS); |
| 526 | ts->tv_nsec = nsec; |
| 527 | |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 528 | if ((_PyTime_t)ts->tv_sec != secs) { |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 529 | error_time_t_overflow(); |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 530 | return -1; |
| 531 | } |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 532 | return 0; |
| 533 | } |
| 534 | #endif |
| 535 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 536 | static int |
Victor Stinner | c379ade | 2015-11-10 12:11:39 +0100 | [diff] [blame] | 537 | pygettimeofday(_PyTime_t *tp, _Py_clock_info_t *info, int raise) |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 538 | { |
| 539 | #ifdef MS_WINDOWS |
| 540 | FILETIME system_time; |
| 541 | ULARGE_INTEGER large; |
| 542 | |
| 543 | assert(info == NULL || raise); |
| 544 | |
| 545 | GetSystemTimeAsFileTime(&system_time); |
| 546 | large.u.LowPart = system_time.dwLowDateTime; |
| 547 | large.u.HighPart = system_time.dwHighDateTime; |
| 548 | /* 11,644,473,600,000,000,000: number of nanoseconds between |
| 549 | the 1st january 1601 and the 1st january 1970 (369 years + 89 leap |
| 550 | days). */ |
| 551 | *tp = large.QuadPart * 100 - 11644473600000000000; |
| 552 | if (info) { |
| 553 | DWORD timeAdjustment, timeIncrement; |
| 554 | BOOL isTimeAdjustmentDisabled, ok; |
| 555 | |
| 556 | info->implementation = "GetSystemTimeAsFileTime()"; |
| 557 | info->monotonic = 0; |
| 558 | ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement, |
| 559 | &isTimeAdjustmentDisabled); |
| 560 | if (!ok) { |
| 561 | PyErr_SetFromWindowsErr(0); |
| 562 | return -1; |
| 563 | } |
| 564 | info->resolution = timeIncrement * 1e-7; |
| 565 | info->adjustable = 1; |
| 566 | } |
| 567 | |
| 568 | #else /* MS_WINDOWS */ |
| 569 | int err; |
| 570 | #ifdef HAVE_CLOCK_GETTIME |
| 571 | struct timespec ts; |
| 572 | #else |
| 573 | struct timeval tv; |
| 574 | #endif |
| 575 | |
| 576 | assert(info == NULL || raise); |
| 577 | |
| 578 | #ifdef HAVE_CLOCK_GETTIME |
| 579 | err = clock_gettime(CLOCK_REALTIME, &ts); |
| 580 | if (err) { |
| 581 | if (raise) |
| 582 | PyErr_SetFromErrno(PyExc_OSError); |
| 583 | return -1; |
| 584 | } |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 585 | if (_PyTime_FromTimespec(tp, &ts, raise) < 0) |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 586 | return -1; |
| 587 | |
| 588 | if (info) { |
| 589 | struct timespec res; |
| 590 | info->implementation = "clock_gettime(CLOCK_REALTIME)"; |
| 591 | info->monotonic = 0; |
| 592 | info->adjustable = 1; |
| 593 | if (clock_getres(CLOCK_REALTIME, &res) == 0) |
| 594 | info->resolution = res.tv_sec + res.tv_nsec * 1e-9; |
| 595 | else |
| 596 | info->resolution = 1e-9; |
| 597 | } |
| 598 | #else /* HAVE_CLOCK_GETTIME */ |
| 599 | |
| 600 | /* test gettimeofday() */ |
| 601 | #ifdef GETTIMEOFDAY_NO_TZ |
| 602 | err = gettimeofday(&tv); |
| 603 | #else |
| 604 | err = gettimeofday(&tv, (struct timezone *)NULL); |
| 605 | #endif |
| 606 | if (err) { |
| 607 | if (raise) |
| 608 | PyErr_SetFromErrno(PyExc_OSError); |
| 609 | return -1; |
| 610 | } |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 611 | if (_PyTime_FromTimeval(tp, &tv, raise) < 0) |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 612 | return -1; |
| 613 | |
| 614 | if (info) { |
| 615 | info->implementation = "gettimeofday()"; |
| 616 | info->resolution = 1e-6; |
| 617 | info->monotonic = 0; |
| 618 | info->adjustable = 1; |
| 619 | } |
| 620 | #endif /* !HAVE_CLOCK_GETTIME */ |
| 621 | #endif /* !MS_WINDOWS */ |
| 622 | return 0; |
| 623 | } |
| 624 | |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 625 | _PyTime_t |
| 626 | _PyTime_GetSystemClock(void) |
| 627 | { |
| 628 | _PyTime_t t; |
Victor Stinner | c379ade | 2015-11-10 12:11:39 +0100 | [diff] [blame] | 629 | if (pygettimeofday(&t, NULL, 0) < 0) { |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 630 | /* should not happen, _PyTime_Init() checked the clock at startup */ |
| 631 | assert(0); |
| 632 | |
| 633 | /* use a fixed value instead of a random value from the stack */ |
| 634 | t = 0; |
| 635 | } |
| 636 | return t; |
| 637 | } |
| 638 | |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 639 | int |
| 640 | _PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info) |
| 641 | { |
Victor Stinner | c379ade | 2015-11-10 12:11:39 +0100 | [diff] [blame] | 642 | return pygettimeofday(t, info, 1); |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 643 | } |
| 644 | |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 645 | static int |
Victor Stinner | 5ad5821 | 2015-09-03 00:14:58 +0200 | [diff] [blame] | 646 | pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 647 | { |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 648 | #if defined(MS_WINDOWS) |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 649 | ULONGLONG ticks; |
| 650 | _PyTime_t t; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 651 | |
| 652 | assert(info == NULL || raise); |
| 653 | |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 654 | ticks = GetTickCount64(); |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 655 | Py_BUILD_ASSERT(sizeof(ticks) <= sizeof(_PyTime_t)); |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 656 | t = (_PyTime_t)ticks; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 657 | |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 658 | if (_PyTime_check_mul_overflow(t, MS_TO_NS)) { |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 659 | if (raise) { |
| 660 | _PyTime_overflow(); |
| 661 | return -1; |
| 662 | } |
| 663 | /* Hello, time traveler! */ |
| 664 | assert(0); |
| 665 | } |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 666 | *tp = t * MS_TO_NS; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 667 | |
| 668 | if (info) { |
| 669 | DWORD timeAdjustment, timeIncrement; |
| 670 | BOOL isTimeAdjustmentDisabled, ok; |
Victor Stinner | eb35229 | 2015-03-27 14:12:08 +0100 | [diff] [blame] | 671 | info->implementation = "GetTickCount64()"; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 672 | info->monotonic = 1; |
| 673 | ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement, |
| 674 | &isTimeAdjustmentDisabled); |
| 675 | if (!ok) { |
| 676 | PyErr_SetFromWindowsErr(0); |
| 677 | return -1; |
| 678 | } |
| 679 | info->resolution = timeIncrement * 1e-7; |
| 680 | info->adjustable = 0; |
| 681 | } |
| 682 | |
| 683 | #elif defined(__APPLE__) |
| 684 | static mach_timebase_info_data_t timebase; |
| 685 | uint64_t time; |
| 686 | |
| 687 | if (timebase.denom == 0) { |
| 688 | /* According to the Technical Q&A QA1398, mach_timebase_info() cannot |
| 689 | fail: https://developer.apple.com/library/mac/#qa/qa1398/ */ |
| 690 | (void)mach_timebase_info(&timebase); |
| 691 | } |
| 692 | |
| 693 | time = mach_absolute_time(); |
| 694 | |
| 695 | /* apply timebase factor */ |
| 696 | time *= timebase.numer; |
| 697 | time /= timebase.denom; |
| 698 | |
| 699 | *tp = time; |
| 700 | |
| 701 | if (info) { |
| 702 | info->implementation = "mach_absolute_time()"; |
| 703 | info->resolution = (double)timebase.numer / timebase.denom * 1e-9; |
| 704 | info->monotonic = 1; |
| 705 | info->adjustable = 0; |
| 706 | } |
| 707 | |
| 708 | #else |
| 709 | struct timespec ts; |
| 710 | #ifdef CLOCK_HIGHRES |
| 711 | const clockid_t clk_id = CLOCK_HIGHRES; |
| 712 | const char *implementation = "clock_gettime(CLOCK_HIGHRES)"; |
| 713 | #else |
| 714 | const clockid_t clk_id = CLOCK_MONOTONIC; |
| 715 | const char *implementation = "clock_gettime(CLOCK_MONOTONIC)"; |
| 716 | #endif |
| 717 | |
| 718 | assert(info == NULL || raise); |
| 719 | |
| 720 | if (clock_gettime(clk_id, &ts) != 0) { |
| 721 | if (raise) { |
| 722 | PyErr_SetFromErrno(PyExc_OSError); |
| 723 | return -1; |
| 724 | } |
| 725 | return -1; |
| 726 | } |
| 727 | |
| 728 | if (info) { |
| 729 | struct timespec res; |
| 730 | info->monotonic = 1; |
| 731 | info->implementation = implementation; |
| 732 | info->adjustable = 0; |
| 733 | if (clock_getres(clk_id, &res) != 0) { |
| 734 | PyErr_SetFromErrno(PyExc_OSError); |
| 735 | return -1; |
| 736 | } |
| 737 | info->resolution = res.tv_sec + res.tv_nsec * 1e-9; |
| 738 | } |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 739 | if (_PyTime_FromTimespec(tp, &ts, raise) < 0) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 740 | return -1; |
| 741 | #endif |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | _PyTime_t |
| 746 | _PyTime_GetMonotonicClock(void) |
| 747 | { |
| 748 | _PyTime_t t; |
Victor Stinner | 5ad5821 | 2015-09-03 00:14:58 +0200 | [diff] [blame] | 749 | if (pymonotonic(&t, NULL, 0) < 0) { |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 750 | /* should not happen, _PyTime_Init() checked that monotonic clock at |
| 751 | startup */ |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 752 | assert(0); |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 753 | |
| 754 | /* use a fixed value instead of a random value from the stack */ |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 755 | t = 0; |
| 756 | } |
| 757 | return t; |
| 758 | } |
| 759 | |
Victor Stinner | 0011124 | 2014-08-29 16:31:59 +0200 | [diff] [blame] | 760 | int |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 761 | _PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) |
| 762 | { |
Victor Stinner | 5ad5821 | 2015-09-03 00:14:58 +0200 | [diff] [blame] | 763 | return pymonotonic(tp, info, 1); |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | int |
Victor Stinner | 0011124 | 2014-08-29 16:31:59 +0200 | [diff] [blame] | 767 | _PyTime_Init(void) |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 768 | { |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 769 | _PyTime_t t; |
| 770 | |
Victor Stinner | 0011124 | 2014-08-29 16:31:59 +0200 | [diff] [blame] | 771 | /* ensure that the system clock works */ |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 772 | if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0) |
Victor Stinner | 0011124 | 2014-08-29 16:31:59 +0200 | [diff] [blame] | 773 | return -1; |
Victor Stinner | ae58649 | 2014-09-02 23:18:25 +0200 | [diff] [blame] | 774 | |
| 775 | /* ensure that the operating system provides a monotonic clock */ |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 776 | if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 777 | return -1; |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 778 | |
Victor Stinner | 0011124 | 2014-08-29 16:31:59 +0200 | [diff] [blame] | 779 | return 0; |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 780 | } |