blob: 5bf8c568e7b6a88ec9436a399cc3f08b2248bf09 [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)) {
Victor Stinner45cff0c2015-03-30 10:22:16 +0200210 /* volatile avoids unsafe optimization on float enabled by gcc -O3 */
211 volatile double d, err;
Victor Stinnercb29f012015-03-27 13:31:18 +0100212
213 /* convert to a number of nanoseconds */
214 d = PyFloat_AsDouble(obj);
215 d *= 1e9;
216
Victor Stinnera695f832015-03-30 03:57:14 +0200217 if (round == _PyTime_ROUND_CEILING)
Victor Stinnercb29f012015-03-27 13:31:18 +0100218 d = ceil(d);
219 else
220 d = floor(d);
221
222 *t = (_PyTime_t)d;
223 err = d - (double)*t;
224 if (fabs(err) >= 1.0) {
225 _PyTime_overflow();
226 return -1;
227 }
228 return 0;
229 }
230 else {
231#ifdef HAVE_LONG_LONG
232 PY_LONG_LONG sec;
233 sec = PyLong_AsLongLong(obj);
234 assert(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
235#else
236 long sec;
237 sec = PyLong_AsLong(obj);
238 assert(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
239#endif
240 if (sec == -1 && PyErr_Occurred()) {
241 if (PyErr_ExceptionMatches(PyExc_OverflowError))
242 _PyTime_overflow();
243 return -1;
244 }
245 *t = sec * SEC_TO_NS;
246 if (*t / SEC_TO_NS != sec) {
247 _PyTime_overflow();
248 return -1;
249 }
250 return 0;
251 }
252}
253
Victor Stinner4bfb4602015-03-27 22:27:24 +0100254double
255_PyTime_AsSecondsDouble(_PyTime_t t)
256{
257 _PyTime_t sec, ns;
258 /* Divide using integers to avoid rounding issues on the integer part.
259 1e-9 cannot be stored exactly in IEEE 64-bit. */
260 sec = t / SEC_TO_NS;
261 ns = t % SEC_TO_NS;
262 return (double)sec + (double)ns * 1e-9;
263}
264
Victor Stinner992c43f2015-03-27 17:12:45 +0100265PyObject *
266_PyTime_AsNanosecondsObject(_PyTime_t t)
267{
268#ifdef HAVE_LONG_LONG
269 assert(sizeof(PY_LONG_LONG) >= sizeof(_PyTime_t));
270 return PyLong_FromLongLong((PY_LONG_LONG)t);
271#else
272 assert(sizeof(long) >= sizeof(_PyTime_t));
273 return PyLong_FromLong((long)t);
274#endif
275}
276
Victor Stinnercb29f012015-03-27 13:31:18 +0100277static _PyTime_t
278_PyTime_Multiply(_PyTime_t t, unsigned int multiply, _PyTime_round_t round)
279{
280 _PyTime_t k;
281 if (multiply < SEC_TO_NS) {
282 k = SEC_TO_NS / multiply;
Victor Stinnera695f832015-03-30 03:57:14 +0200283 if (round == _PyTime_ROUND_CEILING)
Victor Stinnercb29f012015-03-27 13:31:18 +0100284 return (t + k - 1) / k;
285 else
286 return t / k;
287 }
288 else {
289 k = multiply / SEC_TO_NS;
290 return t * k;
291 }
292}
293
294_PyTime_t
295_PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
296{
297 return _PyTime_Multiply(t, 1000, round);
298}
299
Victor Stinner02937aa2015-03-28 05:02:39 +0100300/* FIXME: write unit tests */
Victor Stinnerf5faad22015-03-28 03:52:05 +0100301_PyTime_t
302_PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
303{
304 return _PyTime_Multiply(t, 1000 * 1000, round);
305}
306
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200307static int
308_PyTime_AsTimeval_impl(_PyTime_t t, struct timeval *tv, _PyTime_round_t round,
309 int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100310{
311 _PyTime_t secs, ns;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100312 int res = 0;
Victor Stinnercb29f012015-03-27 13:31:18 +0100313
314 secs = t / SEC_TO_NS;
315 ns = t % SEC_TO_NS;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100316 if (ns < 0) {
317 ns += SEC_TO_NS;
318 secs -= 1;
319 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100320
321#ifdef MS_WINDOWS
322 /* On Windows, timeval.tv_sec is a long (32 bit),
323 whereas time_t can be 64-bit. */
324 assert(sizeof(tv->tv_sec) == sizeof(long));
325#if SIZEOF_TIME_T > SIZEOF_LONG
326 if (secs > LONG_MAX) {
Victor Stinner95e9cef2015-03-28 01:26:47 +0100327 secs = LONG_MAX;
328 res = -1;
329 }
330 else if (secs < LONG_MIN) {
331 secs = LONG_MIN;
332 res = -1;
Victor Stinnercb29f012015-03-27 13:31:18 +0100333 }
334#endif
335 tv->tv_sec = (long)secs;
336#else
337 /* On OpenBSD 5.4, timeval.tv_sec is a long.
338 Example: long is 64-bit, whereas time_t is 32-bit. */
339 tv->tv_sec = secs;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100340 if ((_PyTime_t)tv->tv_sec != secs)
341 res = -1;
Victor Stinnercb29f012015-03-27 13:31:18 +0100342#endif
343
Victor Stinnera695f832015-03-30 03:57:14 +0200344 if (round == _PyTime_ROUND_CEILING)
Victor Stinnercb29f012015-03-27 13:31:18 +0100345 tv->tv_usec = (int)((ns + US_TO_NS - 1) / US_TO_NS);
346 else
347 tv->tv_usec = (int)(ns / US_TO_NS);
Victor Stinner95e9cef2015-03-28 01:26:47 +0100348
349 if (tv->tv_usec >= SEC_TO_US) {
350 tv->tv_usec -= SEC_TO_US;
351 tv->tv_sec += 1;
352 }
353
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200354 if (res && raise)
355 _PyTime_overflow();
Victor Stinneredddf992015-03-30 02:54:57 +0200356
357 assert(0 <= tv->tv_usec && tv->tv_usec <= 999999);
Victor Stinner95e9cef2015-03-28 01:26:47 +0100358 return res;
Victor Stinnercb29f012015-03-27 13:31:18 +0100359}
360
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200361int
362_PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
363{
364 return _PyTime_AsTimeval_impl(t, tv, round, 1);
365}
366
367int
368_PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
369{
370 return _PyTime_AsTimeval_impl(t, tv, round, 0);
371}
372
Victor Stinnerc3378382015-03-28 05:07:51 +0100373#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
Victor Stinner34dc0f42015-03-27 18:19:03 +0100374int
375_PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
376{
Victor Stinner95e9cef2015-03-28 01:26:47 +0100377 _PyTime_t secs, nsec;
378
379 secs = t / SEC_TO_NS;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100380 nsec = t % SEC_TO_NS;
381 if (nsec < 0) {
382 nsec += SEC_TO_NS;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100383 secs -= 1;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100384 }
Victor Stinner95e9cef2015-03-28 01:26:47 +0100385 ts->tv_sec = (time_t)secs;
386 if ((_PyTime_t)ts->tv_sec != secs) {
Victor Stinner34dc0f42015-03-27 18:19:03 +0100387 _PyTime_overflow();
388 return -1;
389 }
390 ts->tv_nsec = nsec;
Victor Stinneredddf992015-03-30 02:54:57 +0200391
392 assert(0 <= ts->tv_nsec && ts->tv_nsec <= 999999999);
Victor Stinner34dc0f42015-03-27 18:19:03 +0100393 return 0;
394}
395#endif
396
Victor Stinnercb29f012015-03-27 13:31:18 +0100397static int
Victor Stinnera47b8812015-03-27 18:16:17 +0100398pygettimeofday_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
399{
400#ifdef MS_WINDOWS
401 FILETIME system_time;
402 ULARGE_INTEGER large;
403
404 assert(info == NULL || raise);
405
406 GetSystemTimeAsFileTime(&system_time);
407 large.u.LowPart = system_time.dwLowDateTime;
408 large.u.HighPart = system_time.dwHighDateTime;
409 /* 11,644,473,600,000,000,000: number of nanoseconds between
410 the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
411 days). */
412 *tp = large.QuadPart * 100 - 11644473600000000000;
413 if (info) {
414 DWORD timeAdjustment, timeIncrement;
415 BOOL isTimeAdjustmentDisabled, ok;
416
417 info->implementation = "GetSystemTimeAsFileTime()";
418 info->monotonic = 0;
419 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
420 &isTimeAdjustmentDisabled);
421 if (!ok) {
422 PyErr_SetFromWindowsErr(0);
423 return -1;
424 }
425 info->resolution = timeIncrement * 1e-7;
426 info->adjustable = 1;
427 }
428
429#else /* MS_WINDOWS */
430 int err;
431#ifdef HAVE_CLOCK_GETTIME
432 struct timespec ts;
433#else
434 struct timeval tv;
435#endif
436
437 assert(info == NULL || raise);
438
439#ifdef HAVE_CLOCK_GETTIME
440 err = clock_gettime(CLOCK_REALTIME, &ts);
441 if (err) {
442 if (raise)
443 PyErr_SetFromErrno(PyExc_OSError);
444 return -1;
445 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100446 if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
Victor Stinnera47b8812015-03-27 18:16:17 +0100447 return -1;
448
449 if (info) {
450 struct timespec res;
451 info->implementation = "clock_gettime(CLOCK_REALTIME)";
452 info->monotonic = 0;
453 info->adjustable = 1;
454 if (clock_getres(CLOCK_REALTIME, &res) == 0)
455 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
456 else
457 info->resolution = 1e-9;
458 }
459#else /* HAVE_CLOCK_GETTIME */
460
461 /* test gettimeofday() */
462#ifdef GETTIMEOFDAY_NO_TZ
463 err = gettimeofday(&tv);
464#else
465 err = gettimeofday(&tv, (struct timezone *)NULL);
466#endif
467 if (err) {
468 if (raise)
469 PyErr_SetFromErrno(PyExc_OSError);
470 return -1;
471 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100472 if (_PyTime_FromTimeval(tp, &tv, raise) < 0)
Victor Stinnera47b8812015-03-27 18:16:17 +0100473 return -1;
474
475 if (info) {
476 info->implementation = "gettimeofday()";
477 info->resolution = 1e-6;
478 info->monotonic = 0;
479 info->adjustable = 1;
480 }
481#endif /* !HAVE_CLOCK_GETTIME */
482#endif /* !MS_WINDOWS */
483 return 0;
484}
485
Victor Stinner09e5cf22015-03-30 00:09:18 +0200486_PyTime_t
487_PyTime_GetSystemClock(void)
488{
489 _PyTime_t t;
490 if (pygettimeofday_new(&t, NULL, 0) < 0) {
491 /* should not happen, _PyTime_Init() checked the clock at startup */
492 assert(0);
493
494 /* use a fixed value instead of a random value from the stack */
495 t = 0;
496 }
497 return t;
498}
499
Victor Stinnera47b8812015-03-27 18:16:17 +0100500int
501_PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
502{
503 return pygettimeofday_new(t, info, 1);
504}
505
506
507static int
Victor Stinnercb29f012015-03-27 13:31:18 +0100508pymonotonic_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
509{
510#ifdef Py_DEBUG
511 static int last_set = 0;
512 static _PyTime_t last = 0;
513#endif
514#if defined(MS_WINDOWS)
Victor Stinnercb29f012015-03-27 13:31:18 +0100515 ULONGLONG result;
516
517 assert(info == NULL || raise);
518
Victor Stinnereb352292015-03-27 14:12:08 +0100519 result = GetTickCount64();
Victor Stinnercb29f012015-03-27 13:31:18 +0100520
521 *tp = result * MS_TO_NS;
522 if (*tp / MS_TO_NS != result) {
523 if (raise) {
524 _PyTime_overflow();
525 return -1;
526 }
527 /* Hello, time traveler! */
528 assert(0);
529 }
530
531 if (info) {
532 DWORD timeAdjustment, timeIncrement;
533 BOOL isTimeAdjustmentDisabled, ok;
Victor Stinnereb352292015-03-27 14:12:08 +0100534 info->implementation = "GetTickCount64()";
Victor Stinnercb29f012015-03-27 13:31:18 +0100535 info->monotonic = 1;
536 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
537 &isTimeAdjustmentDisabled);
538 if (!ok) {
539 PyErr_SetFromWindowsErr(0);
540 return -1;
541 }
542 info->resolution = timeIncrement * 1e-7;
543 info->adjustable = 0;
544 }
545
546#elif defined(__APPLE__)
547 static mach_timebase_info_data_t timebase;
548 uint64_t time;
549
550 if (timebase.denom == 0) {
551 /* According to the Technical Q&A QA1398, mach_timebase_info() cannot
552 fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
553 (void)mach_timebase_info(&timebase);
554 }
555
556 time = mach_absolute_time();
557
558 /* apply timebase factor */
559 time *= timebase.numer;
560 time /= timebase.denom;
561
562 *tp = time;
563
564 if (info) {
565 info->implementation = "mach_absolute_time()";
566 info->resolution = (double)timebase.numer / timebase.denom * 1e-9;
567 info->monotonic = 1;
568 info->adjustable = 0;
569 }
570
571#else
572 struct timespec ts;
573#ifdef CLOCK_HIGHRES
574 const clockid_t clk_id = CLOCK_HIGHRES;
575 const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
576#else
577 const clockid_t clk_id = CLOCK_MONOTONIC;
578 const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
579#endif
580
581 assert(info == NULL || raise);
582
583 if (clock_gettime(clk_id, &ts) != 0) {
584 if (raise) {
585 PyErr_SetFromErrno(PyExc_OSError);
586 return -1;
587 }
588 return -1;
589 }
590
591 if (info) {
592 struct timespec res;
593 info->monotonic = 1;
594 info->implementation = implementation;
595 info->adjustable = 0;
596 if (clock_getres(clk_id, &res) != 0) {
597 PyErr_SetFromErrno(PyExc_OSError);
598 return -1;
599 }
600 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
601 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100602 if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
Victor Stinnercb29f012015-03-27 13:31:18 +0100603 return -1;
604#endif
605#ifdef Py_DEBUG
606 /* monotonic clock cannot go backward */
607 assert(!last_set || last <= *tp);
608 last = *tp;
609 last_set = 1;
610#endif
611 return 0;
612}
613
614_PyTime_t
615_PyTime_GetMonotonicClock(void)
616{
617 _PyTime_t t;
618 if (pymonotonic_new(&t, NULL, 0) < 0) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100619 /* should not happen, _PyTime_Init() checked that monotonic clock at
620 startup */
Victor Stinnercb29f012015-03-27 13:31:18 +0100621 assert(0);
Victor Stinnercb0c6022015-03-28 05:24:19 +0100622
623 /* use a fixed value instead of a random value from the stack */
Victor Stinnercb29f012015-03-27 13:31:18 +0100624 t = 0;
625 }
626 return t;
627}
628
Victor Stinner00111242014-08-29 16:31:59 +0200629int
Victor Stinner4bfb4602015-03-27 22:27:24 +0100630_PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
631{
632 return pymonotonic_new(tp, info, 1);
633}
634
635int
Victor Stinner00111242014-08-29 16:31:59 +0200636_PyTime_Init(void)
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000637{
Victor Stinnercb29f012015-03-27 13:31:18 +0100638 _PyTime_t t;
639
Victor Stinner00111242014-08-29 16:31:59 +0200640 /* ensure that the system clock works */
Victor Stinnera47b8812015-03-27 18:16:17 +0100641 if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0)
Victor Stinner00111242014-08-29 16:31:59 +0200642 return -1;
Victor Stinnerae586492014-09-02 23:18:25 +0200643
644 /* ensure that the operating system provides a monotonic clock */
Victor Stinnera47b8812015-03-27 18:16:17 +0100645 if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0)
Victor Stinnercb29f012015-03-27 13:31:18 +0100646 return -1;
Victor Stinner00111242014-08-29 16:31:59 +0200647 return 0;
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000648}