blob: 89d63e080422b34e20257549b4edd5ee680c35ad [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
Minmin Gongf6605672020-05-18 09:22:53 -07003#include <winsock2.h> /* struct timeval */
Victor Stinner09225b72012-02-07 23:41:01 +01004#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() */
Ronald Oussoren41761932020-11-08 10:05:27 +01008
9#if defined(__APPLE__) && defined(__has_builtin)
10# if __has_builtin(__builtin_available)
11# define HAVE_CLOCK_GETTIME_RUNTIME __builtin_available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
12# endif
13#endif
Victor Stinnerae586492014-09-02 23:18:25 +020014#endif
15
Victor Stinnerc60542b2015-09-10 15:55:07 +020016#define _PyTime_check_mul_overflow(a, b) \
17 (assert(b > 0), \
18 (_PyTime_t)(a) < _PyTime_MIN / (_PyTime_t)(b) \
19 || _PyTime_MAX / (_PyTime_t)(b) < (_PyTime_t)(a))
20
Victor Stinnercb29f012015-03-27 13:31:18 +010021/* To millisecond (10^-3) */
Victor Stinner580ef132015-03-20 01:55:04 +010022#define SEC_TO_MS 1000
Victor Stinner580ef132015-03-20 01:55:04 +010023
Victor Stinnercb29f012015-03-27 13:31:18 +010024/* To microseconds (10^-6) */
25#define MS_TO_US 1000
Victor Stinner580ef132015-03-20 01:55:04 +010026#define SEC_TO_US (SEC_TO_MS * MS_TO_US)
27
Victor Stinnercb29f012015-03-27 13:31:18 +010028/* To nanoseconds (10^-9) */
29#define US_TO_NS 1000
30#define MS_TO_NS (MS_TO_US * US_TO_NS)
31#define SEC_TO_NS (SEC_TO_MS * MS_TO_NS)
32
Victor Stinner62d1c702015-04-01 17:47:07 +020033/* Conversion from nanoseconds */
34#define NS_TO_MS (1000 * 1000)
35#define NS_TO_US (1000)
36
Victor Stinner5d272cc2012-03-13 13:35:55 +010037static void
38error_time_t_overflow(void)
Victor Stinner643cd682012-03-02 22:54:03 +010039{
Victor Stinner5d272cc2012-03-13 13:35:55 +010040 PyErr_SetString(PyExc_OverflowError,
41 "timestamp out of range for platform time_t");
42}
43
Victor Stinner277c8402017-10-11 08:11:38 -070044static void
45_PyTime_overflow(void)
46{
47 PyErr_SetString(PyExc_OverflowError,
48 "timestamp too large to convert to C _PyTime_t");
49}
50
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -070051
Victor Stinnerc29b5852017-11-02 07:28:27 -070052_PyTime_t
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -070053_PyTime_MulDiv(_PyTime_t ticks, _PyTime_t mul, _PyTime_t div)
54{
55 _PyTime_t intpart, remaining;
56 /* Compute (ticks * mul / div) in two parts to prevent integer overflow:
57 compute integer part, and then the remaining part.
58
59 (ticks * mul) / div == (ticks / div) * mul + (ticks % div) * mul / div
60
61 The caller must ensure that "(div - 1) * mul" cannot overflow. */
62 intpart = ticks / div;
63 ticks %= div;
64 remaining = ticks * mul;
65 remaining /= div;
66 return intpart * mul + remaining;
67}
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -070068
69
Larry Hastings76ad59b2012-05-03 00:30:07 -070070time_t
Victor Stinner5d272cc2012-03-13 13:35:55 +010071_PyLong_AsTime_t(PyObject *obj)
72{
Benjamin Petersoned4aa832016-09-05 17:44:18 -070073#if SIZEOF_TIME_T == SIZEOF_LONG_LONG
Benjamin Petersonaf580df2016-09-06 10:46:49 -070074 long long val;
Victor Stinner5d272cc2012-03-13 13:35:55 +010075 val = PyLong_AsLongLong(obj);
76#else
77 long val;
Serhiy Storchakafad85aa2015-11-07 15:42:38 +020078 Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
Victor Stinner5d272cc2012-03-13 13:35:55 +010079 val = PyLong_AsLong(obj);
80#endif
81 if (val == -1 && PyErr_Occurred()) {
Victor Stinner277c8402017-10-11 08:11:38 -070082 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +010083 error_time_t_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -070084 }
Victor Stinner5d272cc2012-03-13 13:35:55 +010085 return -1;
86 }
87 return (time_t)val;
88}
89
Larry Hastings6fe20b32012-04-19 15:07:49 -070090PyObject *
91_PyLong_FromTime_t(time_t t)
92{
Benjamin Petersoned4aa832016-09-05 17:44:18 -070093#if SIZEOF_TIME_T == SIZEOF_LONG_LONG
Benjamin Petersonaf580df2016-09-06 10:46:49 -070094 return PyLong_FromLongLong((long long)t);
Larry Hastings6fe20b32012-04-19 15:07:49 -070095#else
Serhiy Storchakafad85aa2015-11-07 15:42:38 +020096 Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
Larry Hastings6fe20b32012-04-19 15:07:49 -070097 return PyLong_FromLong((long)t);
98#endif
99}
100
Victor Stinnerce6aa742015-09-09 22:28:09 +0200101/* Round to nearest with ties going to nearest even integer
102 (_PyTime_ROUND_HALF_EVEN) */
103static double
Victor Stinner7667f582015-09-09 01:02:23 +0200104_PyTime_RoundHalfEven(double x)
Victor Stinner74474232015-09-02 01:43:56 +0200105{
Victor Stinner7667f582015-09-09 01:02:23 +0200106 double rounded = round(x);
Victor Stinner277c8402017-10-11 08:11:38 -0700107 if (fabs(x-rounded) == 0.5) {
Victor Stinner7667f582015-09-09 01:02:23 +0200108 /* halfway case: round to even */
109 rounded = 2.0*round(x/2.0);
Victor Stinner277c8402017-10-11 08:11:38 -0700110 }
Victor Stinner7667f582015-09-09 01:02:23 +0200111 return rounded;
Victor Stinner74474232015-09-02 01:43:56 +0200112}
113
Victor Stinner9ae47df2015-09-09 22:28:58 +0200114static double
115_PyTime_Round(double x, _PyTime_round_t round)
116{
Victor Stinner1efbeba2015-09-10 11:48:00 +0200117 /* volatile avoids optimization changing how numbers are rounded */
118 volatile double d;
119
120 d = x;
Victor Stinner277c8402017-10-11 08:11:38 -0700121 if (round == _PyTime_ROUND_HALF_EVEN) {
Victor Stinner1efbeba2015-09-10 11:48:00 +0200122 d = _PyTime_RoundHalfEven(d);
Victor Stinner277c8402017-10-11 08:11:38 -0700123 }
124 else if (round == _PyTime_ROUND_CEILING) {
Victor Stinner1efbeba2015-09-10 11:48:00 +0200125 d = ceil(d);
Victor Stinner277c8402017-10-11 08:11:38 -0700126 }
Pablo Galindo2c15b292017-10-17 15:14:41 +0100127 else if (round == _PyTime_ROUND_FLOOR) {
Victor Stinner1efbeba2015-09-10 11:48:00 +0200128 d = floor(d);
Victor Stinner277c8402017-10-11 08:11:38 -0700129 }
Pablo Galindo2c15b292017-10-17 15:14:41 +0100130 else {
131 assert(round == _PyTime_ROUND_UP);
132 d = (d >= 0.0) ? ceil(d) : floor(d);
133 }
Victor Stinner1efbeba2015-09-10 11:48:00 +0200134 return d;
Victor Stinner9ae47df2015-09-09 22:28:58 +0200135}
136
Victor Stinner5d272cc2012-03-13 13:35:55 +0100137static int
Victor Stinner53e137c2015-09-02 00:49:16 +0200138_PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
Victor Stinner277c8402017-10-11 08:11:38 -0700139 long idenominator, _PyTime_round_t round)
Victor Stinner53e137c2015-09-02 00:49:16 +0200140{
Victor Stinner277c8402017-10-11 08:11:38 -0700141 double denominator = (double)idenominator;
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700142 double intpart;
Victor Stinner24b822e2015-09-02 11:58:56 +0200143 /* volatile avoids optimization changing how numbers are rounded */
Victor Stinner53e137c2015-09-02 00:49:16 +0200144 volatile double floatpart;
145
146 floatpart = modf(d, &intpart);
Victor Stinner53e137c2015-09-02 00:49:16 +0200147
148 floatpart *= denominator;
Victor Stinner9ae47df2015-09-09 22:28:58 +0200149 floatpart = _PyTime_Round(floatpart, round);
Victor Stinner67edcc92015-09-02 10:37:46 +0200150 if (floatpart >= denominator) {
151 floatpart -= denominator;
152 intpart += 1.0;
Victor Stinner53e137c2015-09-02 00:49:16 +0200153 }
Victor Stinneradfefa52015-09-04 23:57:25 +0200154 else if (floatpart < 0) {
155 floatpart += denominator;
156 intpart -= 1.0;
157 }
Victor Stinner67edcc92015-09-02 10:37:46 +0200158 assert(0.0 <= floatpart && floatpart < denominator);
Victor Stinner53e137c2015-09-02 00:49:16 +0200159
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700160 if (!_Py_InIntegralTypeRange(time_t, intpart)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200161 error_time_t_overflow();
162 return -1;
163 }
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700164 *sec = (time_t)intpart;
165 *numerator = (long)floatpart;
Victor Stinner277c8402017-10-11 08:11:38 -0700166 assert(0 <= *numerator && *numerator < idenominator);
Victor Stinner53e137c2015-09-02 00:49:16 +0200167 return 0;
168}
169
170static int
Victor Stinner5d272cc2012-03-13 13:35:55 +0100171_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
Victor Stinner277c8402017-10-11 08:11:38 -0700172 long denominator, _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100173{
Victor Stinner277c8402017-10-11 08:11:38 -0700174 assert(denominator >= 1);
Victor Stinnerbbdda212015-09-02 00:50:43 +0200175
Victor Stinner643cd682012-03-02 22:54:03 +0100176 if (PyFloat_Check(obj)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200177 double d = PyFloat_AsDouble(obj);
Han Lee829dacc2017-09-09 08:05:05 +0900178 if (Py_IS_NAN(d)) {
179 *numerator = 0;
180 PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
181 return -1;
182 }
Victor Stinner53e137c2015-09-02 00:49:16 +0200183 return _PyTime_DoubleToDenominator(d, sec, numerator,
184 denominator, round);
Victor Stinner643cd682012-03-02 22:54:03 +0100185 }
186 else {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100187 *sec = _PyLong_AsTime_t(obj);
Victor Stinner67edcc92015-09-02 10:37:46 +0200188 *numerator = 0;
Victor Stinner277c8402017-10-11 08:11:38 -0700189 if (*sec == (time_t)-1 && PyErr_Occurred()) {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100190 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700191 }
Victor Stinner643cd682012-03-02 22:54:03 +0100192 return 0;
193 }
Victor Stinner5d272cc2012-03-13 13:35:55 +0100194}
Victor Stinner643cd682012-03-02 22:54:03 +0100195
Victor Stinner5d272cc2012-03-13 13:35:55 +0100196int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100197_PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100198{
199 if (PyFloat_Check(obj)) {
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700200 double intpart;
Victor Stinner24b822e2015-09-02 11:58:56 +0200201 /* volatile avoids optimization changing how numbers are rounded */
Victor Stinner5786aef2015-09-03 16:33:16 +0200202 volatile double d;
Victor Stinner5d272cc2012-03-13 13:35:55 +0100203
Victor Stinner5d272cc2012-03-13 13:35:55 +0100204 d = PyFloat_AsDouble(obj);
Han Lee829dacc2017-09-09 08:05:05 +0900205 if (Py_IS_NAN(d)) {
206 PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
207 return -1;
208 }
209
Victor Stinner9ae47df2015-09-09 22:28:58 +0200210 d = _PyTime_Round(d, round);
Victor Stinner5d272cc2012-03-13 13:35:55 +0100211 (void)modf(d, &intpart);
212
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700213 if (!_Py_InIntegralTypeRange(time_t, intpart)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100214 error_time_t_overflow();
215 return -1;
216 }
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700217 *sec = (time_t)intpart;
Victor Stinner5d272cc2012-03-13 13:35:55 +0100218 return 0;
219 }
220 else {
221 *sec = _PyLong_AsTime_t(obj);
Victor Stinner277c8402017-10-11 08:11:38 -0700222 if (*sec == (time_t)-1 && PyErr_Occurred()) {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100223 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700224 }
Victor Stinner5d272cc2012-03-13 13:35:55 +0100225 return 0;
226 }
227}
228
229int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100230_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
231 _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100232{
Victor Stinner277c8402017-10-11 08:11:38 -0700233 return _PyTime_ObjectToDenominator(obj, sec, nsec, SEC_TO_NS, round);
Victor Stinner5d272cc2012-03-13 13:35:55 +0100234}
235
236int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100237_PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
238 _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100239{
Victor Stinner277c8402017-10-11 08:11:38 -0700240 return _PyTime_ObjectToDenominator(obj, sec, usec, SEC_TO_US, round);
Victor Stinnercb29f012015-03-27 13:31:18 +0100241}
242
Victor Stinner4bfb4602015-03-27 22:27:24 +0100243_PyTime_t
Victor Stinner13019fd2015-04-03 13:10:54 +0200244_PyTime_FromSeconds(int seconds)
245{
246 _PyTime_t t;
247 /* ensure that integer overflow cannot happen, int type should have 32
248 bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
249 bits). */
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200250 Py_BUILD_ASSERT(INT_MAX <= _PyTime_MAX / SEC_TO_NS);
251 Py_BUILD_ASSERT(INT_MIN >= _PyTime_MIN / SEC_TO_NS);
Victor Stinner277c8402017-10-11 08:11:38 -0700252
253 t = (_PyTime_t)seconds;
Victor Stinnerbbdda212015-09-02 00:50:43 +0200254 assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
255 || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
256 t *= SEC_TO_NS;
Victor Stinner13019fd2015-04-03 13:10:54 +0200257 return t;
258}
259
260_PyTime_t
Victor Stinnerc29b5852017-11-02 07:28:27 -0700261_PyTime_FromNanoseconds(_PyTime_t ns)
Victor Stinner4bfb4602015-03-27 22:27:24 +0100262{
Victor Stinnerc29b5852017-11-02 07:28:27 -0700263 /* _PyTime_t already uses nanosecond resolution, no conversion needed */
264 return ns;
265}
266
267int
268_PyTime_FromNanosecondsObject(_PyTime_t *tp, PyObject *obj)
269{
270 long long nsec;
Victor Stinner4bfb4602015-03-27 22:27:24 +0100271 _PyTime_t t;
Victor Stinnerc29b5852017-11-02 07:28:27 -0700272
273 if (!PyLong_Check(obj)) {
274 PyErr_Format(PyExc_TypeError, "expect int, got %s",
275 Py_TYPE(obj)->tp_name);
276 return -1;
277 }
278
279 Py_BUILD_ASSERT(sizeof(long long) == sizeof(_PyTime_t));
280 nsec = PyLong_AsLongLong(obj);
281 if (nsec == -1 && PyErr_Occurred()) {
282 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
283 _PyTime_overflow();
284 }
285 return -1;
286 }
287
288 /* _PyTime_t already uses nanosecond resolution, no conversion needed */
289 t = (_PyTime_t)nsec;
290 *tp = t;
291 return 0;
Victor Stinner4bfb4602015-03-27 22:27:24 +0100292}
293
Victor Stinnera47b8812015-03-27 18:16:17 +0100294#ifdef HAVE_CLOCK_GETTIME
Victor Stinnercb29f012015-03-27 13:31:18 +0100295static int
Victor Stinnerc29b5852017-11-02 07:28:27 -0700296pytime_fromtimespec(_PyTime_t *tp, struct timespec *ts, int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100297{
Victor Stinnerc29b5852017-11-02 07:28:27 -0700298 _PyTime_t t, nsec;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100299 int res = 0;
300
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200301 Py_BUILD_ASSERT(sizeof(ts->tv_sec) <= sizeof(_PyTime_t));
Victor Stinnerc60542b2015-09-10 15:55:07 +0200302 t = (_PyTime_t)ts->tv_sec;
303
304 if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
Victor Stinner277c8402017-10-11 08:11:38 -0700305 if (raise) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100306 _PyTime_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -0700307 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100308 res = -1;
Victor Stinnerc29b5852017-11-02 07:28:27 -0700309 t = (t > 0) ? _PyTime_MAX : _PyTime_MIN;
Victor Stinnercb29f012015-03-27 13:31:18 +0100310 }
Victor Stinnerc29b5852017-11-02 07:28:27 -0700311 else {
312 t = t * SEC_TO_NS;
313 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100314
Victor Stinnerc29b5852017-11-02 07:28:27 -0700315 nsec = ts->tv_nsec;
316 /* The following test is written for positive only nsec */
317 assert(nsec >= 0);
318 if (t > _PyTime_MAX - nsec) {
319 if (raise) {
320 _PyTime_overflow();
321 }
322 res = -1;
323 t = _PyTime_MAX;
324 }
325 else {
326 t += nsec;
327 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100328
329 *tp = t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100330 return res;
Victor Stinnercb29f012015-03-27 13:31:18 +0100331}
Victor Stinnerc29b5852017-11-02 07:28:27 -0700332
333int
334_PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts)
Victor Stinnera47b8812015-03-27 18:16:17 +0100335{
Victor Stinnerc29b5852017-11-02 07:28:27 -0700336 return pytime_fromtimespec(tp, ts, 1);
337}
338#endif
339
340#if !defined(MS_WINDOWS)
341static int
342pytime_fromtimeval(_PyTime_t *tp, struct timeval *tv, int raise)
343{
344 _PyTime_t t, usec;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100345 int res = 0;
Victor Stinnera47b8812015-03-27 18:16:17 +0100346
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200347 Py_BUILD_ASSERT(sizeof(tv->tv_sec) <= sizeof(_PyTime_t));
Victor Stinnerc60542b2015-09-10 15:55:07 +0200348 t = (_PyTime_t)tv->tv_sec;
349
350 if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
Victor Stinner277c8402017-10-11 08:11:38 -0700351 if (raise) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100352 _PyTime_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -0700353 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100354 res = -1;
Victor Stinnerc29b5852017-11-02 07:28:27 -0700355 t = (t > 0) ? _PyTime_MAX : _PyTime_MIN;
Victor Stinnera47b8812015-03-27 18:16:17 +0100356 }
Victor Stinnerc29b5852017-11-02 07:28:27 -0700357 else {
358 t = t * SEC_TO_NS;
359 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100360
Victor Stinnerc29b5852017-11-02 07:28:27 -0700361 usec = (_PyTime_t)tv->tv_usec * US_TO_NS;
362 /* The following test is written for positive only usec */
363 assert(usec >= 0);
364 if (t > _PyTime_MAX - usec) {
365 if (raise) {
366 _PyTime_overflow();
367 }
368 res = -1;
369 t = _PyTime_MAX;
370 }
371 else {
372 t += usec;
373 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100374
375 *tp = t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100376 return res;
Victor Stinnera47b8812015-03-27 18:16:17 +0100377}
Victor Stinnerc29b5852017-11-02 07:28:27 -0700378
379int
380_PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv)
381{
382 return pytime_fromtimeval(tp, tv, 1);
383}
Victor Stinnercb29f012015-03-27 13:31:18 +0100384#endif
385
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200386static int
Victor Stinnera997c7b2017-10-10 02:51:50 -0700387_PyTime_FromDouble(_PyTime_t *t, double value, _PyTime_round_t round,
388 long unit_to_ns)
Victor Stinner53e137c2015-09-02 00:49:16 +0200389{
Victor Stinner24b822e2015-09-02 11:58:56 +0200390 /* volatile avoids optimization changing how numbers are rounded */
Victor Stinner5786aef2015-09-03 16:33:16 +0200391 volatile double d;
Victor Stinner53e137c2015-09-02 00:49:16 +0200392
393 /* convert to a number of nanoseconds */
394 d = value;
Victor Stinner9ae47df2015-09-09 22:28:58 +0200395 d *= (double)unit_to_ns;
396 d = _PyTime_Round(d, round);
Victor Stinner53e137c2015-09-02 00:49:16 +0200397
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700398 if (!_Py_InIntegralTypeRange(_PyTime_t, d)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200399 _PyTime_overflow();
400 return -1;
401 }
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700402 *t = (_PyTime_t)d;
Victor Stinner53e137c2015-09-02 00:49:16 +0200403 return 0;
404}
405
406static int
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200407_PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
Victor Stinner9ae47df2015-09-09 22:28:58 +0200408 long unit_to_ns)
Victor Stinnercb29f012015-03-27 13:31:18 +0100409{
410 if (PyFloat_Check(obj)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200411 double d;
Victor Stinnercb29f012015-03-27 13:31:18 +0100412 d = PyFloat_AsDouble(obj);
Han Lee829dacc2017-09-09 08:05:05 +0900413 if (Py_IS_NAN(d)) {
414 PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
415 return -1;
416 }
Victor Stinnera997c7b2017-10-10 02:51:50 -0700417 return _PyTime_FromDouble(t, d, round, unit_to_ns);
Victor Stinnercb29f012015-03-27 13:31:18 +0100418 }
419 else {
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700420 long long sec;
421 Py_BUILD_ASSERT(sizeof(long long) <= sizeof(_PyTime_t));
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200422
423 sec = PyLong_AsLongLong(obj);
Victor Stinnercb29f012015-03-27 13:31:18 +0100424 if (sec == -1 && PyErr_Occurred()) {
Victor Stinner277c8402017-10-11 08:11:38 -0700425 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100426 _PyTime_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -0700427 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100428 return -1;
429 }
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200430
Victor Stinnerc60542b2015-09-10 15:55:07 +0200431 if (_PyTime_check_mul_overflow(sec, unit_to_ns)) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100432 _PyTime_overflow();
433 return -1;
434 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200435 *t = sec * unit_to_ns;
Victor Stinnercb29f012015-03-27 13:31:18 +0100436 return 0;
437 }
438}
439
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200440int
441_PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
442{
443 return _PyTime_FromObject(t, obj, round, SEC_TO_NS);
444}
445
446int
447_PyTime_FromMillisecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
448{
449 return _PyTime_FromObject(t, obj, round, MS_TO_NS);
450}
451
Victor Stinner4bfb4602015-03-27 22:27:24 +0100452double
453_PyTime_AsSecondsDouble(_PyTime_t t)
454{
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200455 /* volatile avoids optimization changing how numbers are rounded */
456 volatile double d;
457
Victor Stinner3e2c8d82015-09-09 22:32:48 +0200458 if (t % SEC_TO_NS == 0) {
459 _PyTime_t secs;
460 /* Divide using integers to avoid rounding issues on the integer part.
461 1e-9 cannot be stored exactly in IEEE 64-bit. */
462 secs = t / SEC_TO_NS;
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200463 d = (double)secs;
Victor Stinner3e2c8d82015-09-09 22:32:48 +0200464 }
465 else {
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200466 d = (double)t;
467 d /= 1e9;
Victor Stinner3e2c8d82015-09-09 22:32:48 +0200468 }
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200469 return d;
Victor Stinner4bfb4602015-03-27 22:27:24 +0100470}
471
Victor Stinner992c43f2015-03-27 17:12:45 +0100472PyObject *
473_PyTime_AsNanosecondsObject(_PyTime_t t)
474{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700475 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(_PyTime_t));
476 return PyLong_FromLongLong((long long)t);
Victor Stinner992c43f2015-03-27 17:12:45 +0100477}
478
Victor Stinnercb29f012015-03-27 13:31:18 +0100479static _PyTime_t
Victor Stinner7667f582015-09-09 01:02:23 +0200480_PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
481 const _PyTime_round_t round)
Victor Stinnercb29f012015-03-27 13:31:18 +0100482{
Victor Stinner62d1c702015-04-01 17:47:07 +0200483 assert(k > 1);
Victor Stinner7667f582015-09-09 01:02:23 +0200484 if (round == _PyTime_ROUND_HALF_EVEN) {
485 _PyTime_t x, r, abs_r;
Victor Stinner74474232015-09-02 01:43:56 +0200486 x = t / k;
487 r = t % k;
Victor Stinner7667f582015-09-09 01:02:23 +0200488 abs_r = Py_ABS(r);
489 if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) {
Victor Stinner277c8402017-10-11 08:11:38 -0700490 if (t >= 0) {
Victor Stinner74474232015-09-02 01:43:56 +0200491 x++;
Victor Stinner277c8402017-10-11 08:11:38 -0700492 }
493 else {
Victor Stinner74474232015-09-02 01:43:56 +0200494 x--;
Victor Stinner277c8402017-10-11 08:11:38 -0700495 }
Victor Stinner74474232015-09-02 01:43:56 +0200496 }
497 return x;
498 }
499 else if (round == _PyTime_ROUND_CEILING) {
Victor Stinner277c8402017-10-11 08:11:38 -0700500 if (t >= 0) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100501 return (t + k - 1) / k;
Victor Stinner277c8402017-10-11 08:11:38 -0700502 }
503 else {
Victor Stinner7667f582015-09-09 01:02:23 +0200504 return t / k;
Victor Stinner277c8402017-10-11 08:11:38 -0700505 }
Victor Stinner7667f582015-09-09 01:02:23 +0200506 }
Pablo Galindo2c15b292017-10-17 15:14:41 +0100507 else if (round == _PyTime_ROUND_FLOOR){
Victor Stinner277c8402017-10-11 08:11:38 -0700508 if (t >= 0) {
Victor Stinner7667f582015-09-09 01:02:23 +0200509 return t / k;
Victor Stinner277c8402017-10-11 08:11:38 -0700510 }
511 else {
Victor Stinner62d1c702015-04-01 17:47:07 +0200512 return (t - (k - 1)) / k;
Victor Stinner277c8402017-10-11 08:11:38 -0700513 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100514 }
Pablo Galindo2c15b292017-10-17 15:14:41 +0100515 else {
516 assert(round == _PyTime_ROUND_UP);
517 if (t >= 0) {
518 return (t + k - 1) / k;
519 }
520 else {
521 return (t - (k - 1)) / k;
522 }
523 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100524}
525
526_PyTime_t
527_PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
528{
Victor Stinner62d1c702015-04-01 17:47:07 +0200529 return _PyTime_Divide(t, NS_TO_MS, round);
Victor Stinnercb29f012015-03-27 13:31:18 +0100530}
531
Victor Stinnerf5faad22015-03-28 03:52:05 +0100532_PyTime_t
533_PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
534{
Victor Stinner62d1c702015-04-01 17:47:07 +0200535 return _PyTime_Divide(t, NS_TO_US, round);
Victor Stinnerf5faad22015-03-28 03:52:05 +0100536}
537
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200538static int
Victor Stinner1e2b6882015-09-18 13:23:02 +0200539_PyTime_AsTimeval_impl(_PyTime_t t, _PyTime_t *p_secs, int *p_us,
540 _PyTime_round_t round)
Victor Stinnercb29f012015-03-27 13:31:18 +0100541{
542 _PyTime_t secs, ns;
Victor Stinner74474232015-09-02 01:43:56 +0200543 int usec;
Victor Stinner1e2b6882015-09-18 13:23:02 +0200544 int res = 0;
Victor Stinnercb29f012015-03-27 13:31:18 +0100545
546 secs = t / SEC_TO_NS;
547 ns = t % SEC_TO_NS;
548
Victor Stinner1e2b6882015-09-18 13:23:02 +0200549 usec = (int)_PyTime_Divide(ns, US_TO_NS, round);
550 if (usec < 0) {
551 usec += SEC_TO_US;
Victor Stinner277c8402017-10-11 08:11:38 -0700552 if (secs != _PyTime_MIN) {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200553 secs -= 1;
Victor Stinner277c8402017-10-11 08:11:38 -0700554 }
555 else {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200556 res = -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700557 }
Victor Stinner1e2b6882015-09-18 13:23:02 +0200558 }
559 else if (usec >= SEC_TO_US) {
560 usec -= SEC_TO_US;
Victor Stinner277c8402017-10-11 08:11:38 -0700561 if (secs != _PyTime_MAX) {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200562 secs += 1;
Victor Stinner277c8402017-10-11 08:11:38 -0700563 }
564 else {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200565 res = -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700566 }
Victor Stinner1e2b6882015-09-18 13:23:02 +0200567 }
568 assert(0 <= usec && usec < SEC_TO_US);
569
570 *p_secs = secs;
571 *p_us = usec;
572
573 return res;
574}
575
576static int
577_PyTime_AsTimevalStruct_impl(_PyTime_t t, struct timeval *tv,
578 _PyTime_round_t round, int raise)
579{
Victor Stinnerb7a8af22015-10-01 08:44:03 +0200580 _PyTime_t secs, secs2;
Victor Stinner1e2b6882015-09-18 13:23:02 +0200581 int us;
582 int res;
583
584 res = _PyTime_AsTimeval_impl(t, &secs, &us, round);
585
Victor Stinnercb29f012015-03-27 13:31:18 +0100586#ifdef MS_WINDOWS
Victor Stinnercb29f012015-03-27 13:31:18 +0100587 tv->tv_sec = (long)secs;
588#else
Victor Stinnercb29f012015-03-27 13:31:18 +0100589 tv->tv_sec = secs;
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200590#endif
Victor Stinner1e2b6882015-09-18 13:23:02 +0200591 tv->tv_usec = us;
Victor Stinnercb29f012015-03-27 13:31:18 +0100592
Victor Stinnerb7a8af22015-10-01 08:44:03 +0200593 secs2 = (_PyTime_t)tv->tv_sec;
594 if (res < 0 || secs2 != secs) {
Victor Stinner277c8402017-10-11 08:11:38 -0700595 if (raise) {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200596 error_time_t_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -0700597 }
Victor Stinner1e2b6882015-09-18 13:23:02 +0200598 return -1;
Victor Stinner74474232015-09-02 01:43:56 +0200599 }
Victor Stinner1e2b6882015-09-18 13:23:02 +0200600 return 0;
Victor Stinnercb29f012015-03-27 13:31:18 +0100601}
602
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200603int
604_PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
605{
Victor Stinner1e2b6882015-09-18 13:23:02 +0200606 return _PyTime_AsTimevalStruct_impl(t, tv, round, 1);
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200607}
608
609int
610_PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
611{
Victor Stinner1e2b6882015-09-18 13:23:02 +0200612 return _PyTime_AsTimevalStruct_impl(t, tv, round, 0);
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200613}
614
Victor Stinner1e2b6882015-09-18 13:23:02 +0200615int
616_PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us,
617 _PyTime_round_t round)
618{
619 _PyTime_t secs;
620 int res;
621
622 res = _PyTime_AsTimeval_impl(t, &secs, us, round);
623
624 *p_secs = secs;
625
626 if (res < 0 || (_PyTime_t)*p_secs != secs) {
627 error_time_t_overflow();
628 return -1;
629 }
630 return 0;
631}
632
633
Victor Stinnerc3378382015-03-28 05:07:51 +0100634#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
Victor Stinner34dc0f42015-03-27 18:19:03 +0100635int
636_PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
637{
Victor Stinner95e9cef2015-03-28 01:26:47 +0100638 _PyTime_t secs, nsec;
639
640 secs = t / SEC_TO_NS;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100641 nsec = t % SEC_TO_NS;
642 if (nsec < 0) {
643 nsec += SEC_TO_NS;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100644 secs -= 1;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100645 }
Victor Stinner95e9cef2015-03-28 01:26:47 +0100646 ts->tv_sec = (time_t)secs;
Victor Stinner29ee6742015-09-03 16:25:45 +0200647 assert(0 <= nsec && nsec < SEC_TO_NS);
648 ts->tv_nsec = nsec;
649
Victor Stinner95e9cef2015-03-28 01:26:47 +0100650 if ((_PyTime_t)ts->tv_sec != secs) {
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200651 error_time_t_overflow();
Victor Stinner34dc0f42015-03-27 18:19:03 +0100652 return -1;
653 }
Victor Stinner34dc0f42015-03-27 18:19:03 +0100654 return 0;
655}
656#endif
657
Victor Stinnercb29f012015-03-27 13:31:18 +0100658static int
Victor Stinnerc379ade2015-11-10 12:11:39 +0100659pygettimeofday(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
Victor Stinnera47b8812015-03-27 18:16:17 +0100660{
661#ifdef MS_WINDOWS
662 FILETIME system_time;
663 ULARGE_INTEGER large;
664
665 assert(info == NULL || raise);
666
667 GetSystemTimeAsFileTime(&system_time);
668 large.u.LowPart = system_time.dwLowDateTime;
669 large.u.HighPart = system_time.dwHighDateTime;
670 /* 11,644,473,600,000,000,000: number of nanoseconds between
671 the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
672 days). */
673 *tp = large.QuadPart * 100 - 11644473600000000000;
674 if (info) {
675 DWORD timeAdjustment, timeIncrement;
676 BOOL isTimeAdjustmentDisabled, ok;
677
678 info->implementation = "GetSystemTimeAsFileTime()";
679 info->monotonic = 0;
680 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
681 &isTimeAdjustmentDisabled);
682 if (!ok) {
683 PyErr_SetFromWindowsErr(0);
684 return -1;
685 }
686 info->resolution = timeIncrement * 1e-7;
687 info->adjustable = 1;
688 }
689
690#else /* MS_WINDOWS */
691 int err;
Ronald Oussoren41761932020-11-08 10:05:27 +0100692#if defined(HAVE_CLOCK_GETTIME)
Victor Stinnera47b8812015-03-27 18:16:17 +0100693 struct timespec ts;
Ronald Oussoren41761932020-11-08 10:05:27 +0100694#endif
695
696#if !defined(HAVE_CLOCK_GETTIME) || defined(__APPLE__)
Victor Stinnera47b8812015-03-27 18:16:17 +0100697 struct timeval tv;
698#endif
699
700 assert(info == NULL || raise);
701
702#ifdef HAVE_CLOCK_GETTIME
Ronald Oussoren41761932020-11-08 10:05:27 +0100703
704#ifdef HAVE_CLOCK_GETTIME_RUNTIME
705 if (HAVE_CLOCK_GETTIME_RUNTIME) {
706#endif
707
Victor Stinnera47b8812015-03-27 18:16:17 +0100708 err = clock_gettime(CLOCK_REALTIME, &ts);
709 if (err) {
Victor Stinner277c8402017-10-11 08:11:38 -0700710 if (raise) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100711 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinner277c8402017-10-11 08:11:38 -0700712 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100713 return -1;
714 }
Victor Stinnerc29b5852017-11-02 07:28:27 -0700715 if (pytime_fromtimespec(tp, &ts, raise) < 0) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100716 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700717 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100718
719 if (info) {
720 struct timespec res;
721 info->implementation = "clock_gettime(CLOCK_REALTIME)";
722 info->monotonic = 0;
723 info->adjustable = 1;
Victor Stinner277c8402017-10-11 08:11:38 -0700724 if (clock_getres(CLOCK_REALTIME, &res) == 0) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100725 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
Victor Stinner277c8402017-10-11 08:11:38 -0700726 }
727 else {
Victor Stinnera47b8812015-03-27 18:16:17 +0100728 info->resolution = 1e-9;
Victor Stinner277c8402017-10-11 08:11:38 -0700729 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100730 }
Ronald Oussoren41761932020-11-08 10:05:27 +0100731
732#ifdef HAVE_CLOCK_GETTIME_RUNTIME
733 } else {
734#endif
735
736#endif
737
738#if !defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_GETTIME_RUNTIME)
Victor Stinnera47b8812015-03-27 18:16:17 +0100739
740 /* test gettimeofday() */
Victor Stinnera47b8812015-03-27 18:16:17 +0100741 err = gettimeofday(&tv, (struct timezone *)NULL);
Victor Stinnera47b8812015-03-27 18:16:17 +0100742 if (err) {
Victor Stinner277c8402017-10-11 08:11:38 -0700743 if (raise) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100744 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinner277c8402017-10-11 08:11:38 -0700745 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100746 return -1;
747 }
Victor Stinnerc29b5852017-11-02 07:28:27 -0700748 if (pytime_fromtimeval(tp, &tv, raise) < 0) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100749 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700750 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100751
752 if (info) {
753 info->implementation = "gettimeofday()";
754 info->resolution = 1e-6;
755 info->monotonic = 0;
756 info->adjustable = 1;
757 }
Ronald Oussoren41761932020-11-08 10:05:27 +0100758
759#if defined(HAVE_CLOCK_GETTIME_RUNTIME) && defined(HAVE_CLOCK_GETTIME)
760 } /* end of availibity block */
761#endif
762
Victor Stinnera47b8812015-03-27 18:16:17 +0100763#endif /* !HAVE_CLOCK_GETTIME */
764#endif /* !MS_WINDOWS */
765 return 0;
766}
767
Victor Stinner09e5cf22015-03-30 00:09:18 +0200768_PyTime_t
769_PyTime_GetSystemClock(void)
770{
771 _PyTime_t t;
Victor Stinnerc379ade2015-11-10 12:11:39 +0100772 if (pygettimeofday(&t, NULL, 0) < 0) {
Victor Stinner09e5cf22015-03-30 00:09:18 +0200773 /* should not happen, _PyTime_Init() checked the clock at startup */
Serhiy Storchakaeebaa9b2020-03-09 20:49:52 +0200774 Py_FatalError("pygettimeofday() failed");
Victor Stinner09e5cf22015-03-30 00:09:18 +0200775 }
776 return t;
777}
778
Victor Stinnera47b8812015-03-27 18:16:17 +0100779int
780_PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
781{
Victor Stinnerc379ade2015-11-10 12:11:39 +0100782 return pygettimeofday(t, info, 1);
Victor Stinnera47b8812015-03-27 18:16:17 +0100783}
784
Victor Stinnera47b8812015-03-27 18:16:17 +0100785static int
Victor Stinner5ad58212015-09-03 00:14:58 +0200786pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100787{
Victor Stinnercb29f012015-03-27 13:31:18 +0100788#if defined(MS_WINDOWS)
Victor Stinnerc60542b2015-09-10 15:55:07 +0200789 ULONGLONG ticks;
790 _PyTime_t t;
Victor Stinnercb29f012015-03-27 13:31:18 +0100791
792 assert(info == NULL || raise);
793
Victor Stinnerc60542b2015-09-10 15:55:07 +0200794 ticks = GetTickCount64();
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200795 Py_BUILD_ASSERT(sizeof(ticks) <= sizeof(_PyTime_t));
Victor Stinnerc60542b2015-09-10 15:55:07 +0200796 t = (_PyTime_t)ticks;
Victor Stinnercb29f012015-03-27 13:31:18 +0100797
Victor Stinnerc60542b2015-09-10 15:55:07 +0200798 if (_PyTime_check_mul_overflow(t, MS_TO_NS)) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100799 if (raise) {
800 _PyTime_overflow();
801 return -1;
802 }
803 /* Hello, time traveler! */
Serhiy Storchakaeebaa9b2020-03-09 20:49:52 +0200804 Py_FatalError("pymonotonic: integer overflow");
Victor Stinnercb29f012015-03-27 13:31:18 +0100805 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200806 *tp = t * MS_TO_NS;
Victor Stinnercb29f012015-03-27 13:31:18 +0100807
808 if (info) {
809 DWORD timeAdjustment, timeIncrement;
810 BOOL isTimeAdjustmentDisabled, ok;
Victor Stinnereb352292015-03-27 14:12:08 +0100811 info->implementation = "GetTickCount64()";
Victor Stinnercb29f012015-03-27 13:31:18 +0100812 info->monotonic = 1;
813 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
814 &isTimeAdjustmentDisabled);
815 if (!ok) {
816 PyErr_SetFromWindowsErr(0);
817 return -1;
818 }
819 info->resolution = timeIncrement * 1e-7;
820 info->adjustable = 0;
821 }
822
823#elif defined(__APPLE__)
824 static mach_timebase_info_data_t timebase;
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700825 static uint64_t t0 = 0;
826 uint64_t ticks;
Victor Stinnercb29f012015-03-27 13:31:18 +0100827
828 if (timebase.denom == 0) {
829 /* According to the Technical Q&A QA1398, mach_timebase_info() cannot
830 fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
831 (void)mach_timebase_info(&timebase);
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700832
833 /* Sanity check: should never occur in practice */
834 if (timebase.numer < 1 || timebase.denom < 1) {
835 PyErr_SetString(PyExc_RuntimeError,
836 "invalid mach_timebase_info");
837 return -1;
838 }
839
840 /* Check that timebase.numer and timebase.denom can be casted to
luzpaza5293b42017-11-05 07:37:50 -0600841 _PyTime_t. In practice, timebase uses uint32_t, so casting cannot
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700842 overflow. At the end, only make sure that the type is uint32_t
843 (_PyTime_t is 64-bit long). */
844 assert(sizeof(timebase.numer) < sizeof(_PyTime_t));
845 assert(sizeof(timebase.denom) < sizeof(_PyTime_t));
846
847 /* Make sure that (ticks * timebase.numer) cannot overflow in
848 _PyTime_MulDiv(), with ticks < timebase.denom.
849
850 Known time bases:
851
852 * always (1, 1) on Intel
853 * (1000000000, 33333335) or (1000000000, 25000000) on PowerPC
854
855 None of these time bases can overflow with 64-bit _PyTime_t, but
856 check for overflow, just in case. */
857 if ((_PyTime_t)timebase.numer > _PyTime_MAX / (_PyTime_t)timebase.denom) {
858 PyErr_SetString(PyExc_OverflowError,
859 "mach_timebase_info is too large");
860 return -1;
861 }
862
863 t0 = mach_absolute_time();
Victor Stinnercb29f012015-03-27 13:31:18 +0100864 }
865
Victor Stinnercb29f012015-03-27 13:31:18 +0100866 if (info) {
867 info->implementation = "mach_absolute_time()";
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700868 info->resolution = (double)timebase.numer / (double)timebase.denom * 1e-9;
Victor Stinnercb29f012015-03-27 13:31:18 +0100869 info->monotonic = 1;
870 info->adjustable = 0;
871 }
872
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700873 ticks = mach_absolute_time();
874 /* Use a "time zero" to reduce precision loss when converting time
875 to floatting point number, as in time.monotonic(). */
876 ticks -= t0;
877 *tp = _PyTime_MulDiv(ticks,
878 (_PyTime_t)timebase.numer,
879 (_PyTime_t)timebase.denom);
880
haneyc90e9602017-06-21 11:18:21 -0700881#elif defined(__hpux)
882 hrtime_t time;
883
884 time = gethrtime();
885 if (time == -1) {
886 if (raise) {
887 PyErr_SetFromErrno(PyExc_OSError);
888 }
889 return -1;
890 }
891
892 *tp = time;
893
894 if (info) {
895 info->implementation = "gethrtime()";
896 info->resolution = 1e-9;
897 info->monotonic = 1;
898 info->adjustable = 0;
899 }
900
Victor Stinnercb29f012015-03-27 13:31:18 +0100901#else
902 struct timespec ts;
903#ifdef CLOCK_HIGHRES
904 const clockid_t clk_id = CLOCK_HIGHRES;
905 const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
906#else
907 const clockid_t clk_id = CLOCK_MONOTONIC;
908 const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
909#endif
910
911 assert(info == NULL || raise);
912
913 if (clock_gettime(clk_id, &ts) != 0) {
914 if (raise) {
915 PyErr_SetFromErrno(PyExc_OSError);
916 return -1;
917 }
918 return -1;
919 }
920
921 if (info) {
922 struct timespec res;
923 info->monotonic = 1;
924 info->implementation = implementation;
925 info->adjustable = 0;
926 if (clock_getres(clk_id, &res) != 0) {
927 PyErr_SetFromErrno(PyExc_OSError);
928 return -1;
929 }
930 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
931 }
Victor Stinnerc29b5852017-11-02 07:28:27 -0700932 if (pytime_fromtimespec(tp, &ts, raise) < 0) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100933 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700934 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100935#endif
Victor Stinnercb29f012015-03-27 13:31:18 +0100936 return 0;
937}
938
939_PyTime_t
940_PyTime_GetMonotonicClock(void)
941{
942 _PyTime_t t;
Victor Stinner5ad58212015-09-03 00:14:58 +0200943 if (pymonotonic(&t, NULL, 0) < 0) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100944 /* should not happen, _PyTime_Init() checked that monotonic clock at
945 startup */
Serhiy Storchakaeebaa9b2020-03-09 20:49:52 +0200946 Py_FatalError("pymonotonic() failed");
Victor Stinnercb29f012015-03-27 13:31:18 +0100947 }
948 return t;
949}
950
Victor Stinner00111242014-08-29 16:31:59 +0200951int
Victor Stinner4bfb4602015-03-27 22:27:24 +0100952_PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
953{
Victor Stinner5ad58212015-09-03 00:14:58 +0200954 return pymonotonic(tp, info, 1);
Victor Stinner4bfb4602015-03-27 22:27:24 +0100955}
956
Victor Stinnera997c7b2017-10-10 02:51:50 -0700957
958#ifdef MS_WINDOWS
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700959static int
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700960win_perf_counter(_PyTime_t *tp, _Py_clock_info_t *info)
Victor Stinnera997c7b2017-10-10 02:51:50 -0700961{
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700962 static LONGLONG frequency = 0;
963 static LONGLONG t0 = 0;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700964 LARGE_INTEGER now;
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700965 LONGLONG ticksll;
966 _PyTime_t ticks;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700967
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700968 if (frequency == 0) {
Victor Stinnera997c7b2017-10-10 02:51:50 -0700969 LARGE_INTEGER freq;
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700970 if (!QueryPerformanceFrequency(&freq)) {
Victor Stinnera997c7b2017-10-10 02:51:50 -0700971 PyErr_SetFromWindowsErr(0);
972 return -1;
973 }
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700974 frequency = freq.QuadPart;
975
976 /* Sanity check: should never occur in practice */
977 if (frequency < 1) {
978 PyErr_SetString(PyExc_RuntimeError,
979 "invalid QueryPerformanceFrequency");
980 return -1;
981 }
982
983 /* Check that frequency can be casted to _PyTime_t.
984
985 Make also sure that (ticks * SEC_TO_NS) cannot overflow in
986 _PyTime_MulDiv(), with ticks < frequency.
987
988 Known QueryPerformanceFrequency() values:
989
990 * 10,000,000 (10 MHz): 100 ns resolution
991 * 3,579,545 Hz (3.6 MHz): 279 ns resolution
992
993 None of these frequencies can overflow with 64-bit _PyTime_t, but
994 check for overflow, just in case. */
995 if (frequency > _PyTime_MAX
996 || frequency > (LONGLONG)_PyTime_MAX / (LONGLONG)SEC_TO_NS) {
997 PyErr_SetString(PyExc_OverflowError,
998 "QueryPerformanceFrequency is too large");
999 return -1;
1000 }
1001
1002 QueryPerformanceCounter(&now);
1003 t0 = now.QuadPart;
Victor Stinnera997c7b2017-10-10 02:51:50 -07001004 }
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -07001005
Victor Stinnera997c7b2017-10-10 02:51:50 -07001006 if (info) {
1007 info->implementation = "QueryPerformanceCounter()";
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -07001008 info->resolution = 1.0 / (double)frequency;
Victor Stinnera997c7b2017-10-10 02:51:50 -07001009 info->monotonic = 1;
1010 info->adjustable = 0;
1011 }
1012
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -07001013 QueryPerformanceCounter(&now);
1014 ticksll = now.QuadPart;
1015
1016 /* Use a "time zero" to reduce precision loss when converting time
1017 to floatting point number, as in time.perf_counter(). */
1018 ticksll -= t0;
1019
1020 /* Make sure that casting LONGLONG to _PyTime_t cannot overflow,
1021 both types are signed */
1022 Py_BUILD_ASSERT(sizeof(ticksll) <= sizeof(ticks));
1023 ticks = (_PyTime_t)ticksll;
1024
1025 *tp = _PyTime_MulDiv(ticks, SEC_TO_NS, (_PyTime_t)frequency);
Victor Stinnercba9a0c2017-10-12 08:51:56 -07001026 return 0;
Victor Stinnera997c7b2017-10-10 02:51:50 -07001027}
1028#endif
1029
1030
1031int
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -07001032_PyTime_GetPerfCounterWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
Victor Stinnera997c7b2017-10-10 02:51:50 -07001033{
1034#ifdef MS_WINDOWS
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -07001035 return win_perf_counter(t, info);
Victor Stinnera997c7b2017-10-10 02:51:50 -07001036#else
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -07001037 return _PyTime_GetMonotonicClockWithInfo(t, info);
Victor Stinnera997c7b2017-10-10 02:51:50 -07001038#endif
1039}
1040
1041
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -07001042_PyTime_t
1043_PyTime_GetPerfCounter(void)
Victor Stinnera997c7b2017-10-10 02:51:50 -07001044{
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -07001045 _PyTime_t t;
1046 if (_PyTime_GetPerfCounterWithInfo(&t, NULL)) {
Serhiy Storchakaeebaa9b2020-03-09 20:49:52 +02001047 Py_FatalError("_PyTime_GetPerfCounterWithInfo() failed");
Victor Stinnera997c7b2017-10-10 02:51:50 -07001048 }
1049 return t;
1050}
1051
1052
Victor Stinner4bfb4602015-03-27 22:27:24 +01001053int
Victor Stinner00111242014-08-29 16:31:59 +02001054_PyTime_Init(void)
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +00001055{
Victor Stinnercba9a0c2017-10-12 08:51:56 -07001056 /* check that time.time(), time.monotonic() and time.perf_counter() clocks
1057 are working properly to not have to check for exceptions at runtime. If
1058 a clock works once, it cannot fail in next calls. */
Victor Stinnercb29f012015-03-27 13:31:18 +01001059 _PyTime_t t;
Victor Stinnera997c7b2017-10-10 02:51:50 -07001060 if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0) {
Victor Stinner00111242014-08-29 16:31:59 +02001061 return -1;
Victor Stinnera997c7b2017-10-10 02:51:50 -07001062 }
1063 if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0) {
Victor Stinnercb29f012015-03-27 13:31:18 +01001064 return -1;
Victor Stinnera997c7b2017-10-10 02:51:50 -07001065 }
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -07001066 if (_PyTime_GetPerfCounterWithInfo(&t, NULL) < 0) {
Victor Stinnera997c7b2017-10-10 02:51:50 -07001067 return -1;
1068 }
Victor Stinner00111242014-08-29 16:31:59 +02001069 return 0;
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +00001070}
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001071
1072int
1073_PyTime_localtime(time_t t, struct tm *tm)
1074{
1075#ifdef MS_WINDOWS
1076 int error;
1077
1078 error = localtime_s(tm, &t);
1079 if (error != 0) {
1080 errno = error;
1081 PyErr_SetFromErrno(PyExc_OSError);
1082 return -1;
1083 }
1084 return 0;
1085#else /* !MS_WINDOWS */
Victor Stinner87094902019-04-09 19:12:26 +02001086
Michael Feltde6f38d2020-02-07 18:56:16 +01001087#if defined(_AIX) && (SIZEOF_TIME_T < 8)
Victor Stinner87094902019-04-09 19:12:26 +02001088 /* bpo-34373: AIX does not return NULL if t is too small or too large */
1089 if (t < -2145916800 /* 1902-01-01 */
1090 || t > 2145916800 /* 2038-01-01 */) {
Michael Felte2926b72018-12-28 14:57:37 +01001091 errno = EINVAL;
Michael Felte2926b72018-12-28 14:57:37 +01001092 PyErr_SetString(PyExc_OverflowError,
Victor Stinner87094902019-04-09 19:12:26 +02001093 "localtime argument out of range");
Michael Felte2926b72018-12-28 14:57:37 +01001094 return -1;
1095 }
1096#endif
Victor Stinner87094902019-04-09 19:12:26 +02001097
1098 errno = 0;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001099 if (localtime_r(&t, tm) == NULL) {
Victor Stinner277c8402017-10-11 08:11:38 -07001100 if (errno == 0) {
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001101 errno = EINVAL;
Victor Stinner277c8402017-10-11 08:11:38 -07001102 }
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001103 PyErr_SetFromErrno(PyExc_OSError);
1104 return -1;
1105 }
1106 return 0;
1107#endif /* MS_WINDOWS */
1108}
1109
1110int
1111_PyTime_gmtime(time_t t, struct tm *tm)
1112{
1113#ifdef MS_WINDOWS
1114 int error;
1115
1116 error = gmtime_s(tm, &t);
1117 if (error != 0) {
1118 errno = error;
1119 PyErr_SetFromErrno(PyExc_OSError);
1120 return -1;
1121 }
1122 return 0;
1123#else /* !MS_WINDOWS */
1124 if (gmtime_r(&t, tm) == NULL) {
1125#ifdef EINVAL
Victor Stinner277c8402017-10-11 08:11:38 -07001126 if (errno == 0) {
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001127 errno = EINVAL;
Victor Stinner277c8402017-10-11 08:11:38 -07001128 }
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001129#endif
1130 PyErr_SetFromErrno(PyExc_OSError);
1131 return -1;
1132 }
1133 return 0;
1134#endif /* MS_WINDOWS */
1135}