blob: f19bb3673e473ff1fce3c76e8a22f0f400e5de76 [file] [log] [blame]
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +00001#include "Python.h"
Victor Stinner09225b72012-02-07 23:41:01 +01002#ifdef MS_WINDOWS
3#include <windows.h>
4#endif
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +00005
Victor Stinnerae586492014-09-02 23:18:25 +02006#if defined(__APPLE__)
7#include <mach/mach_time.h> /* mach_absolute_time(), mach_timebase_info() */
8#endif
9
Victor Stinnerc60542b2015-09-10 15:55:07 +020010#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 Stinnercb29f012015-03-27 13:31:18 +010015/* To millisecond (10^-3) */
Victor Stinner580ef132015-03-20 01:55:04 +010016#define SEC_TO_MS 1000
Victor Stinner580ef132015-03-20 01:55:04 +010017
Victor Stinnercb29f012015-03-27 13:31:18 +010018/* To microseconds (10^-6) */
19#define MS_TO_US 1000
Victor Stinner580ef132015-03-20 01:55:04 +010020#define SEC_TO_US (SEC_TO_MS * MS_TO_US)
21
Victor Stinnercb29f012015-03-27 13:31:18 +010022/* 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 Stinner62d1c702015-04-01 17:47:07 +020027/* Conversion from nanoseconds */
28#define NS_TO_MS (1000 * 1000)
29#define NS_TO_US (1000)
30
Victor Stinner5d272cc2012-03-13 13:35:55 +010031static void
32error_time_t_overflow(void)
Victor Stinner643cd682012-03-02 22:54:03 +010033{
Victor Stinner5d272cc2012-03-13 13:35:55 +010034 PyErr_SetString(PyExc_OverflowError,
35 "timestamp out of range for platform time_t");
36}
37
Victor Stinner277c8402017-10-11 08:11:38 -070038static void
39_PyTime_overflow(void)
40{
41 PyErr_SetString(PyExc_OverflowError,
42 "timestamp too large to convert to C _PyTime_t");
43}
44
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -070045
46#if defined(MS_WINDOWS) || defined(__APPLE__)
47Py_LOCAL_INLINE(_PyTime_t)
48_PyTime_MulDiv(_PyTime_t ticks, _PyTime_t mul, _PyTime_t div)
49{
50 _PyTime_t intpart, remaining;
51 /* Compute (ticks * mul / div) in two parts to prevent integer overflow:
52 compute integer part, and then the remaining part.
53
54 (ticks * mul) / div == (ticks / div) * mul + (ticks % div) * mul / div
55
56 The caller must ensure that "(div - 1) * mul" cannot overflow. */
57 intpart = ticks / div;
58 ticks %= div;
59 remaining = ticks * mul;
60 remaining /= div;
61 return intpart * mul + remaining;
62}
63#endif /* defined(MS_WINDOWS) || defined(__APPLE__) */
64
65
Larry Hastings76ad59b2012-05-03 00:30:07 -070066time_t
Victor Stinner5d272cc2012-03-13 13:35:55 +010067_PyLong_AsTime_t(PyObject *obj)
68{
Benjamin Petersoned4aa832016-09-05 17:44:18 -070069#if SIZEOF_TIME_T == SIZEOF_LONG_LONG
Benjamin Petersonaf580df2016-09-06 10:46:49 -070070 long long val;
Victor Stinner5d272cc2012-03-13 13:35:55 +010071 val = PyLong_AsLongLong(obj);
72#else
73 long val;
Serhiy Storchakafad85aa2015-11-07 15:42:38 +020074 Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
Victor Stinner5d272cc2012-03-13 13:35:55 +010075 val = PyLong_AsLong(obj);
76#endif
77 if (val == -1 && PyErr_Occurred()) {
Victor Stinner277c8402017-10-11 08:11:38 -070078 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +010079 error_time_t_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -070080 }
Victor Stinner5d272cc2012-03-13 13:35:55 +010081 return -1;
82 }
83 return (time_t)val;
84}
85
Larry Hastings6fe20b32012-04-19 15:07:49 -070086PyObject *
87_PyLong_FromTime_t(time_t t)
88{
Benjamin Petersoned4aa832016-09-05 17:44:18 -070089#if SIZEOF_TIME_T == SIZEOF_LONG_LONG
Benjamin Petersonaf580df2016-09-06 10:46:49 -070090 return PyLong_FromLongLong((long long)t);
Larry Hastings6fe20b32012-04-19 15:07:49 -070091#else
Serhiy Storchakafad85aa2015-11-07 15:42:38 +020092 Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
Larry Hastings6fe20b32012-04-19 15:07:49 -070093 return PyLong_FromLong((long)t);
94#endif
95}
96
Victor Stinnerce6aa742015-09-09 22:28:09 +020097/* Round to nearest with ties going to nearest even integer
98 (_PyTime_ROUND_HALF_EVEN) */
99static double
Victor Stinner7667f582015-09-09 01:02:23 +0200100_PyTime_RoundHalfEven(double x)
Victor Stinner74474232015-09-02 01:43:56 +0200101{
Victor Stinner7667f582015-09-09 01:02:23 +0200102 double rounded = round(x);
Victor Stinner277c8402017-10-11 08:11:38 -0700103 if (fabs(x-rounded) == 0.5) {
Victor Stinner7667f582015-09-09 01:02:23 +0200104 /* halfway case: round to even */
105 rounded = 2.0*round(x/2.0);
Victor Stinner277c8402017-10-11 08:11:38 -0700106 }
Victor Stinner7667f582015-09-09 01:02:23 +0200107 return rounded;
Victor Stinner74474232015-09-02 01:43:56 +0200108}
109
Victor Stinner9ae47df2015-09-09 22:28:58 +0200110static double
111_PyTime_Round(double x, _PyTime_round_t round)
112{
Victor Stinner1efbeba2015-09-10 11:48:00 +0200113 /* volatile avoids optimization changing how numbers are rounded */
114 volatile double d;
115
116 d = x;
Victor Stinner277c8402017-10-11 08:11:38 -0700117 if (round == _PyTime_ROUND_HALF_EVEN) {
Victor Stinner1efbeba2015-09-10 11:48:00 +0200118 d = _PyTime_RoundHalfEven(d);
Victor Stinner277c8402017-10-11 08:11:38 -0700119 }
120 else if (round == _PyTime_ROUND_CEILING) {
Victor Stinner1efbeba2015-09-10 11:48:00 +0200121 d = ceil(d);
Victor Stinner277c8402017-10-11 08:11:38 -0700122 }
Pablo Galindo2c15b292017-10-17 15:14:41 +0100123 else if (round == _PyTime_ROUND_FLOOR) {
Victor Stinner1efbeba2015-09-10 11:48:00 +0200124 d = floor(d);
Victor Stinner277c8402017-10-11 08:11:38 -0700125 }
Pablo Galindo2c15b292017-10-17 15:14:41 +0100126 else {
127 assert(round == _PyTime_ROUND_UP);
128 d = (d >= 0.0) ? ceil(d) : floor(d);
129 }
Victor Stinner1efbeba2015-09-10 11:48:00 +0200130 return d;
Victor Stinner9ae47df2015-09-09 22:28:58 +0200131}
132
Victor Stinner5d272cc2012-03-13 13:35:55 +0100133static int
Victor Stinner53e137c2015-09-02 00:49:16 +0200134_PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
Victor Stinner277c8402017-10-11 08:11:38 -0700135 long idenominator, _PyTime_round_t round)
Victor Stinner53e137c2015-09-02 00:49:16 +0200136{
Victor Stinner277c8402017-10-11 08:11:38 -0700137 double denominator = (double)idenominator;
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700138 double intpart;
Victor Stinner24b822e2015-09-02 11:58:56 +0200139 /* volatile avoids optimization changing how numbers are rounded */
Victor Stinner53e137c2015-09-02 00:49:16 +0200140 volatile double floatpart;
141
142 floatpart = modf(d, &intpart);
Victor Stinner53e137c2015-09-02 00:49:16 +0200143
144 floatpart *= denominator;
Victor Stinner9ae47df2015-09-09 22:28:58 +0200145 floatpart = _PyTime_Round(floatpart, round);
Victor Stinner67edcc92015-09-02 10:37:46 +0200146 if (floatpart >= denominator) {
147 floatpart -= denominator;
148 intpart += 1.0;
Victor Stinner53e137c2015-09-02 00:49:16 +0200149 }
Victor Stinneradfefa52015-09-04 23:57:25 +0200150 else if (floatpart < 0) {
151 floatpart += denominator;
152 intpart -= 1.0;
153 }
Victor Stinner67edcc92015-09-02 10:37:46 +0200154 assert(0.0 <= floatpart && floatpart < denominator);
Victor Stinner53e137c2015-09-02 00:49:16 +0200155
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700156 if (!_Py_InIntegralTypeRange(time_t, intpart)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200157 error_time_t_overflow();
158 return -1;
159 }
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700160 *sec = (time_t)intpart;
161 *numerator = (long)floatpart;
Victor Stinner277c8402017-10-11 08:11:38 -0700162 assert(0 <= *numerator && *numerator < idenominator);
Victor Stinner53e137c2015-09-02 00:49:16 +0200163 return 0;
164}
165
166static int
Victor Stinner5d272cc2012-03-13 13:35:55 +0100167_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
Victor Stinner277c8402017-10-11 08:11:38 -0700168 long denominator, _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100169{
Victor Stinner277c8402017-10-11 08:11:38 -0700170 assert(denominator >= 1);
Victor Stinnerbbdda212015-09-02 00:50:43 +0200171
Victor Stinner643cd682012-03-02 22:54:03 +0100172 if (PyFloat_Check(obj)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200173 double d = PyFloat_AsDouble(obj);
Han Lee829dacc2017-09-09 08:05:05 +0900174 if (Py_IS_NAN(d)) {
175 *numerator = 0;
176 PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
177 return -1;
178 }
Victor Stinner53e137c2015-09-02 00:49:16 +0200179 return _PyTime_DoubleToDenominator(d, sec, numerator,
180 denominator, round);
Victor Stinner643cd682012-03-02 22:54:03 +0100181 }
182 else {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100183 *sec = _PyLong_AsTime_t(obj);
Victor Stinner67edcc92015-09-02 10:37:46 +0200184 *numerator = 0;
Victor Stinner277c8402017-10-11 08:11:38 -0700185 if (*sec == (time_t)-1 && PyErr_Occurred()) {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100186 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700187 }
Victor Stinner643cd682012-03-02 22:54:03 +0100188 return 0;
189 }
Victor Stinner5d272cc2012-03-13 13:35:55 +0100190}
Victor Stinner643cd682012-03-02 22:54:03 +0100191
Victor Stinner5d272cc2012-03-13 13:35:55 +0100192int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100193_PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100194{
195 if (PyFloat_Check(obj)) {
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700196 double intpart;
Victor Stinner24b822e2015-09-02 11:58:56 +0200197 /* volatile avoids optimization changing how numbers are rounded */
Victor Stinner5786aef2015-09-03 16:33:16 +0200198 volatile double d;
Victor Stinner5d272cc2012-03-13 13:35:55 +0100199
Victor Stinner5d272cc2012-03-13 13:35:55 +0100200 d = PyFloat_AsDouble(obj);
Han Lee829dacc2017-09-09 08:05:05 +0900201 if (Py_IS_NAN(d)) {
202 PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
203 return -1;
204 }
205
Victor Stinner9ae47df2015-09-09 22:28:58 +0200206 d = _PyTime_Round(d, round);
Victor Stinner5d272cc2012-03-13 13:35:55 +0100207 (void)modf(d, &intpart);
208
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700209 if (!_Py_InIntegralTypeRange(time_t, intpart)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100210 error_time_t_overflow();
211 return -1;
212 }
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700213 *sec = (time_t)intpart;
Victor Stinner5d272cc2012-03-13 13:35:55 +0100214 return 0;
215 }
216 else {
217 *sec = _PyLong_AsTime_t(obj);
Victor Stinner277c8402017-10-11 08:11:38 -0700218 if (*sec == (time_t)-1 && PyErr_Occurred()) {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100219 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700220 }
Victor Stinner5d272cc2012-03-13 13:35:55 +0100221 return 0;
222 }
223}
224
225int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100226_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
227 _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100228{
Victor Stinner277c8402017-10-11 08:11:38 -0700229 return _PyTime_ObjectToDenominator(obj, sec, nsec, SEC_TO_NS, round);
Victor Stinner5d272cc2012-03-13 13:35:55 +0100230}
231
232int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100233_PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
234 _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100235{
Victor Stinner277c8402017-10-11 08:11:38 -0700236 return _PyTime_ObjectToDenominator(obj, sec, usec, SEC_TO_US, round);
Victor Stinnercb29f012015-03-27 13:31:18 +0100237}
238
Victor Stinner4bfb4602015-03-27 22:27:24 +0100239_PyTime_t
Victor Stinner13019fd2015-04-03 13:10:54 +0200240_PyTime_FromSeconds(int seconds)
241{
242 _PyTime_t t;
243 /* ensure that integer overflow cannot happen, int type should have 32
244 bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
245 bits). */
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200246 Py_BUILD_ASSERT(INT_MAX <= _PyTime_MAX / SEC_TO_NS);
247 Py_BUILD_ASSERT(INT_MIN >= _PyTime_MIN / SEC_TO_NS);
Victor Stinner277c8402017-10-11 08:11:38 -0700248
249 t = (_PyTime_t)seconds;
Victor Stinnerbbdda212015-09-02 00:50:43 +0200250 assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
251 || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
252 t *= SEC_TO_NS;
Victor Stinner13019fd2015-04-03 13:10:54 +0200253 return t;
254}
255
256_PyTime_t
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700257_PyTime_FromNanoseconds(long long ns)
Victor Stinner4bfb4602015-03-27 22:27:24 +0100258{
259 _PyTime_t t;
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700260 Py_BUILD_ASSERT(sizeof(long long) <= sizeof(_PyTime_t));
261 t = Py_SAFE_DOWNCAST(ns, long long, _PyTime_t);
Victor Stinner4bfb4602015-03-27 22:27:24 +0100262 return t;
263}
264
Victor Stinnera47b8812015-03-27 18:16:17 +0100265#ifdef HAVE_CLOCK_GETTIME
Victor Stinnercb29f012015-03-27 13:31:18 +0100266static int
Victor Stinnercb0c6022015-03-28 05:24:19 +0100267_PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100268{
269 _PyTime_t t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100270 int res = 0;
271
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200272 Py_BUILD_ASSERT(sizeof(ts->tv_sec) <= sizeof(_PyTime_t));
Victor Stinnerc60542b2015-09-10 15:55:07 +0200273 t = (_PyTime_t)ts->tv_sec;
274
275 if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
Victor Stinner277c8402017-10-11 08:11:38 -0700276 if (raise) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100277 _PyTime_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -0700278 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100279 res = -1;
Victor Stinnercb29f012015-03-27 13:31:18 +0100280 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200281 t = t * SEC_TO_NS;
Victor Stinnercb29f012015-03-27 13:31:18 +0100282
283 t += ts->tv_nsec;
284
285 *tp = t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100286 return res;
Victor Stinnercb29f012015-03-27 13:31:18 +0100287}
Victor Stinner1bd18ba2015-03-30 00:25:38 +0200288#elif !defined(MS_WINDOWS)
Victor Stinnera47b8812015-03-27 18:16:17 +0100289static int
Victor Stinnercb0c6022015-03-28 05:24:19 +0100290_PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
Victor Stinnera47b8812015-03-27 18:16:17 +0100291{
292 _PyTime_t t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100293 int res = 0;
Victor Stinnera47b8812015-03-27 18:16:17 +0100294
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200295 Py_BUILD_ASSERT(sizeof(tv->tv_sec) <= sizeof(_PyTime_t));
Victor Stinnerc60542b2015-09-10 15:55:07 +0200296 t = (_PyTime_t)tv->tv_sec;
297
298 if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
Victor Stinner277c8402017-10-11 08:11:38 -0700299 if (raise) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100300 _PyTime_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -0700301 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100302 res = -1;
Victor Stinnera47b8812015-03-27 18:16:17 +0100303 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200304 t = t * SEC_TO_NS;
Victor Stinnera47b8812015-03-27 18:16:17 +0100305
306 t += (_PyTime_t)tv->tv_usec * US_TO_NS;
307
308 *tp = t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100309 return res;
Victor Stinnera47b8812015-03-27 18:16:17 +0100310}
Victor Stinnercb29f012015-03-27 13:31:18 +0100311#endif
312
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200313static int
Victor Stinnera997c7b2017-10-10 02:51:50 -0700314_PyTime_FromDouble(_PyTime_t *t, double value, _PyTime_round_t round,
315 long unit_to_ns)
Victor Stinner53e137c2015-09-02 00:49:16 +0200316{
Victor Stinner24b822e2015-09-02 11:58:56 +0200317 /* volatile avoids optimization changing how numbers are rounded */
Victor Stinner5786aef2015-09-03 16:33:16 +0200318 volatile double d;
Victor Stinner53e137c2015-09-02 00:49:16 +0200319
320 /* convert to a number of nanoseconds */
321 d = value;
Victor Stinner9ae47df2015-09-09 22:28:58 +0200322 d *= (double)unit_to_ns;
323 d = _PyTime_Round(d, round);
Victor Stinner53e137c2015-09-02 00:49:16 +0200324
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700325 if (!_Py_InIntegralTypeRange(_PyTime_t, d)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200326 _PyTime_overflow();
327 return -1;
328 }
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700329 *t = (_PyTime_t)d;
Victor Stinner53e137c2015-09-02 00:49:16 +0200330 return 0;
331}
332
333static int
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200334_PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
Victor Stinner9ae47df2015-09-09 22:28:58 +0200335 long unit_to_ns)
Victor Stinnercb29f012015-03-27 13:31:18 +0100336{
337 if (PyFloat_Check(obj)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200338 double d;
Victor Stinnercb29f012015-03-27 13:31:18 +0100339 d = PyFloat_AsDouble(obj);
Han Lee829dacc2017-09-09 08:05:05 +0900340 if (Py_IS_NAN(d)) {
341 PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
342 return -1;
343 }
Victor Stinnera997c7b2017-10-10 02:51:50 -0700344 return _PyTime_FromDouble(t, d, round, unit_to_ns);
Victor Stinnercb29f012015-03-27 13:31:18 +0100345 }
346 else {
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700347 long long sec;
348 Py_BUILD_ASSERT(sizeof(long long) <= sizeof(_PyTime_t));
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200349
350 sec = PyLong_AsLongLong(obj);
Victor Stinnercb29f012015-03-27 13:31:18 +0100351 if (sec == -1 && PyErr_Occurred()) {
Victor Stinner277c8402017-10-11 08:11:38 -0700352 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100353 _PyTime_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -0700354 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100355 return -1;
356 }
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200357
Victor Stinnerc60542b2015-09-10 15:55:07 +0200358 if (_PyTime_check_mul_overflow(sec, unit_to_ns)) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100359 _PyTime_overflow();
360 return -1;
361 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200362 *t = sec * unit_to_ns;
Victor Stinnercb29f012015-03-27 13:31:18 +0100363 return 0;
364 }
365}
366
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200367int
368_PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
369{
370 return _PyTime_FromObject(t, obj, round, SEC_TO_NS);
371}
372
373int
374_PyTime_FromMillisecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
375{
376 return _PyTime_FromObject(t, obj, round, MS_TO_NS);
377}
378
Victor Stinner4bfb4602015-03-27 22:27:24 +0100379double
380_PyTime_AsSecondsDouble(_PyTime_t t)
381{
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200382 /* volatile avoids optimization changing how numbers are rounded */
383 volatile double d;
384
Victor Stinner3e2c8d82015-09-09 22:32:48 +0200385 if (t % SEC_TO_NS == 0) {
386 _PyTime_t secs;
387 /* Divide using integers to avoid rounding issues on the integer part.
388 1e-9 cannot be stored exactly in IEEE 64-bit. */
389 secs = t / SEC_TO_NS;
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200390 d = (double)secs;
Victor Stinner3e2c8d82015-09-09 22:32:48 +0200391 }
392 else {
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200393 d = (double)t;
394 d /= 1e9;
Victor Stinner3e2c8d82015-09-09 22:32:48 +0200395 }
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200396 return d;
Victor Stinner4bfb4602015-03-27 22:27:24 +0100397}
398
Victor Stinner992c43f2015-03-27 17:12:45 +0100399PyObject *
400_PyTime_AsNanosecondsObject(_PyTime_t t)
401{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700402 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(_PyTime_t));
403 return PyLong_FromLongLong((long long)t);
Victor Stinner992c43f2015-03-27 17:12:45 +0100404}
405
Victor Stinnercb29f012015-03-27 13:31:18 +0100406static _PyTime_t
Victor Stinner7667f582015-09-09 01:02:23 +0200407_PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
408 const _PyTime_round_t round)
Victor Stinnercb29f012015-03-27 13:31:18 +0100409{
Victor Stinner62d1c702015-04-01 17:47:07 +0200410 assert(k > 1);
Victor Stinner7667f582015-09-09 01:02:23 +0200411 if (round == _PyTime_ROUND_HALF_EVEN) {
412 _PyTime_t x, r, abs_r;
Victor Stinner74474232015-09-02 01:43:56 +0200413 x = t / k;
414 r = t % k;
Victor Stinner7667f582015-09-09 01:02:23 +0200415 abs_r = Py_ABS(r);
416 if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) {
Victor Stinner277c8402017-10-11 08:11:38 -0700417 if (t >= 0) {
Victor Stinner74474232015-09-02 01:43:56 +0200418 x++;
Victor Stinner277c8402017-10-11 08:11:38 -0700419 }
420 else {
Victor Stinner74474232015-09-02 01:43:56 +0200421 x--;
Victor Stinner277c8402017-10-11 08:11:38 -0700422 }
Victor Stinner74474232015-09-02 01:43:56 +0200423 }
424 return x;
425 }
426 else if (round == _PyTime_ROUND_CEILING) {
Victor Stinner277c8402017-10-11 08:11:38 -0700427 if (t >= 0) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100428 return (t + k - 1) / k;
Victor Stinner277c8402017-10-11 08:11:38 -0700429 }
430 else {
Victor Stinner7667f582015-09-09 01:02:23 +0200431 return t / k;
Victor Stinner277c8402017-10-11 08:11:38 -0700432 }
Victor Stinner7667f582015-09-09 01:02:23 +0200433 }
Pablo Galindo2c15b292017-10-17 15:14:41 +0100434 else if (round == _PyTime_ROUND_FLOOR){
Victor Stinner277c8402017-10-11 08:11:38 -0700435 if (t >= 0) {
Victor Stinner7667f582015-09-09 01:02:23 +0200436 return t / k;
Victor Stinner277c8402017-10-11 08:11:38 -0700437 }
438 else {
Victor Stinner62d1c702015-04-01 17:47:07 +0200439 return (t - (k - 1)) / k;
Victor Stinner277c8402017-10-11 08:11:38 -0700440 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100441 }
Pablo Galindo2c15b292017-10-17 15:14:41 +0100442 else {
443 assert(round == _PyTime_ROUND_UP);
444 if (t >= 0) {
445 return (t + k - 1) / k;
446 }
447 else {
448 return (t - (k - 1)) / k;
449 }
450 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100451}
452
453_PyTime_t
454_PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
455{
Victor Stinner62d1c702015-04-01 17:47:07 +0200456 return _PyTime_Divide(t, NS_TO_MS, round);
Victor Stinnercb29f012015-03-27 13:31:18 +0100457}
458
Victor Stinnerf5faad22015-03-28 03:52:05 +0100459_PyTime_t
460_PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
461{
Victor Stinner62d1c702015-04-01 17:47:07 +0200462 return _PyTime_Divide(t, NS_TO_US, round);
Victor Stinnerf5faad22015-03-28 03:52:05 +0100463}
464
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200465static int
Victor Stinner1e2b6882015-09-18 13:23:02 +0200466_PyTime_AsTimeval_impl(_PyTime_t t, _PyTime_t *p_secs, int *p_us,
467 _PyTime_round_t round)
Victor Stinnercb29f012015-03-27 13:31:18 +0100468{
469 _PyTime_t secs, ns;
Victor Stinner74474232015-09-02 01:43:56 +0200470 int usec;
Victor Stinner1e2b6882015-09-18 13:23:02 +0200471 int res = 0;
Victor Stinnercb29f012015-03-27 13:31:18 +0100472
473 secs = t / SEC_TO_NS;
474 ns = t % SEC_TO_NS;
475
Victor Stinner1e2b6882015-09-18 13:23:02 +0200476 usec = (int)_PyTime_Divide(ns, US_TO_NS, round);
477 if (usec < 0) {
478 usec += SEC_TO_US;
Victor Stinner277c8402017-10-11 08:11:38 -0700479 if (secs != _PyTime_MIN) {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200480 secs -= 1;
Victor Stinner277c8402017-10-11 08:11:38 -0700481 }
482 else {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200483 res = -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700484 }
Victor Stinner1e2b6882015-09-18 13:23:02 +0200485 }
486 else if (usec >= SEC_TO_US) {
487 usec -= SEC_TO_US;
Victor Stinner277c8402017-10-11 08:11:38 -0700488 if (secs != _PyTime_MAX) {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200489 secs += 1;
Victor Stinner277c8402017-10-11 08:11:38 -0700490 }
491 else {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200492 res = -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700493 }
Victor Stinner1e2b6882015-09-18 13:23:02 +0200494 }
495 assert(0 <= usec && usec < SEC_TO_US);
496
497 *p_secs = secs;
498 *p_us = usec;
499
500 return res;
501}
502
503static int
504_PyTime_AsTimevalStruct_impl(_PyTime_t t, struct timeval *tv,
505 _PyTime_round_t round, int raise)
506{
Victor Stinnerb7a8af22015-10-01 08:44:03 +0200507 _PyTime_t secs, secs2;
Victor Stinner1e2b6882015-09-18 13:23:02 +0200508 int us;
509 int res;
510
511 res = _PyTime_AsTimeval_impl(t, &secs, &us, round);
512
Victor Stinnercb29f012015-03-27 13:31:18 +0100513#ifdef MS_WINDOWS
Victor Stinnercb29f012015-03-27 13:31:18 +0100514 tv->tv_sec = (long)secs;
515#else
Victor Stinnercb29f012015-03-27 13:31:18 +0100516 tv->tv_sec = secs;
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200517#endif
Victor Stinner1e2b6882015-09-18 13:23:02 +0200518 tv->tv_usec = us;
Victor Stinnercb29f012015-03-27 13:31:18 +0100519
Victor Stinnerb7a8af22015-10-01 08:44:03 +0200520 secs2 = (_PyTime_t)tv->tv_sec;
521 if (res < 0 || secs2 != secs) {
Victor Stinner277c8402017-10-11 08:11:38 -0700522 if (raise) {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200523 error_time_t_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -0700524 }
Victor Stinner1e2b6882015-09-18 13:23:02 +0200525 return -1;
Victor Stinner74474232015-09-02 01:43:56 +0200526 }
Victor Stinner1e2b6882015-09-18 13:23:02 +0200527 return 0;
Victor Stinnercb29f012015-03-27 13:31:18 +0100528}
529
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200530int
531_PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
532{
Victor Stinner1e2b6882015-09-18 13:23:02 +0200533 return _PyTime_AsTimevalStruct_impl(t, tv, round, 1);
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200534}
535
536int
537_PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
538{
Victor Stinner1e2b6882015-09-18 13:23:02 +0200539 return _PyTime_AsTimevalStruct_impl(t, tv, round, 0);
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200540}
541
Victor Stinner1e2b6882015-09-18 13:23:02 +0200542int
543_PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us,
544 _PyTime_round_t round)
545{
546 _PyTime_t secs;
547 int res;
548
549 res = _PyTime_AsTimeval_impl(t, &secs, us, round);
550
551 *p_secs = secs;
552
553 if (res < 0 || (_PyTime_t)*p_secs != secs) {
554 error_time_t_overflow();
555 return -1;
556 }
557 return 0;
558}
559
560
Victor Stinnerc3378382015-03-28 05:07:51 +0100561#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
Victor Stinner34dc0f42015-03-27 18:19:03 +0100562int
563_PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
564{
Victor Stinner95e9cef2015-03-28 01:26:47 +0100565 _PyTime_t secs, nsec;
566
567 secs = t / SEC_TO_NS;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100568 nsec = t % SEC_TO_NS;
569 if (nsec < 0) {
570 nsec += SEC_TO_NS;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100571 secs -= 1;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100572 }
Victor Stinner95e9cef2015-03-28 01:26:47 +0100573 ts->tv_sec = (time_t)secs;
Victor Stinner29ee6742015-09-03 16:25:45 +0200574 assert(0 <= nsec && nsec < SEC_TO_NS);
575 ts->tv_nsec = nsec;
576
Victor Stinner95e9cef2015-03-28 01:26:47 +0100577 if ((_PyTime_t)ts->tv_sec != secs) {
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200578 error_time_t_overflow();
Victor Stinner34dc0f42015-03-27 18:19:03 +0100579 return -1;
580 }
Victor Stinner34dc0f42015-03-27 18:19:03 +0100581 return 0;
582}
583#endif
584
Victor Stinnercb29f012015-03-27 13:31:18 +0100585static int
Victor Stinnerc379ade2015-11-10 12:11:39 +0100586pygettimeofday(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
Victor Stinnera47b8812015-03-27 18:16:17 +0100587{
588#ifdef MS_WINDOWS
589 FILETIME system_time;
590 ULARGE_INTEGER large;
591
592 assert(info == NULL || raise);
593
594 GetSystemTimeAsFileTime(&system_time);
595 large.u.LowPart = system_time.dwLowDateTime;
596 large.u.HighPart = system_time.dwHighDateTime;
597 /* 11,644,473,600,000,000,000: number of nanoseconds between
598 the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
599 days). */
600 *tp = large.QuadPart * 100 - 11644473600000000000;
601 if (info) {
602 DWORD timeAdjustment, timeIncrement;
603 BOOL isTimeAdjustmentDisabled, ok;
604
605 info->implementation = "GetSystemTimeAsFileTime()";
606 info->monotonic = 0;
607 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
608 &isTimeAdjustmentDisabled);
609 if (!ok) {
610 PyErr_SetFromWindowsErr(0);
611 return -1;
612 }
613 info->resolution = timeIncrement * 1e-7;
614 info->adjustable = 1;
615 }
616
617#else /* MS_WINDOWS */
618 int err;
619#ifdef HAVE_CLOCK_GETTIME
620 struct timespec ts;
621#else
622 struct timeval tv;
623#endif
624
625 assert(info == NULL || raise);
626
627#ifdef HAVE_CLOCK_GETTIME
628 err = clock_gettime(CLOCK_REALTIME, &ts);
629 if (err) {
Victor Stinner277c8402017-10-11 08:11:38 -0700630 if (raise) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100631 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinner277c8402017-10-11 08:11:38 -0700632 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100633 return -1;
634 }
Victor Stinner277c8402017-10-11 08:11:38 -0700635 if (_PyTime_FromTimespec(tp, &ts, raise) < 0) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100636 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700637 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100638
639 if (info) {
640 struct timespec res;
641 info->implementation = "clock_gettime(CLOCK_REALTIME)";
642 info->monotonic = 0;
643 info->adjustable = 1;
Victor Stinner277c8402017-10-11 08:11:38 -0700644 if (clock_getres(CLOCK_REALTIME, &res) == 0) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100645 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
Victor Stinner277c8402017-10-11 08:11:38 -0700646 }
647 else {
Victor Stinnera47b8812015-03-27 18:16:17 +0100648 info->resolution = 1e-9;
Victor Stinner277c8402017-10-11 08:11:38 -0700649 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100650 }
651#else /* HAVE_CLOCK_GETTIME */
652
653 /* test gettimeofday() */
654#ifdef GETTIMEOFDAY_NO_TZ
655 err = gettimeofday(&tv);
656#else
657 err = gettimeofday(&tv, (struct timezone *)NULL);
658#endif
659 if (err) {
Victor Stinner277c8402017-10-11 08:11:38 -0700660 if (raise) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100661 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinner277c8402017-10-11 08:11:38 -0700662 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100663 return -1;
664 }
Victor Stinner277c8402017-10-11 08:11:38 -0700665 if (_PyTime_FromTimeval(tp, &tv, raise) < 0) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100666 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700667 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100668
669 if (info) {
670 info->implementation = "gettimeofday()";
671 info->resolution = 1e-6;
672 info->monotonic = 0;
673 info->adjustable = 1;
674 }
675#endif /* !HAVE_CLOCK_GETTIME */
676#endif /* !MS_WINDOWS */
677 return 0;
678}
679
Victor Stinner09e5cf22015-03-30 00:09:18 +0200680_PyTime_t
681_PyTime_GetSystemClock(void)
682{
683 _PyTime_t t;
Victor Stinnerc379ade2015-11-10 12:11:39 +0100684 if (pygettimeofday(&t, NULL, 0) < 0) {
Victor Stinner09e5cf22015-03-30 00:09:18 +0200685 /* should not happen, _PyTime_Init() checked the clock at startup */
Barry Warsawb2e57942017-09-14 18:13:16 -0700686 Py_UNREACHABLE();
Victor Stinner09e5cf22015-03-30 00:09:18 +0200687 }
688 return t;
689}
690
Victor Stinnera47b8812015-03-27 18:16:17 +0100691int
692_PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
693{
Victor Stinnerc379ade2015-11-10 12:11:39 +0100694 return pygettimeofday(t, info, 1);
Victor Stinnera47b8812015-03-27 18:16:17 +0100695}
696
Victor Stinnera47b8812015-03-27 18:16:17 +0100697static int
Victor Stinner5ad58212015-09-03 00:14:58 +0200698pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100699{
Victor Stinnercb29f012015-03-27 13:31:18 +0100700#if defined(MS_WINDOWS)
Victor Stinnerc60542b2015-09-10 15:55:07 +0200701 ULONGLONG ticks;
702 _PyTime_t t;
Victor Stinnercb29f012015-03-27 13:31:18 +0100703
704 assert(info == NULL || raise);
705
Victor Stinnerc60542b2015-09-10 15:55:07 +0200706 ticks = GetTickCount64();
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200707 Py_BUILD_ASSERT(sizeof(ticks) <= sizeof(_PyTime_t));
Victor Stinnerc60542b2015-09-10 15:55:07 +0200708 t = (_PyTime_t)ticks;
Victor Stinnercb29f012015-03-27 13:31:18 +0100709
Victor Stinnerc60542b2015-09-10 15:55:07 +0200710 if (_PyTime_check_mul_overflow(t, MS_TO_NS)) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100711 if (raise) {
712 _PyTime_overflow();
713 return -1;
714 }
715 /* Hello, time traveler! */
Barry Warsawb2e57942017-09-14 18:13:16 -0700716 Py_UNREACHABLE();
Victor Stinnercb29f012015-03-27 13:31:18 +0100717 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200718 *tp = t * MS_TO_NS;
Victor Stinnercb29f012015-03-27 13:31:18 +0100719
720 if (info) {
721 DWORD timeAdjustment, timeIncrement;
722 BOOL isTimeAdjustmentDisabled, ok;
Victor Stinnereb352292015-03-27 14:12:08 +0100723 info->implementation = "GetTickCount64()";
Victor Stinnercb29f012015-03-27 13:31:18 +0100724 info->monotonic = 1;
725 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
726 &isTimeAdjustmentDisabled);
727 if (!ok) {
728 PyErr_SetFromWindowsErr(0);
729 return -1;
730 }
731 info->resolution = timeIncrement * 1e-7;
732 info->adjustable = 0;
733 }
734
735#elif defined(__APPLE__)
736 static mach_timebase_info_data_t timebase;
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700737 static uint64_t t0 = 0;
738 uint64_t ticks;
Victor Stinnercb29f012015-03-27 13:31:18 +0100739
740 if (timebase.denom == 0) {
741 /* According to the Technical Q&A QA1398, mach_timebase_info() cannot
742 fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
743 (void)mach_timebase_info(&timebase);
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700744
745 /* Sanity check: should never occur in practice */
746 if (timebase.numer < 1 || timebase.denom < 1) {
747 PyErr_SetString(PyExc_RuntimeError,
748 "invalid mach_timebase_info");
749 return -1;
750 }
751
752 /* Check that timebase.numer and timebase.denom can be casted to
753 _PyTime_t. In pratice, timebase uses uint32_t, so casting cannot
754 overflow. At the end, only make sure that the type is uint32_t
755 (_PyTime_t is 64-bit long). */
756 assert(sizeof(timebase.numer) < sizeof(_PyTime_t));
757 assert(sizeof(timebase.denom) < sizeof(_PyTime_t));
758
759 /* Make sure that (ticks * timebase.numer) cannot overflow in
760 _PyTime_MulDiv(), with ticks < timebase.denom.
761
762 Known time bases:
763
764 * always (1, 1) on Intel
765 * (1000000000, 33333335) or (1000000000, 25000000) on PowerPC
766
767 None of these time bases can overflow with 64-bit _PyTime_t, but
768 check for overflow, just in case. */
769 if ((_PyTime_t)timebase.numer > _PyTime_MAX / (_PyTime_t)timebase.denom) {
770 PyErr_SetString(PyExc_OverflowError,
771 "mach_timebase_info is too large");
772 return -1;
773 }
774
775 t0 = mach_absolute_time();
Victor Stinnercb29f012015-03-27 13:31:18 +0100776 }
777
Victor Stinnercb29f012015-03-27 13:31:18 +0100778 if (info) {
779 info->implementation = "mach_absolute_time()";
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700780 info->resolution = (double)timebase.numer / (double)timebase.denom * 1e-9;
Victor Stinnercb29f012015-03-27 13:31:18 +0100781 info->monotonic = 1;
782 info->adjustable = 0;
783 }
784
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700785 ticks = mach_absolute_time();
786 /* Use a "time zero" to reduce precision loss when converting time
787 to floatting point number, as in time.monotonic(). */
788 ticks -= t0;
789 *tp = _PyTime_MulDiv(ticks,
790 (_PyTime_t)timebase.numer,
791 (_PyTime_t)timebase.denom);
792
haneyc90e9602017-06-21 11:18:21 -0700793#elif defined(__hpux)
794 hrtime_t time;
795
796 time = gethrtime();
797 if (time == -1) {
798 if (raise) {
799 PyErr_SetFromErrno(PyExc_OSError);
800 }
801 return -1;
802 }
803
804 *tp = time;
805
806 if (info) {
807 info->implementation = "gethrtime()";
808 info->resolution = 1e-9;
809 info->monotonic = 1;
810 info->adjustable = 0;
811 }
812
Victor Stinnercb29f012015-03-27 13:31:18 +0100813#else
814 struct timespec ts;
815#ifdef CLOCK_HIGHRES
816 const clockid_t clk_id = CLOCK_HIGHRES;
817 const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
818#else
819 const clockid_t clk_id = CLOCK_MONOTONIC;
820 const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
821#endif
822
823 assert(info == NULL || raise);
824
825 if (clock_gettime(clk_id, &ts) != 0) {
826 if (raise) {
827 PyErr_SetFromErrno(PyExc_OSError);
828 return -1;
829 }
830 return -1;
831 }
832
833 if (info) {
834 struct timespec res;
835 info->monotonic = 1;
836 info->implementation = implementation;
837 info->adjustable = 0;
838 if (clock_getres(clk_id, &res) != 0) {
839 PyErr_SetFromErrno(PyExc_OSError);
840 return -1;
841 }
842 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
843 }
Victor Stinner277c8402017-10-11 08:11:38 -0700844 if (_PyTime_FromTimespec(tp, &ts, raise) < 0) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100845 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700846 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100847#endif
Victor Stinnercb29f012015-03-27 13:31:18 +0100848 return 0;
849}
850
851_PyTime_t
852_PyTime_GetMonotonicClock(void)
853{
854 _PyTime_t t;
Victor Stinner5ad58212015-09-03 00:14:58 +0200855 if (pymonotonic(&t, NULL, 0) < 0) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100856 /* should not happen, _PyTime_Init() checked that monotonic clock at
857 startup */
Barry Warsawb2e57942017-09-14 18:13:16 -0700858 Py_UNREACHABLE();
Victor Stinnercb29f012015-03-27 13:31:18 +0100859 }
860 return t;
861}
862
Victor Stinner00111242014-08-29 16:31:59 +0200863int
Victor Stinner4bfb4602015-03-27 22:27:24 +0100864_PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
865{
Victor Stinner5ad58212015-09-03 00:14:58 +0200866 return pymonotonic(tp, info, 1);
Victor Stinner4bfb4602015-03-27 22:27:24 +0100867}
868
Victor Stinnera997c7b2017-10-10 02:51:50 -0700869
870#ifdef MS_WINDOWS
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700871static int
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700872win_perf_counter(_PyTime_t *tp, _Py_clock_info_t *info)
Victor Stinnera997c7b2017-10-10 02:51:50 -0700873{
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700874 static LONGLONG frequency = 0;
875 static LONGLONG t0 = 0;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700876 LARGE_INTEGER now;
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700877 LONGLONG ticksll;
878 _PyTime_t ticks;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700879
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700880 if (frequency == 0) {
Victor Stinnera997c7b2017-10-10 02:51:50 -0700881 LARGE_INTEGER freq;
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700882 if (!QueryPerformanceFrequency(&freq)) {
Victor Stinnera997c7b2017-10-10 02:51:50 -0700883 PyErr_SetFromWindowsErr(0);
884 return -1;
885 }
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700886 frequency = freq.QuadPart;
887
888 /* Sanity check: should never occur in practice */
889 if (frequency < 1) {
890 PyErr_SetString(PyExc_RuntimeError,
891 "invalid QueryPerformanceFrequency");
892 return -1;
893 }
894
895 /* Check that frequency can be casted to _PyTime_t.
896
897 Make also sure that (ticks * SEC_TO_NS) cannot overflow in
898 _PyTime_MulDiv(), with ticks < frequency.
899
900 Known QueryPerformanceFrequency() values:
901
902 * 10,000,000 (10 MHz): 100 ns resolution
903 * 3,579,545 Hz (3.6 MHz): 279 ns resolution
904
905 None of these frequencies can overflow with 64-bit _PyTime_t, but
906 check for overflow, just in case. */
907 if (frequency > _PyTime_MAX
908 || frequency > (LONGLONG)_PyTime_MAX / (LONGLONG)SEC_TO_NS) {
909 PyErr_SetString(PyExc_OverflowError,
910 "QueryPerformanceFrequency is too large");
911 return -1;
912 }
913
914 QueryPerformanceCounter(&now);
915 t0 = now.QuadPart;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700916 }
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700917
Victor Stinnera997c7b2017-10-10 02:51:50 -0700918 if (info) {
919 info->implementation = "QueryPerformanceCounter()";
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700920 info->resolution = 1.0 / (double)frequency;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700921 info->monotonic = 1;
922 info->adjustable = 0;
923 }
924
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700925 QueryPerformanceCounter(&now);
926 ticksll = now.QuadPart;
927
928 /* Use a "time zero" to reduce precision loss when converting time
929 to floatting point number, as in time.perf_counter(). */
930 ticksll -= t0;
931
932 /* Make sure that casting LONGLONG to _PyTime_t cannot overflow,
933 both types are signed */
934 Py_BUILD_ASSERT(sizeof(ticksll) <= sizeof(ticks));
935 ticks = (_PyTime_t)ticksll;
936
937 *tp = _PyTime_MulDiv(ticks, SEC_TO_NS, (_PyTime_t)frequency);
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700938 return 0;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700939}
940#endif
941
942
943int
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700944_PyTime_GetPerfCounterWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
Victor Stinnera997c7b2017-10-10 02:51:50 -0700945{
946#ifdef MS_WINDOWS
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700947 return win_perf_counter(t, info);
Victor Stinnera997c7b2017-10-10 02:51:50 -0700948#else
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700949 return _PyTime_GetMonotonicClockWithInfo(t, info);
Victor Stinnera997c7b2017-10-10 02:51:50 -0700950#endif
951}
952
953
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700954_PyTime_t
955_PyTime_GetPerfCounter(void)
Victor Stinnera997c7b2017-10-10 02:51:50 -0700956{
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700957 _PyTime_t t;
958 if (_PyTime_GetPerfCounterWithInfo(&t, NULL)) {
Victor Stinnera997c7b2017-10-10 02:51:50 -0700959 Py_UNREACHABLE();
960 }
961 return t;
962}
963
964
Victor Stinner4bfb4602015-03-27 22:27:24 +0100965int
Victor Stinner00111242014-08-29 16:31:59 +0200966_PyTime_Init(void)
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000967{
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700968 /* check that time.time(), time.monotonic() and time.perf_counter() clocks
969 are working properly to not have to check for exceptions at runtime. If
970 a clock works once, it cannot fail in next calls. */
Victor Stinnercb29f012015-03-27 13:31:18 +0100971 _PyTime_t t;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700972 if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0) {
Victor Stinner00111242014-08-29 16:31:59 +0200973 return -1;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700974 }
975 if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100976 return -1;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700977 }
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700978 if (_PyTime_GetPerfCounterWithInfo(&t, NULL) < 0) {
Victor Stinnera997c7b2017-10-10 02:51:50 -0700979 return -1;
980 }
Victor Stinner00111242014-08-29 16:31:59 +0200981 return 0;
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000982}
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400983
984int
985_PyTime_localtime(time_t t, struct tm *tm)
986{
987#ifdef MS_WINDOWS
988 int error;
989
990 error = localtime_s(tm, &t);
991 if (error != 0) {
992 errno = error;
993 PyErr_SetFromErrno(PyExc_OSError);
994 return -1;
995 }
996 return 0;
997#else /* !MS_WINDOWS */
998 if (localtime_r(&t, tm) == NULL) {
999#ifdef EINVAL
Victor Stinner277c8402017-10-11 08:11:38 -07001000 if (errno == 0) {
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001001 errno = EINVAL;
Victor Stinner277c8402017-10-11 08:11:38 -07001002 }
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001003#endif
1004 PyErr_SetFromErrno(PyExc_OSError);
1005 return -1;
1006 }
1007 return 0;
1008#endif /* MS_WINDOWS */
1009}
1010
1011int
1012_PyTime_gmtime(time_t t, struct tm *tm)
1013{
1014#ifdef MS_WINDOWS
1015 int error;
1016
1017 error = gmtime_s(tm, &t);
1018 if (error != 0) {
1019 errno = error;
1020 PyErr_SetFromErrno(PyExc_OSError);
1021 return -1;
1022 }
1023 return 0;
1024#else /* !MS_WINDOWS */
1025 if (gmtime_r(&t, tm) == NULL) {
1026#ifdef EINVAL
Victor Stinner277c8402017-10-11 08:11:38 -07001027 if (errno == 0) {
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001028 errno = EINVAL;
Victor Stinner277c8402017-10-11 08:11:38 -07001029 }
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001030#endif
1031 PyErr_SetFromErrno(PyExc_OSError);
1032 return -1;
1033 }
1034 return 0;
1035#endif /* MS_WINDOWS */
1036}