blob: 7fd2a90f3bd4aca2ec36be2ec020b257d09fbcdd [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
Larry Hastings76ad59b2012-05-03 00:30:07 -070045time_t
Victor Stinner5d272cc2012-03-13 13:35:55 +010046_PyLong_AsTime_t(PyObject *obj)
47{
Benjamin Petersoned4aa832016-09-05 17:44:18 -070048#if SIZEOF_TIME_T == SIZEOF_LONG_LONG
Benjamin Petersonaf580df2016-09-06 10:46:49 -070049 long long val;
Victor Stinner5d272cc2012-03-13 13:35:55 +010050 val = PyLong_AsLongLong(obj);
51#else
52 long val;
Serhiy Storchakafad85aa2015-11-07 15:42:38 +020053 Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
Victor Stinner5d272cc2012-03-13 13:35:55 +010054 val = PyLong_AsLong(obj);
55#endif
56 if (val == -1 && PyErr_Occurred()) {
Victor Stinner277c8402017-10-11 08:11:38 -070057 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +010058 error_time_t_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -070059 }
Victor Stinner5d272cc2012-03-13 13:35:55 +010060 return -1;
61 }
62 return (time_t)val;
63}
64
Larry Hastings6fe20b32012-04-19 15:07:49 -070065PyObject *
66_PyLong_FromTime_t(time_t t)
67{
Benjamin Petersoned4aa832016-09-05 17:44:18 -070068#if SIZEOF_TIME_T == SIZEOF_LONG_LONG
Benjamin Petersonaf580df2016-09-06 10:46:49 -070069 return PyLong_FromLongLong((long long)t);
Larry Hastings6fe20b32012-04-19 15:07:49 -070070#else
Serhiy Storchakafad85aa2015-11-07 15:42:38 +020071 Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
Larry Hastings6fe20b32012-04-19 15:07:49 -070072 return PyLong_FromLong((long)t);
73#endif
74}
75
Victor Stinnerce6aa742015-09-09 22:28:09 +020076/* Round to nearest with ties going to nearest even integer
77 (_PyTime_ROUND_HALF_EVEN) */
78static double
Victor Stinner7667f582015-09-09 01:02:23 +020079_PyTime_RoundHalfEven(double x)
Victor Stinner74474232015-09-02 01:43:56 +020080{
Victor Stinner7667f582015-09-09 01:02:23 +020081 double rounded = round(x);
Victor Stinner277c8402017-10-11 08:11:38 -070082 if (fabs(x-rounded) == 0.5) {
Victor Stinner7667f582015-09-09 01:02:23 +020083 /* halfway case: round to even */
84 rounded = 2.0*round(x/2.0);
Victor Stinner277c8402017-10-11 08:11:38 -070085 }
Victor Stinner7667f582015-09-09 01:02:23 +020086 return rounded;
Victor Stinner74474232015-09-02 01:43:56 +020087}
88
Victor Stinner9ae47df2015-09-09 22:28:58 +020089static double
90_PyTime_Round(double x, _PyTime_round_t round)
91{
Victor Stinner1efbeba2015-09-10 11:48:00 +020092 /* volatile avoids optimization changing how numbers are rounded */
93 volatile double d;
94
95 d = x;
Victor Stinner277c8402017-10-11 08:11:38 -070096 if (round == _PyTime_ROUND_HALF_EVEN) {
Victor Stinner1efbeba2015-09-10 11:48:00 +020097 d = _PyTime_RoundHalfEven(d);
Victor Stinner277c8402017-10-11 08:11:38 -070098 }
99 else if (round == _PyTime_ROUND_CEILING) {
Victor Stinner1efbeba2015-09-10 11:48:00 +0200100 d = ceil(d);
Victor Stinner277c8402017-10-11 08:11:38 -0700101 }
102 else {
Victor Stinner1efbeba2015-09-10 11:48:00 +0200103 d = floor(d);
Victor Stinner277c8402017-10-11 08:11:38 -0700104 }
Victor Stinner1efbeba2015-09-10 11:48:00 +0200105 return d;
Victor Stinner9ae47df2015-09-09 22:28:58 +0200106}
107
Victor Stinner5d272cc2012-03-13 13:35:55 +0100108static int
Victor Stinner53e137c2015-09-02 00:49:16 +0200109_PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
Victor Stinner277c8402017-10-11 08:11:38 -0700110 long idenominator, _PyTime_round_t round)
Victor Stinner53e137c2015-09-02 00:49:16 +0200111{
Victor Stinner277c8402017-10-11 08:11:38 -0700112 double denominator = (double)idenominator;
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700113 double intpart;
Victor Stinner24b822e2015-09-02 11:58:56 +0200114 /* volatile avoids optimization changing how numbers are rounded */
Victor Stinner53e137c2015-09-02 00:49:16 +0200115 volatile double floatpart;
116
117 floatpart = modf(d, &intpart);
Victor Stinner53e137c2015-09-02 00:49:16 +0200118
119 floatpart *= denominator;
Victor Stinner9ae47df2015-09-09 22:28:58 +0200120 floatpart = _PyTime_Round(floatpart, round);
Victor Stinner67edcc92015-09-02 10:37:46 +0200121 if (floatpart >= denominator) {
122 floatpart -= denominator;
123 intpart += 1.0;
Victor Stinner53e137c2015-09-02 00:49:16 +0200124 }
Victor Stinneradfefa52015-09-04 23:57:25 +0200125 else if (floatpart < 0) {
126 floatpart += denominator;
127 intpart -= 1.0;
128 }
Victor Stinner67edcc92015-09-02 10:37:46 +0200129 assert(0.0 <= floatpart && floatpart < denominator);
Victor Stinner53e137c2015-09-02 00:49:16 +0200130
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700131 if (!_Py_InIntegralTypeRange(time_t, intpart)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200132 error_time_t_overflow();
133 return -1;
134 }
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700135 *sec = (time_t)intpart;
136 *numerator = (long)floatpart;
Victor Stinner277c8402017-10-11 08:11:38 -0700137 assert(0 <= *numerator && *numerator < idenominator);
Victor Stinner53e137c2015-09-02 00:49:16 +0200138 return 0;
139}
140
141static int
Victor Stinner5d272cc2012-03-13 13:35:55 +0100142_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
Victor Stinner277c8402017-10-11 08:11:38 -0700143 long denominator, _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100144{
Victor Stinner277c8402017-10-11 08:11:38 -0700145 assert(denominator >= 1);
Victor Stinnerbbdda212015-09-02 00:50:43 +0200146
Victor Stinner643cd682012-03-02 22:54:03 +0100147 if (PyFloat_Check(obj)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200148 double d = PyFloat_AsDouble(obj);
Han Lee829dacc2017-09-09 08:05:05 +0900149 if (Py_IS_NAN(d)) {
150 *numerator = 0;
151 PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
152 return -1;
153 }
Victor Stinner53e137c2015-09-02 00:49:16 +0200154 return _PyTime_DoubleToDenominator(d, sec, numerator,
155 denominator, round);
Victor Stinner643cd682012-03-02 22:54:03 +0100156 }
157 else {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100158 *sec = _PyLong_AsTime_t(obj);
Victor Stinner67edcc92015-09-02 10:37:46 +0200159 *numerator = 0;
Victor Stinner277c8402017-10-11 08:11:38 -0700160 if (*sec == (time_t)-1 && PyErr_Occurred()) {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100161 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700162 }
Victor Stinner643cd682012-03-02 22:54:03 +0100163 return 0;
164 }
Victor Stinner5d272cc2012-03-13 13:35:55 +0100165}
Victor Stinner643cd682012-03-02 22:54:03 +0100166
Victor Stinner5d272cc2012-03-13 13:35:55 +0100167int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100168_PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100169{
170 if (PyFloat_Check(obj)) {
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700171 double intpart;
Victor Stinner24b822e2015-09-02 11:58:56 +0200172 /* volatile avoids optimization changing how numbers are rounded */
Victor Stinner5786aef2015-09-03 16:33:16 +0200173 volatile double d;
Victor Stinner5d272cc2012-03-13 13:35:55 +0100174
Victor Stinner5d272cc2012-03-13 13:35:55 +0100175 d = PyFloat_AsDouble(obj);
Han Lee829dacc2017-09-09 08:05:05 +0900176 if (Py_IS_NAN(d)) {
177 PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
178 return -1;
179 }
180
Victor Stinner9ae47df2015-09-09 22:28:58 +0200181 d = _PyTime_Round(d, round);
Victor Stinner5d272cc2012-03-13 13:35:55 +0100182 (void)modf(d, &intpart);
183
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700184 if (!_Py_InIntegralTypeRange(time_t, intpart)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100185 error_time_t_overflow();
186 return -1;
187 }
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700188 *sec = (time_t)intpart;
Victor Stinner5d272cc2012-03-13 13:35:55 +0100189 return 0;
190 }
191 else {
192 *sec = _PyLong_AsTime_t(obj);
Victor Stinner277c8402017-10-11 08:11:38 -0700193 if (*sec == (time_t)-1 && PyErr_Occurred()) {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100194 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700195 }
Victor Stinner5d272cc2012-03-13 13:35:55 +0100196 return 0;
197 }
198}
199
200int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100201_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
202 _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100203{
Victor Stinner277c8402017-10-11 08:11:38 -0700204 return _PyTime_ObjectToDenominator(obj, sec, nsec, SEC_TO_NS, round);
Victor Stinner5d272cc2012-03-13 13:35:55 +0100205}
206
207int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100208_PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
209 _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100210{
Victor Stinner277c8402017-10-11 08:11:38 -0700211 return _PyTime_ObjectToDenominator(obj, sec, usec, SEC_TO_US, round);
Victor Stinnercb29f012015-03-27 13:31:18 +0100212}
213
Victor Stinner4bfb4602015-03-27 22:27:24 +0100214_PyTime_t
Victor Stinner13019fd2015-04-03 13:10:54 +0200215_PyTime_FromSeconds(int seconds)
216{
217 _PyTime_t t;
218 /* ensure that integer overflow cannot happen, int type should have 32
219 bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
220 bits). */
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200221 Py_BUILD_ASSERT(INT_MAX <= _PyTime_MAX / SEC_TO_NS);
222 Py_BUILD_ASSERT(INT_MIN >= _PyTime_MIN / SEC_TO_NS);
Victor Stinner277c8402017-10-11 08:11:38 -0700223
224 t = (_PyTime_t)seconds;
Victor Stinnerbbdda212015-09-02 00:50:43 +0200225 assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
226 || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
227 t *= SEC_TO_NS;
Victor Stinner13019fd2015-04-03 13:10:54 +0200228 return t;
229}
230
231_PyTime_t
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700232_PyTime_FromNanoseconds(long long ns)
Victor Stinner4bfb4602015-03-27 22:27:24 +0100233{
234 _PyTime_t t;
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700235 Py_BUILD_ASSERT(sizeof(long long) <= sizeof(_PyTime_t));
236 t = Py_SAFE_DOWNCAST(ns, long long, _PyTime_t);
Victor Stinner4bfb4602015-03-27 22:27:24 +0100237 return t;
238}
239
Victor Stinnera47b8812015-03-27 18:16:17 +0100240#ifdef HAVE_CLOCK_GETTIME
Victor Stinnercb29f012015-03-27 13:31:18 +0100241static int
Victor Stinnercb0c6022015-03-28 05:24:19 +0100242_PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100243{
244 _PyTime_t t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100245 int res = 0;
246
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200247 Py_BUILD_ASSERT(sizeof(ts->tv_sec) <= sizeof(_PyTime_t));
Victor Stinnerc60542b2015-09-10 15:55:07 +0200248 t = (_PyTime_t)ts->tv_sec;
249
250 if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
Victor Stinner277c8402017-10-11 08:11:38 -0700251 if (raise) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100252 _PyTime_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -0700253 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100254 res = -1;
Victor Stinnercb29f012015-03-27 13:31:18 +0100255 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200256 t = t * SEC_TO_NS;
Victor Stinnercb29f012015-03-27 13:31:18 +0100257
258 t += ts->tv_nsec;
259
260 *tp = t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100261 return res;
Victor Stinnercb29f012015-03-27 13:31:18 +0100262}
Victor Stinner1bd18ba2015-03-30 00:25:38 +0200263#elif !defined(MS_WINDOWS)
Victor Stinnera47b8812015-03-27 18:16:17 +0100264static int
Victor Stinnercb0c6022015-03-28 05:24:19 +0100265_PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
Victor Stinnera47b8812015-03-27 18:16:17 +0100266{
267 _PyTime_t t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100268 int res = 0;
Victor Stinnera47b8812015-03-27 18:16:17 +0100269
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200270 Py_BUILD_ASSERT(sizeof(tv->tv_sec) <= sizeof(_PyTime_t));
Victor Stinnerc60542b2015-09-10 15:55:07 +0200271 t = (_PyTime_t)tv->tv_sec;
272
273 if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
Victor Stinner277c8402017-10-11 08:11:38 -0700274 if (raise) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100275 _PyTime_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -0700276 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100277 res = -1;
Victor Stinnera47b8812015-03-27 18:16:17 +0100278 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200279 t = t * SEC_TO_NS;
Victor Stinnera47b8812015-03-27 18:16:17 +0100280
281 t += (_PyTime_t)tv->tv_usec * US_TO_NS;
282
283 *tp = t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100284 return res;
Victor Stinnera47b8812015-03-27 18:16:17 +0100285}
Victor Stinnercb29f012015-03-27 13:31:18 +0100286#endif
287
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200288static int
Victor Stinnera997c7b2017-10-10 02:51:50 -0700289_PyTime_FromDouble(_PyTime_t *t, double value, _PyTime_round_t round,
290 long unit_to_ns)
Victor Stinner53e137c2015-09-02 00:49:16 +0200291{
Victor Stinner24b822e2015-09-02 11:58:56 +0200292 /* volatile avoids optimization changing how numbers are rounded */
Victor Stinner5786aef2015-09-03 16:33:16 +0200293 volatile double d;
Victor Stinner53e137c2015-09-02 00:49:16 +0200294
295 /* convert to a number of nanoseconds */
296 d = value;
Victor Stinner9ae47df2015-09-09 22:28:58 +0200297 d *= (double)unit_to_ns;
298 d = _PyTime_Round(d, round);
Victor Stinner53e137c2015-09-02 00:49:16 +0200299
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700300 if (!_Py_InIntegralTypeRange(_PyTime_t, d)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200301 _PyTime_overflow();
302 return -1;
303 }
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700304 *t = (_PyTime_t)d;
Victor Stinner53e137c2015-09-02 00:49:16 +0200305 return 0;
306}
307
308static int
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200309_PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
Victor Stinner9ae47df2015-09-09 22:28:58 +0200310 long unit_to_ns)
Victor Stinnercb29f012015-03-27 13:31:18 +0100311{
312 if (PyFloat_Check(obj)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200313 double d;
Victor Stinnercb29f012015-03-27 13:31:18 +0100314 d = PyFloat_AsDouble(obj);
Han Lee829dacc2017-09-09 08:05:05 +0900315 if (Py_IS_NAN(d)) {
316 PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
317 return -1;
318 }
Victor Stinnera997c7b2017-10-10 02:51:50 -0700319 return _PyTime_FromDouble(t, d, round, unit_to_ns);
Victor Stinnercb29f012015-03-27 13:31:18 +0100320 }
321 else {
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700322 long long sec;
323 Py_BUILD_ASSERT(sizeof(long long) <= sizeof(_PyTime_t));
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200324
325 sec = PyLong_AsLongLong(obj);
Victor Stinnercb29f012015-03-27 13:31:18 +0100326 if (sec == -1 && PyErr_Occurred()) {
Victor Stinner277c8402017-10-11 08:11:38 -0700327 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100328 _PyTime_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -0700329 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100330 return -1;
331 }
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200332
Victor Stinnerc60542b2015-09-10 15:55:07 +0200333 if (_PyTime_check_mul_overflow(sec, unit_to_ns)) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100334 _PyTime_overflow();
335 return -1;
336 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200337 *t = sec * unit_to_ns;
Victor Stinnercb29f012015-03-27 13:31:18 +0100338 return 0;
339 }
340}
341
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200342int
343_PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
344{
345 return _PyTime_FromObject(t, obj, round, SEC_TO_NS);
346}
347
348int
349_PyTime_FromMillisecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
350{
351 return _PyTime_FromObject(t, obj, round, MS_TO_NS);
352}
353
Victor Stinner4bfb4602015-03-27 22:27:24 +0100354double
355_PyTime_AsSecondsDouble(_PyTime_t t)
356{
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200357 /* volatile avoids optimization changing how numbers are rounded */
358 volatile double d;
359
Victor Stinner3e2c8d82015-09-09 22:32:48 +0200360 if (t % SEC_TO_NS == 0) {
361 _PyTime_t secs;
362 /* Divide using integers to avoid rounding issues on the integer part.
363 1e-9 cannot be stored exactly in IEEE 64-bit. */
364 secs = t / SEC_TO_NS;
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200365 d = (double)secs;
Victor Stinner3e2c8d82015-09-09 22:32:48 +0200366 }
367 else {
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200368 d = (double)t;
369 d /= 1e9;
Victor Stinner3e2c8d82015-09-09 22:32:48 +0200370 }
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200371 return d;
Victor Stinner4bfb4602015-03-27 22:27:24 +0100372}
373
Victor Stinner992c43f2015-03-27 17:12:45 +0100374PyObject *
375_PyTime_AsNanosecondsObject(_PyTime_t t)
376{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700377 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(_PyTime_t));
378 return PyLong_FromLongLong((long long)t);
Victor Stinner992c43f2015-03-27 17:12:45 +0100379}
380
Victor Stinnercb29f012015-03-27 13:31:18 +0100381static _PyTime_t
Victor Stinner7667f582015-09-09 01:02:23 +0200382_PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
383 const _PyTime_round_t round)
Victor Stinnercb29f012015-03-27 13:31:18 +0100384{
Victor Stinner62d1c702015-04-01 17:47:07 +0200385 assert(k > 1);
Victor Stinner7667f582015-09-09 01:02:23 +0200386 if (round == _PyTime_ROUND_HALF_EVEN) {
387 _PyTime_t x, r, abs_r;
Victor Stinner74474232015-09-02 01:43:56 +0200388 x = t / k;
389 r = t % k;
Victor Stinner7667f582015-09-09 01:02:23 +0200390 abs_r = Py_ABS(r);
391 if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) {
Victor Stinner277c8402017-10-11 08:11:38 -0700392 if (t >= 0) {
Victor Stinner74474232015-09-02 01:43:56 +0200393 x++;
Victor Stinner277c8402017-10-11 08:11:38 -0700394 }
395 else {
Victor Stinner74474232015-09-02 01:43:56 +0200396 x--;
Victor Stinner277c8402017-10-11 08:11:38 -0700397 }
Victor Stinner74474232015-09-02 01:43:56 +0200398 }
399 return x;
400 }
401 else if (round == _PyTime_ROUND_CEILING) {
Victor Stinner277c8402017-10-11 08:11:38 -0700402 if (t >= 0) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100403 return (t + k - 1) / k;
Victor Stinner277c8402017-10-11 08:11:38 -0700404 }
405 else {
Victor Stinner7667f582015-09-09 01:02:23 +0200406 return t / k;
Victor Stinner277c8402017-10-11 08:11:38 -0700407 }
Victor Stinner7667f582015-09-09 01:02:23 +0200408 }
409 else {
Victor Stinner277c8402017-10-11 08:11:38 -0700410 if (t >= 0) {
Victor Stinner7667f582015-09-09 01:02:23 +0200411 return t / k;
Victor Stinner277c8402017-10-11 08:11:38 -0700412 }
413 else {
Victor Stinner62d1c702015-04-01 17:47:07 +0200414 return (t - (k - 1)) / k;
Victor Stinner277c8402017-10-11 08:11:38 -0700415 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100416 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100417}
418
419_PyTime_t
420_PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
421{
Victor Stinner62d1c702015-04-01 17:47:07 +0200422 return _PyTime_Divide(t, NS_TO_MS, round);
Victor Stinnercb29f012015-03-27 13:31:18 +0100423}
424
Victor Stinnerf5faad22015-03-28 03:52:05 +0100425_PyTime_t
426_PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
427{
Victor Stinner62d1c702015-04-01 17:47:07 +0200428 return _PyTime_Divide(t, NS_TO_US, round);
Victor Stinnerf5faad22015-03-28 03:52:05 +0100429}
430
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200431static int
Victor Stinner1e2b6882015-09-18 13:23:02 +0200432_PyTime_AsTimeval_impl(_PyTime_t t, _PyTime_t *p_secs, int *p_us,
433 _PyTime_round_t round)
Victor Stinnercb29f012015-03-27 13:31:18 +0100434{
435 _PyTime_t secs, ns;
Victor Stinner74474232015-09-02 01:43:56 +0200436 int usec;
Victor Stinner1e2b6882015-09-18 13:23:02 +0200437 int res = 0;
Victor Stinnercb29f012015-03-27 13:31:18 +0100438
439 secs = t / SEC_TO_NS;
440 ns = t % SEC_TO_NS;
441
Victor Stinner1e2b6882015-09-18 13:23:02 +0200442 usec = (int)_PyTime_Divide(ns, US_TO_NS, round);
443 if (usec < 0) {
444 usec += SEC_TO_US;
Victor Stinner277c8402017-10-11 08:11:38 -0700445 if (secs != _PyTime_MIN) {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200446 secs -= 1;
Victor Stinner277c8402017-10-11 08:11:38 -0700447 }
448 else {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200449 res = -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700450 }
Victor Stinner1e2b6882015-09-18 13:23:02 +0200451 }
452 else if (usec >= SEC_TO_US) {
453 usec -= SEC_TO_US;
Victor Stinner277c8402017-10-11 08:11:38 -0700454 if (secs != _PyTime_MAX) {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200455 secs += 1;
Victor Stinner277c8402017-10-11 08:11:38 -0700456 }
457 else {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200458 res = -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700459 }
Victor Stinner1e2b6882015-09-18 13:23:02 +0200460 }
461 assert(0 <= usec && usec < SEC_TO_US);
462
463 *p_secs = secs;
464 *p_us = usec;
465
466 return res;
467}
468
469static int
470_PyTime_AsTimevalStruct_impl(_PyTime_t t, struct timeval *tv,
471 _PyTime_round_t round, int raise)
472{
Victor Stinnerb7a8af22015-10-01 08:44:03 +0200473 _PyTime_t secs, secs2;
Victor Stinner1e2b6882015-09-18 13:23:02 +0200474 int us;
475 int res;
476
477 res = _PyTime_AsTimeval_impl(t, &secs, &us, round);
478
Victor Stinnercb29f012015-03-27 13:31:18 +0100479#ifdef MS_WINDOWS
Victor Stinnercb29f012015-03-27 13:31:18 +0100480 tv->tv_sec = (long)secs;
481#else
Victor Stinnercb29f012015-03-27 13:31:18 +0100482 tv->tv_sec = secs;
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200483#endif
Victor Stinner1e2b6882015-09-18 13:23:02 +0200484 tv->tv_usec = us;
Victor Stinnercb29f012015-03-27 13:31:18 +0100485
Victor Stinnerb7a8af22015-10-01 08:44:03 +0200486 secs2 = (_PyTime_t)tv->tv_sec;
487 if (res < 0 || secs2 != secs) {
Victor Stinner277c8402017-10-11 08:11:38 -0700488 if (raise) {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200489 error_time_t_overflow();
Victor Stinner277c8402017-10-11 08:11:38 -0700490 }
Victor Stinner1e2b6882015-09-18 13:23:02 +0200491 return -1;
Victor Stinner74474232015-09-02 01:43:56 +0200492 }
Victor Stinner1e2b6882015-09-18 13:23:02 +0200493 return 0;
Victor Stinnercb29f012015-03-27 13:31:18 +0100494}
495
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200496int
497_PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
498{
Victor Stinner1e2b6882015-09-18 13:23:02 +0200499 return _PyTime_AsTimevalStruct_impl(t, tv, round, 1);
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200500}
501
502int
503_PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
504{
Victor Stinner1e2b6882015-09-18 13:23:02 +0200505 return _PyTime_AsTimevalStruct_impl(t, tv, round, 0);
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200506}
507
Victor Stinner1e2b6882015-09-18 13:23:02 +0200508int
509_PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us,
510 _PyTime_round_t round)
511{
512 _PyTime_t secs;
513 int res;
514
515 res = _PyTime_AsTimeval_impl(t, &secs, us, round);
516
517 *p_secs = secs;
518
519 if (res < 0 || (_PyTime_t)*p_secs != secs) {
520 error_time_t_overflow();
521 return -1;
522 }
523 return 0;
524}
525
526
Victor Stinnerc3378382015-03-28 05:07:51 +0100527#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
Victor Stinner34dc0f42015-03-27 18:19:03 +0100528int
529_PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
530{
Victor Stinner95e9cef2015-03-28 01:26:47 +0100531 _PyTime_t secs, nsec;
532
533 secs = t / SEC_TO_NS;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100534 nsec = t % SEC_TO_NS;
535 if (nsec < 0) {
536 nsec += SEC_TO_NS;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100537 secs -= 1;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100538 }
Victor Stinner95e9cef2015-03-28 01:26:47 +0100539 ts->tv_sec = (time_t)secs;
Victor Stinner29ee6742015-09-03 16:25:45 +0200540 assert(0 <= nsec && nsec < SEC_TO_NS);
541 ts->tv_nsec = nsec;
542
Victor Stinner95e9cef2015-03-28 01:26:47 +0100543 if ((_PyTime_t)ts->tv_sec != secs) {
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200544 error_time_t_overflow();
Victor Stinner34dc0f42015-03-27 18:19:03 +0100545 return -1;
546 }
Victor Stinner34dc0f42015-03-27 18:19:03 +0100547 return 0;
548}
549#endif
550
Victor Stinnercb29f012015-03-27 13:31:18 +0100551static int
Victor Stinnerc379ade2015-11-10 12:11:39 +0100552pygettimeofday(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
Victor Stinnera47b8812015-03-27 18:16:17 +0100553{
554#ifdef MS_WINDOWS
555 FILETIME system_time;
556 ULARGE_INTEGER large;
557
558 assert(info == NULL || raise);
559
560 GetSystemTimeAsFileTime(&system_time);
561 large.u.LowPart = system_time.dwLowDateTime;
562 large.u.HighPart = system_time.dwHighDateTime;
563 /* 11,644,473,600,000,000,000: number of nanoseconds between
564 the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
565 days). */
566 *tp = large.QuadPart * 100 - 11644473600000000000;
567 if (info) {
568 DWORD timeAdjustment, timeIncrement;
569 BOOL isTimeAdjustmentDisabled, ok;
570
571 info->implementation = "GetSystemTimeAsFileTime()";
572 info->monotonic = 0;
573 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
574 &isTimeAdjustmentDisabled);
575 if (!ok) {
576 PyErr_SetFromWindowsErr(0);
577 return -1;
578 }
579 info->resolution = timeIncrement * 1e-7;
580 info->adjustable = 1;
581 }
582
583#else /* MS_WINDOWS */
584 int err;
585#ifdef HAVE_CLOCK_GETTIME
586 struct timespec ts;
587#else
588 struct timeval tv;
589#endif
590
591 assert(info == NULL || raise);
592
593#ifdef HAVE_CLOCK_GETTIME
594 err = clock_gettime(CLOCK_REALTIME, &ts);
595 if (err) {
Victor Stinner277c8402017-10-11 08:11:38 -0700596 if (raise) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100597 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinner277c8402017-10-11 08:11:38 -0700598 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100599 return -1;
600 }
Victor Stinner277c8402017-10-11 08:11:38 -0700601 if (_PyTime_FromTimespec(tp, &ts, raise) < 0) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100602 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700603 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100604
605 if (info) {
606 struct timespec res;
607 info->implementation = "clock_gettime(CLOCK_REALTIME)";
608 info->monotonic = 0;
609 info->adjustable = 1;
Victor Stinner277c8402017-10-11 08:11:38 -0700610 if (clock_getres(CLOCK_REALTIME, &res) == 0) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100611 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
Victor Stinner277c8402017-10-11 08:11:38 -0700612 }
613 else {
Victor Stinnera47b8812015-03-27 18:16:17 +0100614 info->resolution = 1e-9;
Victor Stinner277c8402017-10-11 08:11:38 -0700615 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100616 }
617#else /* HAVE_CLOCK_GETTIME */
618
619 /* test gettimeofday() */
620#ifdef GETTIMEOFDAY_NO_TZ
621 err = gettimeofday(&tv);
622#else
623 err = gettimeofday(&tv, (struct timezone *)NULL);
624#endif
625 if (err) {
Victor Stinner277c8402017-10-11 08:11:38 -0700626 if (raise) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100627 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinner277c8402017-10-11 08:11:38 -0700628 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100629 return -1;
630 }
Victor Stinner277c8402017-10-11 08:11:38 -0700631 if (_PyTime_FromTimeval(tp, &tv, raise) < 0) {
Victor Stinnera47b8812015-03-27 18:16:17 +0100632 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700633 }
Victor Stinnera47b8812015-03-27 18:16:17 +0100634
635 if (info) {
636 info->implementation = "gettimeofday()";
637 info->resolution = 1e-6;
638 info->monotonic = 0;
639 info->adjustable = 1;
640 }
641#endif /* !HAVE_CLOCK_GETTIME */
642#endif /* !MS_WINDOWS */
643 return 0;
644}
645
Victor Stinner09e5cf22015-03-30 00:09:18 +0200646_PyTime_t
647_PyTime_GetSystemClock(void)
648{
649 _PyTime_t t;
Victor Stinnerc379ade2015-11-10 12:11:39 +0100650 if (pygettimeofday(&t, NULL, 0) < 0) {
Victor Stinner09e5cf22015-03-30 00:09:18 +0200651 /* should not happen, _PyTime_Init() checked the clock at startup */
Barry Warsawb2e57942017-09-14 18:13:16 -0700652 Py_UNREACHABLE();
Victor Stinner09e5cf22015-03-30 00:09:18 +0200653 }
654 return t;
655}
656
Victor Stinnera47b8812015-03-27 18:16:17 +0100657int
658_PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
659{
Victor Stinnerc379ade2015-11-10 12:11:39 +0100660 return pygettimeofday(t, info, 1);
Victor Stinnera47b8812015-03-27 18:16:17 +0100661}
662
Victor Stinnera47b8812015-03-27 18:16:17 +0100663static int
Victor Stinner5ad58212015-09-03 00:14:58 +0200664pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100665{
Victor Stinnercb29f012015-03-27 13:31:18 +0100666#if defined(MS_WINDOWS)
Victor Stinnerc60542b2015-09-10 15:55:07 +0200667 ULONGLONG ticks;
668 _PyTime_t t;
Victor Stinnercb29f012015-03-27 13:31:18 +0100669
670 assert(info == NULL || raise);
671
Victor Stinnerc60542b2015-09-10 15:55:07 +0200672 ticks = GetTickCount64();
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200673 Py_BUILD_ASSERT(sizeof(ticks) <= sizeof(_PyTime_t));
Victor Stinnerc60542b2015-09-10 15:55:07 +0200674 t = (_PyTime_t)ticks;
Victor Stinnercb29f012015-03-27 13:31:18 +0100675
Victor Stinnerc60542b2015-09-10 15:55:07 +0200676 if (_PyTime_check_mul_overflow(t, MS_TO_NS)) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100677 if (raise) {
678 _PyTime_overflow();
679 return -1;
680 }
681 /* Hello, time traveler! */
Barry Warsawb2e57942017-09-14 18:13:16 -0700682 Py_UNREACHABLE();
Victor Stinnercb29f012015-03-27 13:31:18 +0100683 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200684 *tp = t * MS_TO_NS;
Victor Stinnercb29f012015-03-27 13:31:18 +0100685
686 if (info) {
687 DWORD timeAdjustment, timeIncrement;
688 BOOL isTimeAdjustmentDisabled, ok;
Victor Stinnereb352292015-03-27 14:12:08 +0100689 info->implementation = "GetTickCount64()";
Victor Stinnercb29f012015-03-27 13:31:18 +0100690 info->monotonic = 1;
691 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
692 &isTimeAdjustmentDisabled);
693 if (!ok) {
694 PyErr_SetFromWindowsErr(0);
695 return -1;
696 }
697 info->resolution = timeIncrement * 1e-7;
698 info->adjustable = 0;
699 }
700
701#elif defined(__APPLE__)
702 static mach_timebase_info_data_t timebase;
703 uint64_t time;
704
705 if (timebase.denom == 0) {
706 /* According to the Technical Q&A QA1398, mach_timebase_info() cannot
707 fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
708 (void)mach_timebase_info(&timebase);
709 }
710
711 time = mach_absolute_time();
712
713 /* apply timebase factor */
714 time *= timebase.numer;
715 time /= timebase.denom;
716
717 *tp = time;
718
719 if (info) {
720 info->implementation = "mach_absolute_time()";
721 info->resolution = (double)timebase.numer / timebase.denom * 1e-9;
722 info->monotonic = 1;
723 info->adjustable = 0;
724 }
725
haneyc90e9602017-06-21 11:18:21 -0700726#elif defined(__hpux)
727 hrtime_t time;
728
729 time = gethrtime();
730 if (time == -1) {
731 if (raise) {
732 PyErr_SetFromErrno(PyExc_OSError);
733 }
734 return -1;
735 }
736
737 *tp = time;
738
739 if (info) {
740 info->implementation = "gethrtime()";
741 info->resolution = 1e-9;
742 info->monotonic = 1;
743 info->adjustable = 0;
744 }
745
Victor Stinnercb29f012015-03-27 13:31:18 +0100746#else
747 struct timespec ts;
748#ifdef CLOCK_HIGHRES
749 const clockid_t clk_id = CLOCK_HIGHRES;
750 const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
751#else
752 const clockid_t clk_id = CLOCK_MONOTONIC;
753 const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
754#endif
755
756 assert(info == NULL || raise);
757
758 if (clock_gettime(clk_id, &ts) != 0) {
759 if (raise) {
760 PyErr_SetFromErrno(PyExc_OSError);
761 return -1;
762 }
763 return -1;
764 }
765
766 if (info) {
767 struct timespec res;
768 info->monotonic = 1;
769 info->implementation = implementation;
770 info->adjustable = 0;
771 if (clock_getres(clk_id, &res) != 0) {
772 PyErr_SetFromErrno(PyExc_OSError);
773 return -1;
774 }
775 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
776 }
Victor Stinner277c8402017-10-11 08:11:38 -0700777 if (_PyTime_FromTimespec(tp, &ts, raise) < 0) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100778 return -1;
Victor Stinner277c8402017-10-11 08:11:38 -0700779 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100780#endif
Victor Stinnercb29f012015-03-27 13:31:18 +0100781 return 0;
782}
783
784_PyTime_t
785_PyTime_GetMonotonicClock(void)
786{
787 _PyTime_t t;
Victor Stinner5ad58212015-09-03 00:14:58 +0200788 if (pymonotonic(&t, NULL, 0) < 0) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100789 /* should not happen, _PyTime_Init() checked that monotonic clock at
790 startup */
Barry Warsawb2e57942017-09-14 18:13:16 -0700791 Py_UNREACHABLE();
Victor Stinnercb29f012015-03-27 13:31:18 +0100792 }
793 return t;
794}
795
Victor Stinner00111242014-08-29 16:31:59 +0200796int
Victor Stinner4bfb4602015-03-27 22:27:24 +0100797_PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
798{
Victor Stinner5ad58212015-09-03 00:14:58 +0200799 return pymonotonic(tp, info, 1);
Victor Stinner4bfb4602015-03-27 22:27:24 +0100800}
801
Victor Stinnera997c7b2017-10-10 02:51:50 -0700802
803#ifdef MS_WINDOWS
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700804static int
805win_perf_counter(double *tp, _Py_clock_info_t *info)
Victor Stinnera997c7b2017-10-10 02:51:50 -0700806{
807 static LONGLONG cpu_frequency = 0;
808 static LONGLONG ctrStart;
809 LARGE_INTEGER now;
810 double diff;
811
812 if (cpu_frequency == 0) {
813 LARGE_INTEGER freq;
814 QueryPerformanceCounter(&now);
815 ctrStart = now.QuadPart;
816 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
817 PyErr_SetFromWindowsErr(0);
818 return -1;
819 }
820 cpu_frequency = freq.QuadPart;
821 }
822 QueryPerformanceCounter(&now);
823 diff = (double)(now.QuadPart - ctrStart);
824 if (info) {
825 info->implementation = "QueryPerformanceCounter()";
826 info->resolution = 1.0 / (double)cpu_frequency;
827 info->monotonic = 1;
828 info->adjustable = 0;
829 }
830
831 diff = diff / (double)cpu_frequency;
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700832 *tp = diff;
833 return 0;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700834}
835#endif
836
837
838int
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700839_PyTime_GetPerfCounterDoubleWithInfo(double *d, _Py_clock_info_t *info)
Victor Stinnera997c7b2017-10-10 02:51:50 -0700840{
841#ifdef MS_WINDOWS
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700842 return win_perf_counter(d, info);
Victor Stinnera997c7b2017-10-10 02:51:50 -0700843#else
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700844 _PyTime_t t;
845 if (_PyTime_GetMonotonicClockWithInfo(&t, info) < 0) {
846 return -1;
847 }
848 *d = _PyTime_AsSecondsDouble(t);
849 return 0;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700850#endif
851}
852
853
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700854double
855_PyTime_GetPerfCounterDouble(void)
Victor Stinnera997c7b2017-10-10 02:51:50 -0700856{
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700857 double t;
858 if (_PyTime_GetPerfCounterDoubleWithInfo(&t, NULL)) {
Victor Stinnera997c7b2017-10-10 02:51:50 -0700859 Py_UNREACHABLE();
860 }
861 return t;
862}
863
864
Victor Stinner4bfb4602015-03-27 22:27:24 +0100865int
Victor Stinner00111242014-08-29 16:31:59 +0200866_PyTime_Init(void)
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000867{
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700868 /* check that time.time(), time.monotonic() and time.perf_counter() clocks
869 are working properly to not have to check for exceptions at runtime. If
870 a clock works once, it cannot fail in next calls. */
Victor Stinnercb29f012015-03-27 13:31:18 +0100871 _PyTime_t t;
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700872 double d;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700873 if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0) {
Victor Stinner00111242014-08-29 16:31:59 +0200874 return -1;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700875 }
876 if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100877 return -1;
Victor Stinnera997c7b2017-10-10 02:51:50 -0700878 }
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700879 if (_PyTime_GetPerfCounterDoubleWithInfo(&d, NULL) < 0) {
Victor Stinnera997c7b2017-10-10 02:51:50 -0700880 return -1;
881 }
Victor Stinner00111242014-08-29 16:31:59 +0200882 return 0;
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000883}
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400884
885int
886_PyTime_localtime(time_t t, struct tm *tm)
887{
888#ifdef MS_WINDOWS
889 int error;
890
891 error = localtime_s(tm, &t);
892 if (error != 0) {
893 errno = error;
894 PyErr_SetFromErrno(PyExc_OSError);
895 return -1;
896 }
897 return 0;
898#else /* !MS_WINDOWS */
899 if (localtime_r(&t, tm) == NULL) {
900#ifdef EINVAL
Victor Stinner277c8402017-10-11 08:11:38 -0700901 if (errno == 0) {
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400902 errno = EINVAL;
Victor Stinner277c8402017-10-11 08:11:38 -0700903 }
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400904#endif
905 PyErr_SetFromErrno(PyExc_OSError);
906 return -1;
907 }
908 return 0;
909#endif /* MS_WINDOWS */
910}
911
912int
913_PyTime_gmtime(time_t t, struct tm *tm)
914{
915#ifdef MS_WINDOWS
916 int error;
917
918 error = gmtime_s(tm, &t);
919 if (error != 0) {
920 errno = error;
921 PyErr_SetFromErrno(PyExc_OSError);
922 return -1;
923 }
924 return 0;
925#else /* !MS_WINDOWS */
926 if (gmtime_r(&t, tm) == NULL) {
927#ifdef EINVAL
Victor Stinner277c8402017-10-11 08:11:38 -0700928 if (errno == 0) {
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400929 errno = EINVAL;
Victor Stinner277c8402017-10-11 08:11:38 -0700930 }
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400931#endif
932 PyErr_SetFromErrno(PyExc_OSError);
933 return -1;
934 }
935 return 0;
936#endif /* MS_WINDOWS */
937}