blob: 98f29acc38bd7fb31d4a37a8f576220c23788174 [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 Stinnercb29f012015-03-27 13:31:18 +010010/* To millisecond (10^-3) */
Victor Stinner580ef132015-03-20 01:55:04 +010011#define SEC_TO_MS 1000
Victor Stinner580ef132015-03-20 01:55:04 +010012
Victor Stinnercb29f012015-03-27 13:31:18 +010013/* To microseconds (10^-6) */
14#define MS_TO_US 1000
Victor Stinner580ef132015-03-20 01:55:04 +010015#define SEC_TO_US (SEC_TO_MS * MS_TO_US)
16
Victor Stinnercb29f012015-03-27 13:31:18 +010017/* To nanoseconds (10^-9) */
18#define US_TO_NS 1000
19#define MS_TO_NS (MS_TO_US * US_TO_NS)
20#define SEC_TO_NS (SEC_TO_MS * MS_TO_NS)
21
Victor Stinner5d272cc2012-03-13 13:35:55 +010022static void
23error_time_t_overflow(void)
Victor Stinner643cd682012-03-02 22:54:03 +010024{
Victor Stinner5d272cc2012-03-13 13:35:55 +010025 PyErr_SetString(PyExc_OverflowError,
26 "timestamp out of range for platform time_t");
27}
28
Larry Hastings76ad59b2012-05-03 00:30:07 -070029time_t
Victor Stinner5d272cc2012-03-13 13:35:55 +010030_PyLong_AsTime_t(PyObject *obj)
31{
32#if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
33 PY_LONG_LONG val;
34 val = PyLong_AsLongLong(obj);
35#else
36 long val;
37 assert(sizeof(time_t) <= sizeof(long));
38 val = PyLong_AsLong(obj);
39#endif
40 if (val == -1 && PyErr_Occurred()) {
41 if (PyErr_ExceptionMatches(PyExc_OverflowError))
42 error_time_t_overflow();
43 return -1;
44 }
45 return (time_t)val;
46}
47
Larry Hastings6fe20b32012-04-19 15:07:49 -070048PyObject *
49_PyLong_FromTime_t(time_t t)
50{
51#if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
52 return PyLong_FromLongLong((PY_LONG_LONG)t);
53#else
54 assert(sizeof(time_t) <= sizeof(long));
55 return PyLong_FromLong((long)t);
56#endif
57}
58
Victor Stinner5d272cc2012-03-13 13:35:55 +010059static int
60_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
Victor Stinner3c1b3792014-02-17 00:02:43 +010061 double denominator, _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +010062{
63 assert(denominator <= LONG_MAX);
Victor Stinner643cd682012-03-02 22:54:03 +010064 if (PyFloat_Check(obj)) {
Victor Stinnerbd273c12012-03-13 19:12:23 +010065 double d, intpart, err;
66 /* volatile avoids unsafe optimization on float enabled by gcc -O3 */
67 volatile double floatpart;
Victor Stinner643cd682012-03-02 22:54:03 +010068
69 d = PyFloat_AsDouble(obj);
70 floatpart = modf(d, &intpart);
71 if (floatpart < 0) {
72 floatpart = 1.0 + floatpart;
73 intpart -= 1.0;
74 }
75
Victor Stinner3c1b3792014-02-17 00:02:43 +010076 floatpart *= denominator;
Victor Stinnera695f832015-03-30 03:57:14 +020077 if (round == _PyTime_ROUND_CEILING) {
Victor Stinnerf81f0f92015-03-30 00:44:06 +020078 floatpart = ceil(floatpart);
79 if (floatpart >= denominator) {
80 floatpart = 0.0;
81 intpart += 1.0;
Victor Stinner3c1b3792014-02-17 00:02:43 +010082 }
Victor Stinnerf81f0f92015-03-30 00:44:06 +020083 }
84 else {
85 floatpart = floor(floatpart);
Victor Stinner3c1b3792014-02-17 00:02:43 +010086 }
87
Victor Stinner643cd682012-03-02 22:54:03 +010088 *sec = (time_t)intpart;
89 err = intpart - (double)*sec;
Victor Stinner5d272cc2012-03-13 13:35:55 +010090 if (err <= -1.0 || err >= 1.0) {
91 error_time_t_overflow();
92 return -1;
93 }
Victor Stinner643cd682012-03-02 22:54:03 +010094
Victor Stinner5d272cc2012-03-13 13:35:55 +010095 *numerator = (long)floatpart;
Victor Stinner643cd682012-03-02 22:54:03 +010096 return 0;
97 }
98 else {
Victor Stinner5d272cc2012-03-13 13:35:55 +010099 *sec = _PyLong_AsTime_t(obj);
100 if (*sec == (time_t)-1 && PyErr_Occurred())
101 return -1;
102 *numerator = 0;
Victor Stinner643cd682012-03-02 22:54:03 +0100103 return 0;
104 }
Victor Stinner5d272cc2012-03-13 13:35:55 +0100105}
Victor Stinner643cd682012-03-02 22:54:03 +0100106
Victor Stinner5d272cc2012-03-13 13:35:55 +0100107int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100108_PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100109{
110 if (PyFloat_Check(obj)) {
111 double d, intpart, err;
112
Victor Stinner5d272cc2012-03-13 13:35:55 +0100113 d = PyFloat_AsDouble(obj);
Victor Stinnera695f832015-03-30 03:57:14 +0200114 if (round == _PyTime_ROUND_CEILING)
Victor Stinnerf81f0f92015-03-30 00:44:06 +0200115 d = ceil(d);
116 else
117 d = floor(d);
Victor Stinner5d272cc2012-03-13 13:35:55 +0100118 (void)modf(d, &intpart);
119
120 *sec = (time_t)intpart;
121 err = intpart - (double)*sec;
122 if (err <= -1.0 || err >= 1.0) {
123 error_time_t_overflow();
124 return -1;
125 }
126 return 0;
127 }
128 else {
129 *sec = _PyLong_AsTime_t(obj);
130 if (*sec == (time_t)-1 && PyErr_Occurred())
131 return -1;
132 return 0;
133 }
134}
135
136int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100137_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
138 _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100139{
Victor Stinner3c1b3792014-02-17 00:02:43 +0100140 return _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
Victor Stinner5d272cc2012-03-13 13:35:55 +0100141}
142
143int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100144_PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
145 _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100146{
Victor Stinner3c1b3792014-02-17 00:02:43 +0100147 return _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
Victor Stinner643cd682012-03-02 22:54:03 +0100148}
149
Victor Stinnercb29f012015-03-27 13:31:18 +0100150static void
151_PyTime_overflow(void)
152{
153 PyErr_SetString(PyExc_OverflowError,
154 "timestamp too large to convert to C _PyTime_t");
155}
156
Victor Stinner4bfb4602015-03-27 22:27:24 +0100157_PyTime_t
158_PyTime_FromNanoseconds(PY_LONG_LONG ns)
159{
160 _PyTime_t t;
161 assert(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
162 t = Py_SAFE_DOWNCAST(ns, PY_LONG_LONG, _PyTime_t);
163 return t;
164}
165
Victor Stinnera47b8812015-03-27 18:16:17 +0100166#ifdef HAVE_CLOCK_GETTIME
Victor Stinnercb29f012015-03-27 13:31:18 +0100167static int
Victor Stinnercb0c6022015-03-28 05:24:19 +0100168_PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100169{
170 _PyTime_t t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100171 int res = 0;
172
Victor Stinnercb29f012015-03-27 13:31:18 +0100173 t = (_PyTime_t)ts->tv_sec * SEC_TO_NS;
174 if (t / SEC_TO_NS != ts->tv_sec) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100175 if (raise)
176 _PyTime_overflow();
177 res = -1;
Victor Stinnercb29f012015-03-27 13:31:18 +0100178 }
179
180 t += ts->tv_nsec;
181
182 *tp = t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100183 return res;
Victor Stinnercb29f012015-03-27 13:31:18 +0100184}
Victor Stinner1bd18ba2015-03-30 00:25:38 +0200185#elif !defined(MS_WINDOWS)
Victor Stinnera47b8812015-03-27 18:16:17 +0100186static int
Victor Stinnercb0c6022015-03-28 05:24:19 +0100187_PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
Victor Stinnera47b8812015-03-27 18:16:17 +0100188{
189 _PyTime_t t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100190 int res = 0;
Victor Stinnera47b8812015-03-27 18:16:17 +0100191
192 t = (_PyTime_t)tv->tv_sec * SEC_TO_NS;
193 if (t / SEC_TO_NS != tv->tv_sec) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100194 if (raise)
195 _PyTime_overflow();
196 res = -1;
Victor Stinnera47b8812015-03-27 18:16:17 +0100197 }
198
199 t += (_PyTime_t)tv->tv_usec * US_TO_NS;
200
201 *tp = t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100202 return res;
Victor Stinnera47b8812015-03-27 18:16:17 +0100203}
Victor Stinnercb29f012015-03-27 13:31:18 +0100204#endif
205
206int
Victor Stinner992c43f2015-03-27 17:12:45 +0100207_PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
Victor Stinnercb29f012015-03-27 13:31:18 +0100208{
209 if (PyFloat_Check(obj)) {
210 double d, err;
211
212 /* convert to a number of nanoseconds */
213 d = PyFloat_AsDouble(obj);
214 d *= 1e9;
215
Victor Stinnera695f832015-03-30 03:57:14 +0200216 if (round == _PyTime_ROUND_CEILING)
Victor Stinnercb29f012015-03-27 13:31:18 +0100217 d = ceil(d);
218 else
219 d = floor(d);
220
221 *t = (_PyTime_t)d;
222 err = d - (double)*t;
223 if (fabs(err) >= 1.0) {
224 _PyTime_overflow();
225 return -1;
226 }
227 return 0;
228 }
229 else {
230#ifdef HAVE_LONG_LONG
231 PY_LONG_LONG sec;
232 sec = PyLong_AsLongLong(obj);
233 assert(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
234#else
235 long sec;
236 sec = PyLong_AsLong(obj);
237 assert(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
238#endif
239 if (sec == -1 && PyErr_Occurred()) {
240 if (PyErr_ExceptionMatches(PyExc_OverflowError))
241 _PyTime_overflow();
242 return -1;
243 }
244 *t = sec * SEC_TO_NS;
245 if (*t / SEC_TO_NS != sec) {
246 _PyTime_overflow();
247 return -1;
248 }
249 return 0;
250 }
251}
252
Victor Stinner4bfb4602015-03-27 22:27:24 +0100253double
254_PyTime_AsSecondsDouble(_PyTime_t t)
255{
256 _PyTime_t sec, ns;
257 /* Divide using integers to avoid rounding issues on the integer part.
258 1e-9 cannot be stored exactly in IEEE 64-bit. */
259 sec = t / SEC_TO_NS;
260 ns = t % SEC_TO_NS;
261 return (double)sec + (double)ns * 1e-9;
262}
263
Victor Stinner992c43f2015-03-27 17:12:45 +0100264PyObject *
265_PyTime_AsNanosecondsObject(_PyTime_t t)
266{
267#ifdef HAVE_LONG_LONG
268 assert(sizeof(PY_LONG_LONG) >= sizeof(_PyTime_t));
269 return PyLong_FromLongLong((PY_LONG_LONG)t);
270#else
271 assert(sizeof(long) >= sizeof(_PyTime_t));
272 return PyLong_FromLong((long)t);
273#endif
274}
275
Victor Stinnercb29f012015-03-27 13:31:18 +0100276static _PyTime_t
277_PyTime_Multiply(_PyTime_t t, unsigned int multiply, _PyTime_round_t round)
278{
279 _PyTime_t k;
280 if (multiply < SEC_TO_NS) {
281 k = SEC_TO_NS / multiply;
Victor Stinnera695f832015-03-30 03:57:14 +0200282 if (round == _PyTime_ROUND_CEILING)
Victor Stinnercb29f012015-03-27 13:31:18 +0100283 return (t + k - 1) / k;
284 else
285 return t / k;
286 }
287 else {
288 k = multiply / SEC_TO_NS;
289 return t * k;
290 }
291}
292
293_PyTime_t
294_PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
295{
296 return _PyTime_Multiply(t, 1000, round);
297}
298
Victor Stinner02937aa2015-03-28 05:02:39 +0100299/* FIXME: write unit tests */
Victor Stinnerf5faad22015-03-28 03:52:05 +0100300_PyTime_t
301_PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
302{
303 return _PyTime_Multiply(t, 1000 * 1000, round);
304}
305
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200306static int
307_PyTime_AsTimeval_impl(_PyTime_t t, struct timeval *tv, _PyTime_round_t round,
308 int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100309{
310 _PyTime_t secs, ns;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100311 int res = 0;
Victor Stinnercb29f012015-03-27 13:31:18 +0100312
313 secs = t / SEC_TO_NS;
314 ns = t % SEC_TO_NS;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100315 if (ns < 0) {
316 ns += SEC_TO_NS;
317 secs -= 1;
318 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100319
320#ifdef MS_WINDOWS
321 /* On Windows, timeval.tv_sec is a long (32 bit),
322 whereas time_t can be 64-bit. */
323 assert(sizeof(tv->tv_sec) == sizeof(long));
324#if SIZEOF_TIME_T > SIZEOF_LONG
325 if (secs > LONG_MAX) {
Victor Stinner95e9cef2015-03-28 01:26:47 +0100326 secs = LONG_MAX;
327 res = -1;
328 }
329 else if (secs < LONG_MIN) {
330 secs = LONG_MIN;
331 res = -1;
Victor Stinnercb29f012015-03-27 13:31:18 +0100332 }
333#endif
334 tv->tv_sec = (long)secs;
335#else
336 /* On OpenBSD 5.4, timeval.tv_sec is a long.
337 Example: long is 64-bit, whereas time_t is 32-bit. */
338 tv->tv_sec = secs;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100339 if ((_PyTime_t)tv->tv_sec != secs)
340 res = -1;
Victor Stinnercb29f012015-03-27 13:31:18 +0100341#endif
342
Victor Stinnera695f832015-03-30 03:57:14 +0200343 if (round == _PyTime_ROUND_CEILING)
Victor Stinnercb29f012015-03-27 13:31:18 +0100344 tv->tv_usec = (int)((ns + US_TO_NS - 1) / US_TO_NS);
345 else
346 tv->tv_usec = (int)(ns / US_TO_NS);
Victor Stinner95e9cef2015-03-28 01:26:47 +0100347
348 if (tv->tv_usec >= SEC_TO_US) {
349 tv->tv_usec -= SEC_TO_US;
350 tv->tv_sec += 1;
351 }
352
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200353 if (res && raise)
354 _PyTime_overflow();
Victor Stinneredddf992015-03-30 02:54:57 +0200355
356 assert(0 <= tv->tv_usec && tv->tv_usec <= 999999);
Victor Stinner95e9cef2015-03-28 01:26:47 +0100357 return res;
Victor Stinnercb29f012015-03-27 13:31:18 +0100358}
359
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200360int
361_PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
362{
363 return _PyTime_AsTimeval_impl(t, tv, round, 1);
364}
365
366int
367_PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
368{
369 return _PyTime_AsTimeval_impl(t, tv, round, 0);
370}
371
Victor Stinnerc3378382015-03-28 05:07:51 +0100372#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
Victor Stinner34dc0f42015-03-27 18:19:03 +0100373int
374_PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
375{
Victor Stinner95e9cef2015-03-28 01:26:47 +0100376 _PyTime_t secs, nsec;
377
378 secs = t / SEC_TO_NS;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100379 nsec = t % SEC_TO_NS;
380 if (nsec < 0) {
381 nsec += SEC_TO_NS;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100382 secs -= 1;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100383 }
Victor Stinner95e9cef2015-03-28 01:26:47 +0100384 ts->tv_sec = (time_t)secs;
385 if ((_PyTime_t)ts->tv_sec != secs) {
Victor Stinner34dc0f42015-03-27 18:19:03 +0100386 _PyTime_overflow();
387 return -1;
388 }
389 ts->tv_nsec = nsec;
Victor Stinneredddf992015-03-30 02:54:57 +0200390
391 assert(0 <= ts->tv_nsec && ts->tv_nsec <= 999999999);
Victor Stinner34dc0f42015-03-27 18:19:03 +0100392 return 0;
393}
394#endif
395
Victor Stinnercb29f012015-03-27 13:31:18 +0100396static int
Victor Stinnera47b8812015-03-27 18:16:17 +0100397pygettimeofday_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
398{
399#ifdef MS_WINDOWS
400 FILETIME system_time;
401 ULARGE_INTEGER large;
402
403 assert(info == NULL || raise);
404
405 GetSystemTimeAsFileTime(&system_time);
406 large.u.LowPart = system_time.dwLowDateTime;
407 large.u.HighPart = system_time.dwHighDateTime;
408 /* 11,644,473,600,000,000,000: number of nanoseconds between
409 the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
410 days). */
411 *tp = large.QuadPart * 100 - 11644473600000000000;
412 if (info) {
413 DWORD timeAdjustment, timeIncrement;
414 BOOL isTimeAdjustmentDisabled, ok;
415
416 info->implementation = "GetSystemTimeAsFileTime()";
417 info->monotonic = 0;
418 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
419 &isTimeAdjustmentDisabled);
420 if (!ok) {
421 PyErr_SetFromWindowsErr(0);
422 return -1;
423 }
424 info->resolution = timeIncrement * 1e-7;
425 info->adjustable = 1;
426 }
427
428#else /* MS_WINDOWS */
429 int err;
430#ifdef HAVE_CLOCK_GETTIME
431 struct timespec ts;
432#else
433 struct timeval tv;
434#endif
435
436 assert(info == NULL || raise);
437
438#ifdef HAVE_CLOCK_GETTIME
439 err = clock_gettime(CLOCK_REALTIME, &ts);
440 if (err) {
441 if (raise)
442 PyErr_SetFromErrno(PyExc_OSError);
443 return -1;
444 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100445 if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
Victor Stinnera47b8812015-03-27 18:16:17 +0100446 return -1;
447
448 if (info) {
449 struct timespec res;
450 info->implementation = "clock_gettime(CLOCK_REALTIME)";
451 info->monotonic = 0;
452 info->adjustable = 1;
453 if (clock_getres(CLOCK_REALTIME, &res) == 0)
454 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
455 else
456 info->resolution = 1e-9;
457 }
458#else /* HAVE_CLOCK_GETTIME */
459
460 /* test gettimeofday() */
461#ifdef GETTIMEOFDAY_NO_TZ
462 err = gettimeofday(&tv);
463#else
464 err = gettimeofday(&tv, (struct timezone *)NULL);
465#endif
466 if (err) {
467 if (raise)
468 PyErr_SetFromErrno(PyExc_OSError);
469 return -1;
470 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100471 if (_PyTime_FromTimeval(tp, &tv, raise) < 0)
Victor Stinnera47b8812015-03-27 18:16:17 +0100472 return -1;
473
474 if (info) {
475 info->implementation = "gettimeofday()";
476 info->resolution = 1e-6;
477 info->monotonic = 0;
478 info->adjustable = 1;
479 }
480#endif /* !HAVE_CLOCK_GETTIME */
481#endif /* !MS_WINDOWS */
482 return 0;
483}
484
Victor Stinner09e5cf22015-03-30 00:09:18 +0200485_PyTime_t
486_PyTime_GetSystemClock(void)
487{
488 _PyTime_t t;
489 if (pygettimeofday_new(&t, NULL, 0) < 0) {
490 /* should not happen, _PyTime_Init() checked the clock at startup */
491 assert(0);
492
493 /* use a fixed value instead of a random value from the stack */
494 t = 0;
495 }
496 return t;
497}
498
Victor Stinnera47b8812015-03-27 18:16:17 +0100499int
500_PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
501{
502 return pygettimeofday_new(t, info, 1);
503}
504
505
506static int
Victor Stinnercb29f012015-03-27 13:31:18 +0100507pymonotonic_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
508{
509#ifdef Py_DEBUG
510 static int last_set = 0;
511 static _PyTime_t last = 0;
512#endif
513#if defined(MS_WINDOWS)
Victor Stinnercb29f012015-03-27 13:31:18 +0100514 ULONGLONG result;
515
516 assert(info == NULL || raise);
517
Victor Stinnereb352292015-03-27 14:12:08 +0100518 result = GetTickCount64();
Victor Stinnercb29f012015-03-27 13:31:18 +0100519
520 *tp = result * MS_TO_NS;
521 if (*tp / MS_TO_NS != result) {
522 if (raise) {
523 _PyTime_overflow();
524 return -1;
525 }
526 /* Hello, time traveler! */
527 assert(0);
528 }
529
530 if (info) {
531 DWORD timeAdjustment, timeIncrement;
532 BOOL isTimeAdjustmentDisabled, ok;
Victor Stinnereb352292015-03-27 14:12:08 +0100533 info->implementation = "GetTickCount64()";
Victor Stinnercb29f012015-03-27 13:31:18 +0100534 info->monotonic = 1;
535 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
536 &isTimeAdjustmentDisabled);
537 if (!ok) {
538 PyErr_SetFromWindowsErr(0);
539 return -1;
540 }
541 info->resolution = timeIncrement * 1e-7;
542 info->adjustable = 0;
543 }
544
545#elif defined(__APPLE__)
546 static mach_timebase_info_data_t timebase;
547 uint64_t time;
548
549 if (timebase.denom == 0) {
550 /* According to the Technical Q&A QA1398, mach_timebase_info() cannot
551 fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
552 (void)mach_timebase_info(&timebase);
553 }
554
555 time = mach_absolute_time();
556
557 /* apply timebase factor */
558 time *= timebase.numer;
559 time /= timebase.denom;
560
561 *tp = time;
562
563 if (info) {
564 info->implementation = "mach_absolute_time()";
565 info->resolution = (double)timebase.numer / timebase.denom * 1e-9;
566 info->monotonic = 1;
567 info->adjustable = 0;
568 }
569
570#else
571 struct timespec ts;
572#ifdef CLOCK_HIGHRES
573 const clockid_t clk_id = CLOCK_HIGHRES;
574 const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
575#else
576 const clockid_t clk_id = CLOCK_MONOTONIC;
577 const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
578#endif
579
580 assert(info == NULL || raise);
581
582 if (clock_gettime(clk_id, &ts) != 0) {
583 if (raise) {
584 PyErr_SetFromErrno(PyExc_OSError);
585 return -1;
586 }
587 return -1;
588 }
589
590 if (info) {
591 struct timespec res;
592 info->monotonic = 1;
593 info->implementation = implementation;
594 info->adjustable = 0;
595 if (clock_getres(clk_id, &res) != 0) {
596 PyErr_SetFromErrno(PyExc_OSError);
597 return -1;
598 }
599 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
600 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100601 if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
Victor Stinnercb29f012015-03-27 13:31:18 +0100602 return -1;
603#endif
604#ifdef Py_DEBUG
605 /* monotonic clock cannot go backward */
606 assert(!last_set || last <= *tp);
607 last = *tp;
608 last_set = 1;
609#endif
610 return 0;
611}
612
613_PyTime_t
614_PyTime_GetMonotonicClock(void)
615{
616 _PyTime_t t;
617 if (pymonotonic_new(&t, NULL, 0) < 0) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100618 /* should not happen, _PyTime_Init() checked that monotonic clock at
619 startup */
Victor Stinnercb29f012015-03-27 13:31:18 +0100620 assert(0);
Victor Stinnercb0c6022015-03-28 05:24:19 +0100621
622 /* use a fixed value instead of a random value from the stack */
Victor Stinnercb29f012015-03-27 13:31:18 +0100623 t = 0;
624 }
625 return t;
626}
627
Victor Stinner00111242014-08-29 16:31:59 +0200628int
Victor Stinner4bfb4602015-03-27 22:27:24 +0100629_PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
630{
631 return pymonotonic_new(tp, info, 1);
632}
633
634int
Victor Stinner00111242014-08-29 16:31:59 +0200635_PyTime_Init(void)
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000636{
Victor Stinnercb29f012015-03-27 13:31:18 +0100637 _PyTime_t t;
638
Victor Stinner00111242014-08-29 16:31:59 +0200639 /* ensure that the system clock works */
Victor Stinnera47b8812015-03-27 18:16:17 +0100640 if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0)
Victor Stinner00111242014-08-29 16:31:59 +0200641 return -1;
Victor Stinnerae586492014-09-02 23:18:25 +0200642
643 /* ensure that the operating system provides a monotonic clock */
Victor Stinnera47b8812015-03-27 18:16:17 +0100644 if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0)
Victor Stinnercb29f012015-03-27 13:31:18 +0100645 return -1;
Victor Stinner00111242014-08-29 16:31:59 +0200646 return 0;
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000647}