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 | |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 38 | static void |
| 39 | _PyTime_overflow(void) |
| 40 | { |
| 41 | PyErr_SetString(PyExc_OverflowError, |
| 42 | "timestamp too large to convert to C _PyTime_t"); |
| 43 | } |
| 44 | |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 45 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 46 | _PyTime_t |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 47 | _PyTime_MulDiv(_PyTime_t ticks, _PyTime_t mul, _PyTime_t div) |
| 48 | { |
| 49 | _PyTime_t intpart, remaining; |
| 50 | /* Compute (ticks * mul / div) in two parts to prevent integer overflow: |
| 51 | compute integer part, and then the remaining part. |
| 52 | |
| 53 | (ticks * mul) / div == (ticks / div) * mul + (ticks % div) * mul / div |
| 54 | |
| 55 | The caller must ensure that "(div - 1) * mul" cannot overflow. */ |
| 56 | intpart = ticks / div; |
| 57 | ticks %= div; |
| 58 | remaining = ticks * mul; |
| 59 | remaining /= div; |
| 60 | return intpart * mul + remaining; |
| 61 | } |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 62 | |
| 63 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 64 | time_t |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 65 | _PyLong_AsTime_t(PyObject *obj) |
| 66 | { |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 67 | #if SIZEOF_TIME_T == SIZEOF_LONG_LONG |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 68 | long long val; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 69 | val = PyLong_AsLongLong(obj); |
| 70 | #else |
| 71 | long val; |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 72 | Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long)); |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 73 | val = PyLong_AsLong(obj); |
| 74 | #endif |
| 75 | if (val == -1 && PyErr_Occurred()) { |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 76 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 77 | error_time_t_overflow(); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 78 | } |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 79 | return -1; |
| 80 | } |
| 81 | return (time_t)val; |
| 82 | } |
| 83 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 84 | PyObject * |
| 85 | _PyLong_FromTime_t(time_t t) |
| 86 | { |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 87 | #if SIZEOF_TIME_T == SIZEOF_LONG_LONG |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 88 | return PyLong_FromLongLong((long long)t); |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 89 | #else |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 90 | Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long)); |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 91 | return PyLong_FromLong((long)t); |
| 92 | #endif |
| 93 | } |
| 94 | |
Victor Stinner | ce6aa74 | 2015-09-09 22:28:09 +0200 | [diff] [blame] | 95 | /* Round to nearest with ties going to nearest even integer |
| 96 | (_PyTime_ROUND_HALF_EVEN) */ |
| 97 | static double |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 98 | _PyTime_RoundHalfEven(double x) |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 99 | { |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 100 | double rounded = round(x); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 101 | if (fabs(x-rounded) == 0.5) { |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 102 | /* halfway case: round to even */ |
| 103 | rounded = 2.0*round(x/2.0); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 104 | } |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 105 | return rounded; |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 108 | static double |
| 109 | _PyTime_Round(double x, _PyTime_round_t round) |
| 110 | { |
Victor Stinner | 1efbeba | 2015-09-10 11:48:00 +0200 | [diff] [blame] | 111 | /* volatile avoids optimization changing how numbers are rounded */ |
| 112 | volatile double d; |
| 113 | |
| 114 | d = x; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 115 | if (round == _PyTime_ROUND_HALF_EVEN) { |
Victor Stinner | 1efbeba | 2015-09-10 11:48:00 +0200 | [diff] [blame] | 116 | d = _PyTime_RoundHalfEven(d); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 117 | } |
| 118 | else if (round == _PyTime_ROUND_CEILING) { |
Victor Stinner | 1efbeba | 2015-09-10 11:48:00 +0200 | [diff] [blame] | 119 | d = ceil(d); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 120 | } |
Pablo Galindo | 2c15b29 | 2017-10-17 15:14:41 +0100 | [diff] [blame] | 121 | else if (round == _PyTime_ROUND_FLOOR) { |
Victor Stinner | 1efbeba | 2015-09-10 11:48:00 +0200 | [diff] [blame] | 122 | d = floor(d); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 123 | } |
Pablo Galindo | 2c15b29 | 2017-10-17 15:14:41 +0100 | [diff] [blame] | 124 | else { |
| 125 | assert(round == _PyTime_ROUND_UP); |
| 126 | d = (d >= 0.0) ? ceil(d) : floor(d); |
| 127 | } |
Victor Stinner | 1efbeba | 2015-09-10 11:48:00 +0200 | [diff] [blame] | 128 | return d; |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 131 | static int |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 132 | _PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator, |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 133 | long idenominator, _PyTime_round_t round) |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 134 | { |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 135 | double denominator = (double)idenominator; |
Benjamin Peterson | a853a8b | 2017-09-07 11:13:59 -0700 | [diff] [blame] | 136 | double intpart; |
Victor Stinner | 24b822e | 2015-09-02 11:58:56 +0200 | [diff] [blame] | 137 | /* volatile avoids optimization changing how numbers are rounded */ |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 138 | volatile double floatpart; |
| 139 | |
| 140 | floatpart = modf(d, &intpart); |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 141 | |
| 142 | floatpart *= denominator; |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 143 | floatpart = _PyTime_Round(floatpart, round); |
Victor Stinner | 67edcc9 | 2015-09-02 10:37:46 +0200 | [diff] [blame] | 144 | if (floatpart >= denominator) { |
| 145 | floatpart -= denominator; |
| 146 | intpart += 1.0; |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 147 | } |
Victor Stinner | adfefa5 | 2015-09-04 23:57:25 +0200 | [diff] [blame] | 148 | else if (floatpart < 0) { |
| 149 | floatpart += denominator; |
| 150 | intpart -= 1.0; |
| 151 | } |
Victor Stinner | 67edcc9 | 2015-09-02 10:37:46 +0200 | [diff] [blame] | 152 | assert(0.0 <= floatpart && floatpart < denominator); |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 153 | |
Benjamin Peterson | a853a8b | 2017-09-07 11:13:59 -0700 | [diff] [blame] | 154 | if (!_Py_InIntegralTypeRange(time_t, intpart)) { |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 155 | error_time_t_overflow(); |
| 156 | return -1; |
| 157 | } |
Benjamin Peterson | a853a8b | 2017-09-07 11:13:59 -0700 | [diff] [blame] | 158 | *sec = (time_t)intpart; |
| 159 | *numerator = (long)floatpart; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 160 | assert(0 <= *numerator && *numerator < idenominator); |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 165 | _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator, |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 166 | long denominator, _PyTime_round_t round) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 167 | { |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 168 | assert(denominator >= 1); |
Victor Stinner | bbdda21 | 2015-09-02 00:50:43 +0200 | [diff] [blame] | 169 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 170 | if (PyFloat_Check(obj)) { |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 171 | double d = PyFloat_AsDouble(obj); |
Han Lee | 829dacc | 2017-09-09 08:05:05 +0900 | [diff] [blame] | 172 | if (Py_IS_NAN(d)) { |
| 173 | *numerator = 0; |
| 174 | PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)"); |
| 175 | return -1; |
| 176 | } |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 177 | return _PyTime_DoubleToDenominator(d, sec, numerator, |
| 178 | denominator, round); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 179 | } |
| 180 | else { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 181 | *sec = _PyLong_AsTime_t(obj); |
Victor Stinner | 67edcc9 | 2015-09-02 10:37:46 +0200 | [diff] [blame] | 182 | *numerator = 0; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 183 | if (*sec == (time_t)-1 && PyErr_Occurred()) { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 184 | return -1; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 185 | } |
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 |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 191 | _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 192 | { |
| 193 | if (PyFloat_Check(obj)) { |
Benjamin Peterson | a853a8b | 2017-09-07 11:13:59 -0700 | [diff] [blame] | 194 | double intpart; |
Victor Stinner | 24b822e | 2015-09-02 11:58:56 +0200 | [diff] [blame] | 195 | /* volatile avoids optimization changing how numbers are rounded */ |
Victor Stinner | 5786aef | 2015-09-03 16:33:16 +0200 | [diff] [blame] | 196 | volatile double d; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 197 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 198 | d = PyFloat_AsDouble(obj); |
Han Lee | 829dacc | 2017-09-09 08:05:05 +0900 | [diff] [blame] | 199 | if (Py_IS_NAN(d)) { |
| 200 | PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)"); |
| 201 | return -1; |
| 202 | } |
| 203 | |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 204 | d = _PyTime_Round(d, round); |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 205 | (void)modf(d, &intpart); |
| 206 | |
Benjamin Peterson | a853a8b | 2017-09-07 11:13:59 -0700 | [diff] [blame] | 207 | if (!_Py_InIntegralTypeRange(time_t, intpart)) { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 208 | error_time_t_overflow(); |
| 209 | return -1; |
| 210 | } |
Benjamin Peterson | a853a8b | 2017-09-07 11:13:59 -0700 | [diff] [blame] | 211 | *sec = (time_t)intpart; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | else { |
| 215 | *sec = _PyLong_AsTime_t(obj); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 216 | if (*sec == (time_t)-1 && PyErr_Occurred()) { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 217 | return -1; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 218 | } |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | int |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 224 | _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec, |
| 225 | _PyTime_round_t round) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 226 | { |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 227 | return _PyTime_ObjectToDenominator(obj, sec, nsec, SEC_TO_NS, round); |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | int |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 231 | _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec, |
| 232 | _PyTime_round_t round) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 233 | { |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 234 | return _PyTime_ObjectToDenominator(obj, sec, usec, SEC_TO_US, round); |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 237 | _PyTime_t |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 238 | _PyTime_FromSeconds(int seconds) |
| 239 | { |
| 240 | _PyTime_t t; |
| 241 | /* ensure that integer overflow cannot happen, int type should have 32 |
| 242 | bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30 |
| 243 | bits). */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 244 | Py_BUILD_ASSERT(INT_MAX <= _PyTime_MAX / SEC_TO_NS); |
| 245 | Py_BUILD_ASSERT(INT_MIN >= _PyTime_MIN / SEC_TO_NS); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 246 | |
| 247 | t = (_PyTime_t)seconds; |
Victor Stinner | bbdda21 | 2015-09-02 00:50:43 +0200 | [diff] [blame] | 248 | assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS) |
| 249 | || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS)); |
| 250 | t *= SEC_TO_NS; |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 251 | return t; |
| 252 | } |
| 253 | |
| 254 | _PyTime_t |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 255 | _PyTime_FromNanoseconds(_PyTime_t ns) |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 256 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 257 | /* _PyTime_t already uses nanosecond resolution, no conversion needed */ |
| 258 | return ns; |
| 259 | } |
| 260 | |
| 261 | int |
| 262 | _PyTime_FromNanosecondsObject(_PyTime_t *tp, PyObject *obj) |
| 263 | { |
| 264 | long long nsec; |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 265 | _PyTime_t t; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 266 | |
| 267 | if (!PyLong_Check(obj)) { |
| 268 | PyErr_Format(PyExc_TypeError, "expect int, got %s", |
| 269 | Py_TYPE(obj)->tp_name); |
| 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | Py_BUILD_ASSERT(sizeof(long long) == sizeof(_PyTime_t)); |
| 274 | nsec = PyLong_AsLongLong(obj); |
| 275 | if (nsec == -1 && PyErr_Occurred()) { |
| 276 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 277 | _PyTime_overflow(); |
| 278 | } |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | /* _PyTime_t already uses nanosecond resolution, no conversion needed */ |
| 283 | t = (_PyTime_t)nsec; |
| 284 | *tp = t; |
| 285 | return 0; |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 288 | #ifdef HAVE_CLOCK_GETTIME |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 289 | static int |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 290 | pytime_fromtimespec(_PyTime_t *tp, struct timespec *ts, int raise) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 291 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 292 | _PyTime_t t, nsec; |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 293 | int res = 0; |
| 294 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 295 | Py_BUILD_ASSERT(sizeof(ts->tv_sec) <= sizeof(_PyTime_t)); |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 296 | t = (_PyTime_t)ts->tv_sec; |
| 297 | |
| 298 | if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) { |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 299 | if (raise) { |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 300 | _PyTime_overflow(); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 301 | } |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 302 | res = -1; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 303 | t = (t > 0) ? _PyTime_MAX : _PyTime_MIN; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 304 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 305 | else { |
| 306 | t = t * SEC_TO_NS; |
| 307 | } |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 308 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 309 | nsec = ts->tv_nsec; |
| 310 | /* The following test is written for positive only nsec */ |
| 311 | assert(nsec >= 0); |
| 312 | if (t > _PyTime_MAX - nsec) { |
| 313 | if (raise) { |
| 314 | _PyTime_overflow(); |
| 315 | } |
| 316 | res = -1; |
| 317 | t = _PyTime_MAX; |
| 318 | } |
| 319 | else { |
| 320 | t += nsec; |
| 321 | } |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 322 | |
| 323 | *tp = t; |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 324 | return res; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 325 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 326 | |
| 327 | int |
| 328 | _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts) |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 329 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 330 | return pytime_fromtimespec(tp, ts, 1); |
| 331 | } |
| 332 | #endif |
| 333 | |
| 334 | #if !defined(MS_WINDOWS) |
| 335 | static int |
| 336 | pytime_fromtimeval(_PyTime_t *tp, struct timeval *tv, int raise) |
| 337 | { |
| 338 | _PyTime_t t, usec; |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 339 | int res = 0; |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 340 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 341 | Py_BUILD_ASSERT(sizeof(tv->tv_sec) <= sizeof(_PyTime_t)); |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 342 | t = (_PyTime_t)tv->tv_sec; |
| 343 | |
| 344 | if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) { |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 345 | if (raise) { |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 346 | _PyTime_overflow(); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 347 | } |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 348 | res = -1; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 349 | t = (t > 0) ? _PyTime_MAX : _PyTime_MIN; |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 350 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 351 | else { |
| 352 | t = t * SEC_TO_NS; |
| 353 | } |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 354 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 355 | usec = (_PyTime_t)tv->tv_usec * US_TO_NS; |
| 356 | /* The following test is written for positive only usec */ |
| 357 | assert(usec >= 0); |
| 358 | if (t > _PyTime_MAX - usec) { |
| 359 | if (raise) { |
| 360 | _PyTime_overflow(); |
| 361 | } |
| 362 | res = -1; |
| 363 | t = _PyTime_MAX; |
| 364 | } |
| 365 | else { |
| 366 | t += usec; |
| 367 | } |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 368 | |
| 369 | *tp = t; |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 370 | return res; |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 371 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 372 | |
| 373 | int |
| 374 | _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv) |
| 375 | { |
| 376 | return pytime_fromtimeval(tp, tv, 1); |
| 377 | } |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 378 | #endif |
| 379 | |
Victor Stinner | fa09beb | 2015-03-30 21:36:10 +0200 | [diff] [blame] | 380 | static int |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 381 | _PyTime_FromDouble(_PyTime_t *t, double value, _PyTime_round_t round, |
| 382 | long unit_to_ns) |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 383 | { |
Victor Stinner | 24b822e | 2015-09-02 11:58:56 +0200 | [diff] [blame] | 384 | /* volatile avoids optimization changing how numbers are rounded */ |
Victor Stinner | 5786aef | 2015-09-03 16:33:16 +0200 | [diff] [blame] | 385 | volatile double d; |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 386 | |
| 387 | /* convert to a number of nanoseconds */ |
| 388 | d = value; |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 389 | d *= (double)unit_to_ns; |
| 390 | d = _PyTime_Round(d, round); |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 391 | |
Benjamin Peterson | a853a8b | 2017-09-07 11:13:59 -0700 | [diff] [blame] | 392 | if (!_Py_InIntegralTypeRange(_PyTime_t, d)) { |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 393 | _PyTime_overflow(); |
| 394 | return -1; |
| 395 | } |
Benjamin Peterson | a853a8b | 2017-09-07 11:13:59 -0700 | [diff] [blame] | 396 | *t = (_PyTime_t)d; |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static int |
Victor Stinner | fa09beb | 2015-03-30 21:36:10 +0200 | [diff] [blame] | 401 | _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round, |
Victor Stinner | 9ae47df | 2015-09-09 22:28:58 +0200 | [diff] [blame] | 402 | long unit_to_ns) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 403 | { |
| 404 | if (PyFloat_Check(obj)) { |
Victor Stinner | 53e137c | 2015-09-02 00:49:16 +0200 | [diff] [blame] | 405 | double d; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 406 | d = PyFloat_AsDouble(obj); |
Han Lee | 829dacc | 2017-09-09 08:05:05 +0900 | [diff] [blame] | 407 | if (Py_IS_NAN(d)) { |
| 408 | PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)"); |
| 409 | return -1; |
| 410 | } |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 411 | return _PyTime_FromDouble(t, d, round, unit_to_ns); |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 412 | } |
| 413 | else { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 414 | long long sec; |
| 415 | Py_BUILD_ASSERT(sizeof(long long) <= sizeof(_PyTime_t)); |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 416 | |
| 417 | sec = PyLong_AsLongLong(obj); |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 418 | if (sec == -1 && PyErr_Occurred()) { |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 419 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 420 | _PyTime_overflow(); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 421 | } |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 422 | return -1; |
| 423 | } |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 424 | |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 425 | if (_PyTime_check_mul_overflow(sec, unit_to_ns)) { |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 426 | _PyTime_overflow(); |
| 427 | return -1; |
| 428 | } |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 429 | *t = sec * unit_to_ns; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | } |
| 433 | |
Victor Stinner | fa09beb | 2015-03-30 21:36:10 +0200 | [diff] [blame] | 434 | int |
| 435 | _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) |
| 436 | { |
| 437 | return _PyTime_FromObject(t, obj, round, SEC_TO_NS); |
| 438 | } |
| 439 | |
| 440 | int |
| 441 | _PyTime_FromMillisecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) |
| 442 | { |
| 443 | return _PyTime_FromObject(t, obj, round, MS_TO_NS); |
| 444 | } |
| 445 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 446 | double |
| 447 | _PyTime_AsSecondsDouble(_PyTime_t t) |
| 448 | { |
Victor Stinner | ff0ed3e | 2015-09-10 13:25:17 +0200 | [diff] [blame] | 449 | /* volatile avoids optimization changing how numbers are rounded */ |
| 450 | volatile double d; |
| 451 | |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 452 | if (t % SEC_TO_NS == 0) { |
| 453 | _PyTime_t secs; |
| 454 | /* Divide using integers to avoid rounding issues on the integer part. |
| 455 | 1e-9 cannot be stored exactly in IEEE 64-bit. */ |
| 456 | secs = t / SEC_TO_NS; |
Victor Stinner | ff0ed3e | 2015-09-10 13:25:17 +0200 | [diff] [blame] | 457 | d = (double)secs; |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 458 | } |
| 459 | else { |
Victor Stinner | ff0ed3e | 2015-09-10 13:25:17 +0200 | [diff] [blame] | 460 | d = (double)t; |
| 461 | d /= 1e9; |
Victor Stinner | 3e2c8d8 | 2015-09-09 22:32:48 +0200 | [diff] [blame] | 462 | } |
Victor Stinner | ff0ed3e | 2015-09-10 13:25:17 +0200 | [diff] [blame] | 463 | return d; |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 466 | PyObject * |
| 467 | _PyTime_AsNanosecondsObject(_PyTime_t t) |
| 468 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 469 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(_PyTime_t)); |
| 470 | return PyLong_FromLongLong((long long)t); |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 471 | } |
| 472 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 473 | static _PyTime_t |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 474 | _PyTime_Divide(const _PyTime_t t, const _PyTime_t k, |
| 475 | const _PyTime_round_t round) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 476 | { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 477 | assert(k > 1); |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 478 | if (round == _PyTime_ROUND_HALF_EVEN) { |
| 479 | _PyTime_t x, r, abs_r; |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 480 | x = t / k; |
| 481 | r = t % k; |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 482 | abs_r = Py_ABS(r); |
| 483 | if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) { |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 484 | if (t >= 0) { |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 485 | x++; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 486 | } |
| 487 | else { |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 488 | x--; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 489 | } |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 490 | } |
| 491 | return x; |
| 492 | } |
| 493 | else if (round == _PyTime_ROUND_CEILING) { |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 494 | if (t >= 0) { |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 495 | return (t + k - 1) / k; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 496 | } |
| 497 | else { |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 498 | return t / k; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 499 | } |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 500 | } |
Pablo Galindo | 2c15b29 | 2017-10-17 15:14:41 +0100 | [diff] [blame] | 501 | else if (round == _PyTime_ROUND_FLOOR){ |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 502 | if (t >= 0) { |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 503 | return t / k; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 504 | } |
| 505 | else { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 506 | return (t - (k - 1)) / k; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 507 | } |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 508 | } |
Pablo Galindo | 2c15b29 | 2017-10-17 15:14:41 +0100 | [diff] [blame] | 509 | else { |
| 510 | assert(round == _PyTime_ROUND_UP); |
| 511 | if (t >= 0) { |
| 512 | return (t + k - 1) / k; |
| 513 | } |
| 514 | else { |
| 515 | return (t - (k - 1)) / k; |
| 516 | } |
| 517 | } |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | _PyTime_t |
| 521 | _PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round) |
| 522 | { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 523 | return _PyTime_Divide(t, NS_TO_MS, round); |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 524 | } |
| 525 | |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 526 | _PyTime_t |
| 527 | _PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round) |
| 528 | { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 529 | return _PyTime_Divide(t, NS_TO_US, round); |
Victor Stinner | f5faad2 | 2015-03-28 03:52:05 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 532 | static int |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 533 | _PyTime_AsTimeval_impl(_PyTime_t t, _PyTime_t *p_secs, int *p_us, |
| 534 | _PyTime_round_t round) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 535 | { |
| 536 | _PyTime_t secs, ns; |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 537 | int usec; |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 538 | int res = 0; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 539 | |
| 540 | secs = t / SEC_TO_NS; |
| 541 | ns = t % SEC_TO_NS; |
| 542 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 543 | usec = (int)_PyTime_Divide(ns, US_TO_NS, round); |
| 544 | if (usec < 0) { |
| 545 | usec += SEC_TO_US; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 546 | if (secs != _PyTime_MIN) { |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 547 | secs -= 1; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 548 | } |
| 549 | else { |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 550 | res = -1; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 551 | } |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 552 | } |
| 553 | else if (usec >= SEC_TO_US) { |
| 554 | usec -= SEC_TO_US; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 555 | if (secs != _PyTime_MAX) { |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 556 | secs += 1; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 557 | } |
| 558 | else { |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 559 | res = -1; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 560 | } |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 561 | } |
| 562 | assert(0 <= usec && usec < SEC_TO_US); |
| 563 | |
| 564 | *p_secs = secs; |
| 565 | *p_us = usec; |
| 566 | |
| 567 | return res; |
| 568 | } |
| 569 | |
| 570 | static int |
| 571 | _PyTime_AsTimevalStruct_impl(_PyTime_t t, struct timeval *tv, |
| 572 | _PyTime_round_t round, int raise) |
| 573 | { |
Victor Stinner | b7a8af2 | 2015-10-01 08:44:03 +0200 | [diff] [blame] | 574 | _PyTime_t secs, secs2; |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 575 | int us; |
| 576 | int res; |
| 577 | |
| 578 | res = _PyTime_AsTimeval_impl(t, &secs, &us, round); |
| 579 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 580 | #ifdef MS_WINDOWS |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 581 | tv->tv_sec = (long)secs; |
| 582 | #else |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 583 | tv->tv_sec = secs; |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 584 | #endif |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 585 | tv->tv_usec = us; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 586 | |
Victor Stinner | b7a8af2 | 2015-10-01 08:44:03 +0200 | [diff] [blame] | 587 | secs2 = (_PyTime_t)tv->tv_sec; |
| 588 | if (res < 0 || secs2 != secs) { |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 589 | if (raise) { |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 590 | error_time_t_overflow(); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 591 | } |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 592 | return -1; |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 593 | } |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 594 | return 0; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 597 | int |
| 598 | _PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round) |
| 599 | { |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 600 | return _PyTime_AsTimevalStruct_impl(t, tv, round, 1); |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | int |
| 604 | _PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round) |
| 605 | { |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 606 | return _PyTime_AsTimevalStruct_impl(t, tv, round, 0); |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 607 | } |
| 608 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 609 | int |
| 610 | _PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us, |
| 611 | _PyTime_round_t round) |
| 612 | { |
| 613 | _PyTime_t secs; |
| 614 | int res; |
| 615 | |
| 616 | res = _PyTime_AsTimeval_impl(t, &secs, us, round); |
| 617 | |
| 618 | *p_secs = secs; |
| 619 | |
| 620 | if (res < 0 || (_PyTime_t)*p_secs != secs) { |
| 621 | error_time_t_overflow(); |
| 622 | return -1; |
| 623 | } |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | |
Victor Stinner | c337838 | 2015-03-28 05:07:51 +0100 | [diff] [blame] | 628 | #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE) |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 629 | int |
| 630 | _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts) |
| 631 | { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 632 | _PyTime_t secs, nsec; |
| 633 | |
| 634 | secs = t / SEC_TO_NS; |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 635 | nsec = t % SEC_TO_NS; |
| 636 | if (nsec < 0) { |
| 637 | nsec += SEC_TO_NS; |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 638 | secs -= 1; |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 639 | } |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 640 | ts->tv_sec = (time_t)secs; |
Victor Stinner | 29ee674 | 2015-09-03 16:25:45 +0200 | [diff] [blame] | 641 | assert(0 <= nsec && nsec < SEC_TO_NS); |
| 642 | ts->tv_nsec = nsec; |
| 643 | |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 644 | if ((_PyTime_t)ts->tv_sec != secs) { |
Victor Stinner | 9c72f9b | 2015-09-10 09:10:14 +0200 | [diff] [blame] | 645 | error_time_t_overflow(); |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 646 | return -1; |
| 647 | } |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 648 | return 0; |
| 649 | } |
| 650 | #endif |
| 651 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 652 | static int |
Victor Stinner | c379ade | 2015-11-10 12:11:39 +0100 | [diff] [blame] | 653 | pygettimeofday(_PyTime_t *tp, _Py_clock_info_t *info, int raise) |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 654 | { |
| 655 | #ifdef MS_WINDOWS |
| 656 | FILETIME system_time; |
| 657 | ULARGE_INTEGER large; |
| 658 | |
| 659 | assert(info == NULL || raise); |
| 660 | |
| 661 | GetSystemTimeAsFileTime(&system_time); |
| 662 | large.u.LowPart = system_time.dwLowDateTime; |
| 663 | large.u.HighPart = system_time.dwHighDateTime; |
| 664 | /* 11,644,473,600,000,000,000: number of nanoseconds between |
| 665 | the 1st january 1601 and the 1st january 1970 (369 years + 89 leap |
| 666 | days). */ |
| 667 | *tp = large.QuadPart * 100 - 11644473600000000000; |
| 668 | if (info) { |
| 669 | DWORD timeAdjustment, timeIncrement; |
| 670 | BOOL isTimeAdjustmentDisabled, ok; |
| 671 | |
| 672 | info->implementation = "GetSystemTimeAsFileTime()"; |
| 673 | info->monotonic = 0; |
| 674 | ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement, |
| 675 | &isTimeAdjustmentDisabled); |
| 676 | if (!ok) { |
| 677 | PyErr_SetFromWindowsErr(0); |
| 678 | return -1; |
| 679 | } |
| 680 | info->resolution = timeIncrement * 1e-7; |
| 681 | info->adjustable = 1; |
| 682 | } |
| 683 | |
| 684 | #else /* MS_WINDOWS */ |
| 685 | int err; |
| 686 | #ifdef HAVE_CLOCK_GETTIME |
| 687 | struct timespec ts; |
| 688 | #else |
| 689 | struct timeval tv; |
| 690 | #endif |
| 691 | |
| 692 | assert(info == NULL || raise); |
| 693 | |
| 694 | #ifdef HAVE_CLOCK_GETTIME |
| 695 | err = clock_gettime(CLOCK_REALTIME, &ts); |
| 696 | if (err) { |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 697 | if (raise) { |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 698 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 699 | } |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 700 | return -1; |
| 701 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 702 | if (pytime_fromtimespec(tp, &ts, raise) < 0) { |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 703 | return -1; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 704 | } |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 705 | |
| 706 | if (info) { |
| 707 | struct timespec res; |
| 708 | info->implementation = "clock_gettime(CLOCK_REALTIME)"; |
| 709 | info->monotonic = 0; |
| 710 | info->adjustable = 1; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 711 | if (clock_getres(CLOCK_REALTIME, &res) == 0) { |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 712 | info->resolution = res.tv_sec + res.tv_nsec * 1e-9; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 713 | } |
| 714 | else { |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 715 | info->resolution = 1e-9; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 716 | } |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 717 | } |
| 718 | #else /* HAVE_CLOCK_GETTIME */ |
| 719 | |
| 720 | /* test gettimeofday() */ |
| 721 | #ifdef GETTIMEOFDAY_NO_TZ |
| 722 | err = gettimeofday(&tv); |
| 723 | #else |
| 724 | err = gettimeofday(&tv, (struct timezone *)NULL); |
| 725 | #endif |
| 726 | if (err) { |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 727 | if (raise) { |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 728 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 729 | } |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 730 | return -1; |
| 731 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 732 | if (pytime_fromtimeval(tp, &tv, raise) < 0) { |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 733 | return -1; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 734 | } |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 735 | |
| 736 | if (info) { |
| 737 | info->implementation = "gettimeofday()"; |
| 738 | info->resolution = 1e-6; |
| 739 | info->monotonic = 0; |
| 740 | info->adjustable = 1; |
| 741 | } |
| 742 | #endif /* !HAVE_CLOCK_GETTIME */ |
| 743 | #endif /* !MS_WINDOWS */ |
| 744 | return 0; |
| 745 | } |
| 746 | |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 747 | _PyTime_t |
| 748 | _PyTime_GetSystemClock(void) |
| 749 | { |
| 750 | _PyTime_t t; |
Victor Stinner | c379ade | 2015-11-10 12:11:39 +0100 | [diff] [blame] | 751 | if (pygettimeofday(&t, NULL, 0) < 0) { |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 752 | /* should not happen, _PyTime_Init() checked the clock at startup */ |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 753 | Py_UNREACHABLE(); |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 754 | } |
| 755 | return t; |
| 756 | } |
| 757 | |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 758 | int |
| 759 | _PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info) |
| 760 | { |
Victor Stinner | c379ade | 2015-11-10 12:11:39 +0100 | [diff] [blame] | 761 | return pygettimeofday(t, info, 1); |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 762 | } |
| 763 | |
Victor Stinner | a47b881 | 2015-03-27 18:16:17 +0100 | [diff] [blame] | 764 | static int |
Victor Stinner | 5ad5821 | 2015-09-03 00:14:58 +0200 | [diff] [blame] | 765 | pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 766 | { |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 767 | #if defined(MS_WINDOWS) |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 768 | ULONGLONG ticks; |
| 769 | _PyTime_t t; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 770 | |
| 771 | assert(info == NULL || raise); |
| 772 | |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 773 | ticks = GetTickCount64(); |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 774 | Py_BUILD_ASSERT(sizeof(ticks) <= sizeof(_PyTime_t)); |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 775 | t = (_PyTime_t)ticks; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 776 | |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 777 | if (_PyTime_check_mul_overflow(t, MS_TO_NS)) { |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 778 | if (raise) { |
| 779 | _PyTime_overflow(); |
| 780 | return -1; |
| 781 | } |
| 782 | /* Hello, time traveler! */ |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 783 | Py_UNREACHABLE(); |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 784 | } |
Victor Stinner | c60542b | 2015-09-10 15:55:07 +0200 | [diff] [blame] | 785 | *tp = t * MS_TO_NS; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 786 | |
| 787 | if (info) { |
| 788 | DWORD timeAdjustment, timeIncrement; |
| 789 | BOOL isTimeAdjustmentDisabled, ok; |
Victor Stinner | eb35229 | 2015-03-27 14:12:08 +0100 | [diff] [blame] | 790 | info->implementation = "GetTickCount64()"; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 791 | info->monotonic = 1; |
| 792 | ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement, |
| 793 | &isTimeAdjustmentDisabled); |
| 794 | if (!ok) { |
| 795 | PyErr_SetFromWindowsErr(0); |
| 796 | return -1; |
| 797 | } |
| 798 | info->resolution = timeIncrement * 1e-7; |
| 799 | info->adjustable = 0; |
| 800 | } |
| 801 | |
| 802 | #elif defined(__APPLE__) |
| 803 | static mach_timebase_info_data_t timebase; |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 804 | static uint64_t t0 = 0; |
| 805 | uint64_t ticks; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 806 | |
| 807 | if (timebase.denom == 0) { |
| 808 | /* According to the Technical Q&A QA1398, mach_timebase_info() cannot |
| 809 | fail: https://developer.apple.com/library/mac/#qa/qa1398/ */ |
| 810 | (void)mach_timebase_info(&timebase); |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 811 | |
| 812 | /* Sanity check: should never occur in practice */ |
| 813 | if (timebase.numer < 1 || timebase.denom < 1) { |
| 814 | PyErr_SetString(PyExc_RuntimeError, |
| 815 | "invalid mach_timebase_info"); |
| 816 | return -1; |
| 817 | } |
| 818 | |
| 819 | /* Check that timebase.numer and timebase.denom can be casted to |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 820 | _PyTime_t. In practice, timebase uses uint32_t, so casting cannot |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 821 | overflow. At the end, only make sure that the type is uint32_t |
| 822 | (_PyTime_t is 64-bit long). */ |
| 823 | assert(sizeof(timebase.numer) < sizeof(_PyTime_t)); |
| 824 | assert(sizeof(timebase.denom) < sizeof(_PyTime_t)); |
| 825 | |
| 826 | /* Make sure that (ticks * timebase.numer) cannot overflow in |
| 827 | _PyTime_MulDiv(), with ticks < timebase.denom. |
| 828 | |
| 829 | Known time bases: |
| 830 | |
| 831 | * always (1, 1) on Intel |
| 832 | * (1000000000, 33333335) or (1000000000, 25000000) on PowerPC |
| 833 | |
| 834 | None of these time bases can overflow with 64-bit _PyTime_t, but |
| 835 | check for overflow, just in case. */ |
| 836 | if ((_PyTime_t)timebase.numer > _PyTime_MAX / (_PyTime_t)timebase.denom) { |
| 837 | PyErr_SetString(PyExc_OverflowError, |
| 838 | "mach_timebase_info is too large"); |
| 839 | return -1; |
| 840 | } |
| 841 | |
| 842 | t0 = mach_absolute_time(); |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 843 | } |
| 844 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 845 | if (info) { |
| 846 | info->implementation = "mach_absolute_time()"; |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 847 | info->resolution = (double)timebase.numer / (double)timebase.denom * 1e-9; |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 848 | info->monotonic = 1; |
| 849 | info->adjustable = 0; |
| 850 | } |
| 851 | |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 852 | ticks = mach_absolute_time(); |
| 853 | /* Use a "time zero" to reduce precision loss when converting time |
| 854 | to floatting point number, as in time.monotonic(). */ |
| 855 | ticks -= t0; |
| 856 | *tp = _PyTime_MulDiv(ticks, |
| 857 | (_PyTime_t)timebase.numer, |
| 858 | (_PyTime_t)timebase.denom); |
| 859 | |
haney | c90e960 | 2017-06-21 11:18:21 -0700 | [diff] [blame] | 860 | #elif defined(__hpux) |
| 861 | hrtime_t time; |
| 862 | |
| 863 | time = gethrtime(); |
| 864 | if (time == -1) { |
| 865 | if (raise) { |
| 866 | PyErr_SetFromErrno(PyExc_OSError); |
| 867 | } |
| 868 | return -1; |
| 869 | } |
| 870 | |
| 871 | *tp = time; |
| 872 | |
| 873 | if (info) { |
| 874 | info->implementation = "gethrtime()"; |
| 875 | info->resolution = 1e-9; |
| 876 | info->monotonic = 1; |
| 877 | info->adjustable = 0; |
| 878 | } |
| 879 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 880 | #else |
| 881 | struct timespec ts; |
| 882 | #ifdef CLOCK_HIGHRES |
| 883 | const clockid_t clk_id = CLOCK_HIGHRES; |
| 884 | const char *implementation = "clock_gettime(CLOCK_HIGHRES)"; |
| 885 | #else |
| 886 | const clockid_t clk_id = CLOCK_MONOTONIC; |
| 887 | const char *implementation = "clock_gettime(CLOCK_MONOTONIC)"; |
| 888 | #endif |
| 889 | |
| 890 | assert(info == NULL || raise); |
| 891 | |
| 892 | if (clock_gettime(clk_id, &ts) != 0) { |
| 893 | if (raise) { |
| 894 | PyErr_SetFromErrno(PyExc_OSError); |
| 895 | return -1; |
| 896 | } |
| 897 | return -1; |
| 898 | } |
| 899 | |
| 900 | if (info) { |
| 901 | struct timespec res; |
| 902 | info->monotonic = 1; |
| 903 | info->implementation = implementation; |
| 904 | info->adjustable = 0; |
| 905 | if (clock_getres(clk_id, &res) != 0) { |
| 906 | PyErr_SetFromErrno(PyExc_OSError); |
| 907 | return -1; |
| 908 | } |
| 909 | info->resolution = res.tv_sec + res.tv_nsec * 1e-9; |
| 910 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 911 | if (pytime_fromtimespec(tp, &ts, raise) < 0) { |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 912 | return -1; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 913 | } |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 914 | #endif |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | _PyTime_t |
| 919 | _PyTime_GetMonotonicClock(void) |
| 920 | { |
| 921 | _PyTime_t t; |
Victor Stinner | 5ad5821 | 2015-09-03 00:14:58 +0200 | [diff] [blame] | 922 | if (pymonotonic(&t, NULL, 0) < 0) { |
Victor Stinner | cb0c602 | 2015-03-28 05:24:19 +0100 | [diff] [blame] | 923 | /* should not happen, _PyTime_Init() checked that monotonic clock at |
| 924 | startup */ |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 925 | Py_UNREACHABLE(); |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 926 | } |
| 927 | return t; |
| 928 | } |
| 929 | |
Victor Stinner | 0011124 | 2014-08-29 16:31:59 +0200 | [diff] [blame] | 930 | int |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 931 | _PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) |
| 932 | { |
Victor Stinner | 5ad5821 | 2015-09-03 00:14:58 +0200 | [diff] [blame] | 933 | return pymonotonic(tp, info, 1); |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 934 | } |
| 935 | |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 936 | |
| 937 | #ifdef MS_WINDOWS |
Victor Stinner | cba9a0c | 2017-10-12 08:51:56 -0700 | [diff] [blame] | 938 | static int |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 939 | win_perf_counter(_PyTime_t *tp, _Py_clock_info_t *info) |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 940 | { |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 941 | static LONGLONG frequency = 0; |
| 942 | static LONGLONG t0 = 0; |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 943 | LARGE_INTEGER now; |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 944 | LONGLONG ticksll; |
| 945 | _PyTime_t ticks; |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 946 | |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 947 | if (frequency == 0) { |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 948 | LARGE_INTEGER freq; |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 949 | if (!QueryPerformanceFrequency(&freq)) { |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 950 | PyErr_SetFromWindowsErr(0); |
| 951 | return -1; |
| 952 | } |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 953 | frequency = freq.QuadPart; |
| 954 | |
| 955 | /* Sanity check: should never occur in practice */ |
| 956 | if (frequency < 1) { |
| 957 | PyErr_SetString(PyExc_RuntimeError, |
| 958 | "invalid QueryPerformanceFrequency"); |
| 959 | return -1; |
| 960 | } |
| 961 | |
| 962 | /* Check that frequency can be casted to _PyTime_t. |
| 963 | |
| 964 | Make also sure that (ticks * SEC_TO_NS) cannot overflow in |
| 965 | _PyTime_MulDiv(), with ticks < frequency. |
| 966 | |
| 967 | Known QueryPerformanceFrequency() values: |
| 968 | |
| 969 | * 10,000,000 (10 MHz): 100 ns resolution |
| 970 | * 3,579,545 Hz (3.6 MHz): 279 ns resolution |
| 971 | |
| 972 | None of these frequencies can overflow with 64-bit _PyTime_t, but |
| 973 | check for overflow, just in case. */ |
| 974 | if (frequency > _PyTime_MAX |
| 975 | || frequency > (LONGLONG)_PyTime_MAX / (LONGLONG)SEC_TO_NS) { |
| 976 | PyErr_SetString(PyExc_OverflowError, |
| 977 | "QueryPerformanceFrequency is too large"); |
| 978 | return -1; |
| 979 | } |
| 980 | |
| 981 | QueryPerformanceCounter(&now); |
| 982 | t0 = now.QuadPart; |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 983 | } |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 984 | |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 985 | if (info) { |
| 986 | info->implementation = "QueryPerformanceCounter()"; |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 987 | info->resolution = 1.0 / (double)frequency; |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 988 | info->monotonic = 1; |
| 989 | info->adjustable = 0; |
| 990 | } |
| 991 | |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 992 | QueryPerformanceCounter(&now); |
| 993 | ticksll = now.QuadPart; |
| 994 | |
| 995 | /* Use a "time zero" to reduce precision loss when converting time |
| 996 | to floatting point number, as in time.perf_counter(). */ |
| 997 | ticksll -= t0; |
| 998 | |
| 999 | /* Make sure that casting LONGLONG to _PyTime_t cannot overflow, |
| 1000 | both types are signed */ |
| 1001 | Py_BUILD_ASSERT(sizeof(ticksll) <= sizeof(ticks)); |
| 1002 | ticks = (_PyTime_t)ticksll; |
| 1003 | |
| 1004 | *tp = _PyTime_MulDiv(ticks, SEC_TO_NS, (_PyTime_t)frequency); |
Victor Stinner | cba9a0c | 2017-10-12 08:51:56 -0700 | [diff] [blame] | 1005 | return 0; |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 1006 | } |
| 1007 | #endif |
| 1008 | |
| 1009 | |
| 1010 | int |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 1011 | _PyTime_GetPerfCounterWithInfo(_PyTime_t *t, _Py_clock_info_t *info) |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 1012 | { |
| 1013 | #ifdef MS_WINDOWS |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 1014 | return win_perf_counter(t, info); |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 1015 | #else |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 1016 | return _PyTime_GetMonotonicClockWithInfo(t, info); |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 1017 | #endif |
| 1018 | } |
| 1019 | |
| 1020 | |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 1021 | _PyTime_t |
| 1022 | _PyTime_GetPerfCounter(void) |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 1023 | { |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 1024 | _PyTime_t t; |
| 1025 | if (_PyTime_GetPerfCounterWithInfo(&t, NULL)) { |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 1026 | Py_UNREACHABLE(); |
| 1027 | } |
| 1028 | return t; |
| 1029 | } |
| 1030 | |
| 1031 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 1032 | int |
Victor Stinner | 0011124 | 2014-08-29 16:31:59 +0200 | [diff] [blame] | 1033 | _PyTime_Init(void) |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 1034 | { |
Victor Stinner | cba9a0c | 2017-10-12 08:51:56 -0700 | [diff] [blame] | 1035 | /* check that time.time(), time.monotonic() and time.perf_counter() clocks |
| 1036 | are working properly to not have to check for exceptions at runtime. If |
| 1037 | a clock works once, it cannot fail in next calls. */ |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 1038 | _PyTime_t t; |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 1039 | if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0) { |
Victor Stinner | 0011124 | 2014-08-29 16:31:59 +0200 | [diff] [blame] | 1040 | return -1; |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 1041 | } |
| 1042 | if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0) { |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 1043 | return -1; |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 1044 | } |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 1045 | if (_PyTime_GetPerfCounterWithInfo(&t, NULL) < 0) { |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 1046 | return -1; |
| 1047 | } |
Victor Stinner | 0011124 | 2014-08-29 16:31:59 +0200 | [diff] [blame] | 1048 | return 0; |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 1049 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 1050 | |
| 1051 | int |
| 1052 | _PyTime_localtime(time_t t, struct tm *tm) |
| 1053 | { |
| 1054 | #ifdef MS_WINDOWS |
| 1055 | int error; |
| 1056 | |
| 1057 | error = localtime_s(tm, &t); |
| 1058 | if (error != 0) { |
| 1059 | errno = error; |
| 1060 | PyErr_SetFromErrno(PyExc_OSError); |
| 1061 | return -1; |
| 1062 | } |
| 1063 | return 0; |
| 1064 | #else /* !MS_WINDOWS */ |
Michael Felt | e2926b7 | 2018-12-28 14:57:37 +0100 | [diff] [blame] | 1065 | #ifdef _AIX |
| 1066 | /* AIX does not return NULL on an error |
| 1067 | so test ranges - asif! |
| 1068 | (1902-01-01, -2145916800.0) |
| 1069 | (2038-01-01, 2145916800.0) */ |
| 1070 | if (abs(t) > (time_t) 2145916800) { |
| 1071 | #ifdef EINVAL |
| 1072 | errno = EINVAL; |
| 1073 | #endif |
| 1074 | PyErr_SetString(PyExc_OverflowError, |
| 1075 | "ctime argument out of range"); |
| 1076 | return -1; |
| 1077 | } |
| 1078 | #endif |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 1079 | if (localtime_r(&t, tm) == NULL) { |
| 1080 | #ifdef EINVAL |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 1081 | if (errno == 0) { |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 1082 | errno = EINVAL; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 1083 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 1084 | #endif |
| 1085 | PyErr_SetFromErrno(PyExc_OSError); |
| 1086 | return -1; |
| 1087 | } |
| 1088 | return 0; |
| 1089 | #endif /* MS_WINDOWS */ |
| 1090 | } |
| 1091 | |
| 1092 | int |
| 1093 | _PyTime_gmtime(time_t t, struct tm *tm) |
| 1094 | { |
| 1095 | #ifdef MS_WINDOWS |
| 1096 | int error; |
| 1097 | |
| 1098 | error = gmtime_s(tm, &t); |
| 1099 | if (error != 0) { |
| 1100 | errno = error; |
| 1101 | PyErr_SetFromErrno(PyExc_OSError); |
| 1102 | return -1; |
| 1103 | } |
| 1104 | return 0; |
| 1105 | #else /* !MS_WINDOWS */ |
| 1106 | if (gmtime_r(&t, tm) == NULL) { |
| 1107 | #ifdef EINVAL |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 1108 | if (errno == 0) { |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 1109 | errno = EINVAL; |
Victor Stinner | 277c840 | 2017-10-11 08:11:38 -0700 | [diff] [blame] | 1110 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 1111 | #endif |
| 1112 | PyErr_SetFromErrno(PyExc_OSError); |
| 1113 | return -1; |
| 1114 | } |
| 1115 | return 0; |
| 1116 | #endif /* MS_WINDOWS */ |
| 1117 | } |