blob: 02a93749c7168b22ed0b65134615f834f6595c2a [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 Stinner62d1c702015-04-01 17:47:07 +020022/* Conversion from nanoseconds */
23#define NS_TO_MS (1000 * 1000)
24#define NS_TO_US (1000)
25
Victor Stinner5d272cc2012-03-13 13:35:55 +010026static void
27error_time_t_overflow(void)
Victor Stinner643cd682012-03-02 22:54:03 +010028{
Victor Stinner5d272cc2012-03-13 13:35:55 +010029 PyErr_SetString(PyExc_OverflowError,
30 "timestamp out of range for platform time_t");
31}
32
Larry Hastings76ad59b2012-05-03 00:30:07 -070033time_t
Victor Stinner5d272cc2012-03-13 13:35:55 +010034_PyLong_AsTime_t(PyObject *obj)
35{
36#if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
37 PY_LONG_LONG val;
38 val = PyLong_AsLongLong(obj);
39#else
40 long val;
41 assert(sizeof(time_t) <= sizeof(long));
42 val = PyLong_AsLong(obj);
43#endif
44 if (val == -1 && PyErr_Occurred()) {
45 if (PyErr_ExceptionMatches(PyExc_OverflowError))
46 error_time_t_overflow();
47 return -1;
48 }
49 return (time_t)val;
50}
51
Larry Hastings6fe20b32012-04-19 15:07:49 -070052PyObject *
53_PyLong_FromTime_t(time_t t)
54{
55#if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
56 return PyLong_FromLongLong((PY_LONG_LONG)t);
57#else
58 assert(sizeof(time_t) <= sizeof(long));
59 return PyLong_FromLong((long)t);
60#endif
61}
62
Victor Stinner5d272cc2012-03-13 13:35:55 +010063static int
64_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
Victor Stinner3c1b3792014-02-17 00:02:43 +010065 double denominator, _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +010066{
67 assert(denominator <= LONG_MAX);
Victor Stinner643cd682012-03-02 22:54:03 +010068 if (PyFloat_Check(obj)) {
Victor Stinnerbd273c12012-03-13 19:12:23 +010069 double d, intpart, err;
70 /* volatile avoids unsafe optimization on float enabled by gcc -O3 */
71 volatile double floatpart;
Victor Stinner643cd682012-03-02 22:54:03 +010072
73 d = PyFloat_AsDouble(obj);
74 floatpart = modf(d, &intpart);
75 if (floatpart < 0) {
76 floatpart = 1.0 + floatpart;
77 intpart -= 1.0;
78 }
79
Victor Stinner3c1b3792014-02-17 00:02:43 +010080 floatpart *= denominator;
Victor Stinnera695f832015-03-30 03:57:14 +020081 if (round == _PyTime_ROUND_CEILING) {
Victor Stinnerf81f0f92015-03-30 00:44:06 +020082 floatpart = ceil(floatpart);
83 if (floatpart >= denominator) {
84 floatpart = 0.0;
85 intpart += 1.0;
Victor Stinner3c1b3792014-02-17 00:02:43 +010086 }
Victor Stinnerf81f0f92015-03-30 00:44:06 +020087 }
88 else {
89 floatpart = floor(floatpart);
Victor Stinner3c1b3792014-02-17 00:02:43 +010090 }
91
Victor Stinner643cd682012-03-02 22:54:03 +010092 *sec = (time_t)intpart;
93 err = intpart - (double)*sec;
Victor Stinner5d272cc2012-03-13 13:35:55 +010094 if (err <= -1.0 || err >= 1.0) {
95 error_time_t_overflow();
96 return -1;
97 }
Victor Stinner643cd682012-03-02 22:54:03 +010098
Victor Stinner5d272cc2012-03-13 13:35:55 +010099 *numerator = (long)floatpart;
Victor Stinner643cd682012-03-02 22:54:03 +0100100 return 0;
101 }
102 else {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100103 *sec = _PyLong_AsTime_t(obj);
104 if (*sec == (time_t)-1 && PyErr_Occurred())
105 return -1;
106 *numerator = 0;
Victor Stinner643cd682012-03-02 22:54:03 +0100107 return 0;
108 }
Victor Stinner5d272cc2012-03-13 13:35:55 +0100109}
Victor Stinner643cd682012-03-02 22:54:03 +0100110
Victor Stinner5d272cc2012-03-13 13:35:55 +0100111int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100112_PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100113{
114 if (PyFloat_Check(obj)) {
115 double d, intpart, err;
116
Victor Stinner5d272cc2012-03-13 13:35:55 +0100117 d = PyFloat_AsDouble(obj);
Victor Stinnera695f832015-03-30 03:57:14 +0200118 if (round == _PyTime_ROUND_CEILING)
Victor Stinnerf81f0f92015-03-30 00:44:06 +0200119 d = ceil(d);
120 else
121 d = floor(d);
Victor Stinner5d272cc2012-03-13 13:35:55 +0100122 (void)modf(d, &intpart);
123
124 *sec = (time_t)intpart;
125 err = intpart - (double)*sec;
126 if (err <= -1.0 || err >= 1.0) {
127 error_time_t_overflow();
128 return -1;
129 }
130 return 0;
131 }
132 else {
133 *sec = _PyLong_AsTime_t(obj);
134 if (*sec == (time_t)-1 && PyErr_Occurred())
135 return -1;
136 return 0;
137 }
138}
139
140int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100141_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
142 _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100143{
Victor Stinner3c1b3792014-02-17 00:02:43 +0100144 return _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
Victor Stinner5d272cc2012-03-13 13:35:55 +0100145}
146
147int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100148_PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
149 _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100150{
Victor Stinner3c1b3792014-02-17 00:02:43 +0100151 return _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
Victor Stinner643cd682012-03-02 22:54:03 +0100152}
153
Victor Stinnercb29f012015-03-27 13:31:18 +0100154static void
155_PyTime_overflow(void)
156{
157 PyErr_SetString(PyExc_OverflowError,
158 "timestamp too large to convert to C _PyTime_t");
159}
160
Victor Stinner4bfb4602015-03-27 22:27:24 +0100161_PyTime_t
Victor Stinner13019fd2015-04-03 13:10:54 +0200162_PyTime_FromSeconds(int seconds)
163{
164 _PyTime_t t;
165 /* ensure that integer overflow cannot happen, int type should have 32
166 bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
167 bits). */
168 assert((seconds >= 0 && seconds <= _PyTime_MAX / SEC_TO_NS)
169 || (seconds < 0 && seconds >= _PyTime_MIN / SEC_TO_NS));
170 t = (_PyTime_t)seconds * SEC_TO_NS;
171 return t;
172}
173
174_PyTime_t
Victor Stinner4bfb4602015-03-27 22:27:24 +0100175_PyTime_FromNanoseconds(PY_LONG_LONG ns)
176{
177 _PyTime_t t;
178 assert(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
179 t = Py_SAFE_DOWNCAST(ns, PY_LONG_LONG, _PyTime_t);
180 return t;
181}
182
Victor Stinnera47b8812015-03-27 18:16:17 +0100183#ifdef HAVE_CLOCK_GETTIME
Victor Stinnercb29f012015-03-27 13:31:18 +0100184static int
Victor Stinnercb0c6022015-03-28 05:24:19 +0100185_PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100186{
187 _PyTime_t t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100188 int res = 0;
189
Victor Stinnercb29f012015-03-27 13:31:18 +0100190 t = (_PyTime_t)ts->tv_sec * SEC_TO_NS;
191 if (t / SEC_TO_NS != ts->tv_sec) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100192 if (raise)
193 _PyTime_overflow();
194 res = -1;
Victor Stinnercb29f012015-03-27 13:31:18 +0100195 }
196
197 t += ts->tv_nsec;
198
199 *tp = t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100200 return res;
Victor Stinnercb29f012015-03-27 13:31:18 +0100201}
Victor Stinner1bd18ba2015-03-30 00:25:38 +0200202#elif !defined(MS_WINDOWS)
Victor Stinnera47b8812015-03-27 18:16:17 +0100203static int
Victor Stinnercb0c6022015-03-28 05:24:19 +0100204_PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
Victor Stinnera47b8812015-03-27 18:16:17 +0100205{
206 _PyTime_t t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100207 int res = 0;
Victor Stinnera47b8812015-03-27 18:16:17 +0100208
209 t = (_PyTime_t)tv->tv_sec * SEC_TO_NS;
210 if (t / SEC_TO_NS != tv->tv_sec) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100211 if (raise)
212 _PyTime_overflow();
213 res = -1;
Victor Stinnera47b8812015-03-27 18:16:17 +0100214 }
215
216 t += (_PyTime_t)tv->tv_usec * US_TO_NS;
217
218 *tp = t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100219 return res;
Victor Stinnera47b8812015-03-27 18:16:17 +0100220}
Victor Stinnercb29f012015-03-27 13:31:18 +0100221#endif
222
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200223static int
224_PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
225 long to_nanoseconds)
Victor Stinnercb29f012015-03-27 13:31:18 +0100226{
227 if (PyFloat_Check(obj)) {
Victor Stinner45cff0c2015-03-30 10:22:16 +0200228 /* volatile avoids unsafe optimization on float enabled by gcc -O3 */
229 volatile double d, err;
Victor Stinnercb29f012015-03-27 13:31:18 +0100230
231 /* convert to a number of nanoseconds */
232 d = PyFloat_AsDouble(obj);
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200233 d *= to_nanoseconds;
Victor Stinnercb29f012015-03-27 13:31:18 +0100234
Victor Stinnera695f832015-03-30 03:57:14 +0200235 if (round == _PyTime_ROUND_CEILING)
Victor Stinnercb29f012015-03-27 13:31:18 +0100236 d = ceil(d);
237 else
238 d = floor(d);
239
240 *t = (_PyTime_t)d;
241 err = d - (double)*t;
242 if (fabs(err) >= 1.0) {
243 _PyTime_overflow();
244 return -1;
245 }
246 return 0;
247 }
248 else {
249#ifdef HAVE_LONG_LONG
250 PY_LONG_LONG sec;
251 sec = PyLong_AsLongLong(obj);
252 assert(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
253#else
254 long sec;
255 sec = PyLong_AsLong(obj);
256 assert(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
257#endif
258 if (sec == -1 && PyErr_Occurred()) {
259 if (PyErr_ExceptionMatches(PyExc_OverflowError))
260 _PyTime_overflow();
261 return -1;
262 }
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200263 *t = sec * to_nanoseconds;
264 if (*t / to_nanoseconds != sec) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100265 _PyTime_overflow();
266 return -1;
267 }
268 return 0;
269 }
270}
271
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200272int
273_PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
274{
275 return _PyTime_FromObject(t, obj, round, SEC_TO_NS);
276}
277
278int
279_PyTime_FromMillisecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
280{
281 return _PyTime_FromObject(t, obj, round, MS_TO_NS);
282}
283
Victor Stinner4bfb4602015-03-27 22:27:24 +0100284double
285_PyTime_AsSecondsDouble(_PyTime_t t)
286{
287 _PyTime_t sec, ns;
288 /* Divide using integers to avoid rounding issues on the integer part.
289 1e-9 cannot be stored exactly in IEEE 64-bit. */
290 sec = t / SEC_TO_NS;
291 ns = t % SEC_TO_NS;
292 return (double)sec + (double)ns * 1e-9;
293}
294
Victor Stinner992c43f2015-03-27 17:12:45 +0100295PyObject *
296_PyTime_AsNanosecondsObject(_PyTime_t t)
297{
298#ifdef HAVE_LONG_LONG
299 assert(sizeof(PY_LONG_LONG) >= sizeof(_PyTime_t));
300 return PyLong_FromLongLong((PY_LONG_LONG)t);
301#else
302 assert(sizeof(long) >= sizeof(_PyTime_t));
303 return PyLong_FromLong((long)t);
304#endif
305}
306
Victor Stinnercb29f012015-03-27 13:31:18 +0100307static _PyTime_t
Victor Stinner62d1c702015-04-01 17:47:07 +0200308_PyTime_Divide(_PyTime_t t, _PyTime_t k, _PyTime_round_t round)
Victor Stinnercb29f012015-03-27 13:31:18 +0100309{
Victor Stinner62d1c702015-04-01 17:47:07 +0200310 assert(k > 1);
311 if (round == _PyTime_ROUND_CEILING) {
312 if (t >= 0)
Victor Stinnercb29f012015-03-27 13:31:18 +0100313 return (t + k - 1) / k;
314 else
Victor Stinner62d1c702015-04-01 17:47:07 +0200315 return (t - (k - 1)) / k;
Victor Stinnercb29f012015-03-27 13:31:18 +0100316 }
Victor Stinner62d1c702015-04-01 17:47:07 +0200317 else
318 return t / k;
Victor Stinnercb29f012015-03-27 13:31:18 +0100319}
320
321_PyTime_t
322_PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
323{
Victor Stinner62d1c702015-04-01 17:47:07 +0200324 return _PyTime_Divide(t, NS_TO_MS, round);
Victor Stinnercb29f012015-03-27 13:31:18 +0100325}
326
Victor Stinnerf5faad22015-03-28 03:52:05 +0100327_PyTime_t
328_PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
329{
Victor Stinner62d1c702015-04-01 17:47:07 +0200330 return _PyTime_Divide(t, NS_TO_US, round);
Victor Stinnerf5faad22015-03-28 03:52:05 +0100331}
332
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200333static int
334_PyTime_AsTimeval_impl(_PyTime_t t, struct timeval *tv, _PyTime_round_t round,
335 int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100336{
337 _PyTime_t secs, ns;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100338 int res = 0;
Victor Stinnercb29f012015-03-27 13:31:18 +0100339
340 secs = t / SEC_TO_NS;
341 ns = t % SEC_TO_NS;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100342 if (ns < 0) {
343 ns += SEC_TO_NS;
344 secs -= 1;
345 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100346
347#ifdef MS_WINDOWS
348 /* On Windows, timeval.tv_sec is a long (32 bit),
349 whereas time_t can be 64-bit. */
350 assert(sizeof(tv->tv_sec) == sizeof(long));
351#if SIZEOF_TIME_T > SIZEOF_LONG
352 if (secs > LONG_MAX) {
Victor Stinner95e9cef2015-03-28 01:26:47 +0100353 secs = LONG_MAX;
354 res = -1;
355 }
356 else if (secs < LONG_MIN) {
357 secs = LONG_MIN;
358 res = -1;
Victor Stinnercb29f012015-03-27 13:31:18 +0100359 }
360#endif
361 tv->tv_sec = (long)secs;
362#else
363 /* On OpenBSD 5.4, timeval.tv_sec is a long.
364 Example: long is 64-bit, whereas time_t is 32-bit. */
365 tv->tv_sec = secs;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100366 if ((_PyTime_t)tv->tv_sec != secs)
367 res = -1;
Victor Stinnercb29f012015-03-27 13:31:18 +0100368#endif
369
Victor Stinnera695f832015-03-30 03:57:14 +0200370 if (round == _PyTime_ROUND_CEILING)
Victor Stinnercb29f012015-03-27 13:31:18 +0100371 tv->tv_usec = (int)((ns + US_TO_NS - 1) / US_TO_NS);
372 else
373 tv->tv_usec = (int)(ns / US_TO_NS);
Victor Stinner95e9cef2015-03-28 01:26:47 +0100374
375 if (tv->tv_usec >= SEC_TO_US) {
376 tv->tv_usec -= SEC_TO_US;
377 tv->tv_sec += 1;
378 }
379
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200380 if (res && raise)
381 _PyTime_overflow();
Victor Stinneredddf992015-03-30 02:54:57 +0200382
383 assert(0 <= tv->tv_usec && tv->tv_usec <= 999999);
Victor Stinner95e9cef2015-03-28 01:26:47 +0100384 return res;
Victor Stinnercb29f012015-03-27 13:31:18 +0100385}
386
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200387int
388_PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
389{
390 return _PyTime_AsTimeval_impl(t, tv, round, 1);
391}
392
393int
394_PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
395{
396 return _PyTime_AsTimeval_impl(t, tv, round, 0);
397}
398
Victor Stinnerc3378382015-03-28 05:07:51 +0100399#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
Victor Stinner34dc0f42015-03-27 18:19:03 +0100400int
401_PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
402{
Victor Stinner95e9cef2015-03-28 01:26:47 +0100403 _PyTime_t secs, nsec;
404
405 secs = t / SEC_TO_NS;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100406 nsec = t % SEC_TO_NS;
407 if (nsec < 0) {
408 nsec += SEC_TO_NS;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100409 secs -= 1;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100410 }
Victor Stinner95e9cef2015-03-28 01:26:47 +0100411 ts->tv_sec = (time_t)secs;
412 if ((_PyTime_t)ts->tv_sec != secs) {
Victor Stinner34dc0f42015-03-27 18:19:03 +0100413 _PyTime_overflow();
414 return -1;
415 }
416 ts->tv_nsec = nsec;
Victor Stinneredddf992015-03-30 02:54:57 +0200417
418 assert(0 <= ts->tv_nsec && ts->tv_nsec <= 999999999);
Victor Stinner34dc0f42015-03-27 18:19:03 +0100419 return 0;
420}
421#endif
422
Victor Stinnercb29f012015-03-27 13:31:18 +0100423static int
Victor Stinnera47b8812015-03-27 18:16:17 +0100424pygettimeofday_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
425{
426#ifdef MS_WINDOWS
427 FILETIME system_time;
428 ULARGE_INTEGER large;
429
430 assert(info == NULL || raise);
431
432 GetSystemTimeAsFileTime(&system_time);
433 large.u.LowPart = system_time.dwLowDateTime;
434 large.u.HighPart = system_time.dwHighDateTime;
435 /* 11,644,473,600,000,000,000: number of nanoseconds between
436 the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
437 days). */
438 *tp = large.QuadPart * 100 - 11644473600000000000;
439 if (info) {
440 DWORD timeAdjustment, timeIncrement;
441 BOOL isTimeAdjustmentDisabled, ok;
442
443 info->implementation = "GetSystemTimeAsFileTime()";
444 info->monotonic = 0;
445 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
446 &isTimeAdjustmentDisabled);
447 if (!ok) {
448 PyErr_SetFromWindowsErr(0);
449 return -1;
450 }
451 info->resolution = timeIncrement * 1e-7;
452 info->adjustable = 1;
453 }
454
455#else /* MS_WINDOWS */
456 int err;
457#ifdef HAVE_CLOCK_GETTIME
458 struct timespec ts;
459#else
460 struct timeval tv;
461#endif
462
463 assert(info == NULL || raise);
464
465#ifdef HAVE_CLOCK_GETTIME
466 err = clock_gettime(CLOCK_REALTIME, &ts);
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_FromTimespec(tp, &ts, raise) < 0)
Victor Stinnera47b8812015-03-27 18:16:17 +0100473 return -1;
474
475 if (info) {
476 struct timespec res;
477 info->implementation = "clock_gettime(CLOCK_REALTIME)";
478 info->monotonic = 0;
479 info->adjustable = 1;
480 if (clock_getres(CLOCK_REALTIME, &res) == 0)
481 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
482 else
483 info->resolution = 1e-9;
484 }
485#else /* HAVE_CLOCK_GETTIME */
486
487 /* test gettimeofday() */
488#ifdef GETTIMEOFDAY_NO_TZ
489 err = gettimeofday(&tv);
490#else
491 err = gettimeofday(&tv, (struct timezone *)NULL);
492#endif
493 if (err) {
494 if (raise)
495 PyErr_SetFromErrno(PyExc_OSError);
496 return -1;
497 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100498 if (_PyTime_FromTimeval(tp, &tv, raise) < 0)
Victor Stinnera47b8812015-03-27 18:16:17 +0100499 return -1;
500
501 if (info) {
502 info->implementation = "gettimeofday()";
503 info->resolution = 1e-6;
504 info->monotonic = 0;
505 info->adjustable = 1;
506 }
507#endif /* !HAVE_CLOCK_GETTIME */
508#endif /* !MS_WINDOWS */
509 return 0;
510}
511
Victor Stinner09e5cf22015-03-30 00:09:18 +0200512_PyTime_t
513_PyTime_GetSystemClock(void)
514{
515 _PyTime_t t;
516 if (pygettimeofday_new(&t, NULL, 0) < 0) {
517 /* should not happen, _PyTime_Init() checked the clock at startup */
518 assert(0);
519
520 /* use a fixed value instead of a random value from the stack */
521 t = 0;
522 }
523 return t;
524}
525
Victor Stinnera47b8812015-03-27 18:16:17 +0100526int
527_PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
528{
529 return pygettimeofday_new(t, info, 1);
530}
531
532
533static int
Victor Stinnercb29f012015-03-27 13:31:18 +0100534pymonotonic_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
535{
536#ifdef Py_DEBUG
537 static int last_set = 0;
538 static _PyTime_t last = 0;
539#endif
540#if defined(MS_WINDOWS)
Victor Stinnercb29f012015-03-27 13:31:18 +0100541 ULONGLONG result;
542
543 assert(info == NULL || raise);
544
Victor Stinnereb352292015-03-27 14:12:08 +0100545 result = GetTickCount64();
Victor Stinnercb29f012015-03-27 13:31:18 +0100546
547 *tp = result * MS_TO_NS;
548 if (*tp / MS_TO_NS != result) {
549 if (raise) {
550 _PyTime_overflow();
551 return -1;
552 }
553 /* Hello, time traveler! */
554 assert(0);
555 }
556
557 if (info) {
558 DWORD timeAdjustment, timeIncrement;
559 BOOL isTimeAdjustmentDisabled, ok;
Victor Stinnereb352292015-03-27 14:12:08 +0100560 info->implementation = "GetTickCount64()";
Victor Stinnercb29f012015-03-27 13:31:18 +0100561 info->monotonic = 1;
562 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
563 &isTimeAdjustmentDisabled);
564 if (!ok) {
565 PyErr_SetFromWindowsErr(0);
566 return -1;
567 }
568 info->resolution = timeIncrement * 1e-7;
569 info->adjustable = 0;
570 }
571
572#elif defined(__APPLE__)
573 static mach_timebase_info_data_t timebase;
574 uint64_t time;
575
576 if (timebase.denom == 0) {
577 /* According to the Technical Q&A QA1398, mach_timebase_info() cannot
578 fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
579 (void)mach_timebase_info(&timebase);
580 }
581
582 time = mach_absolute_time();
583
584 /* apply timebase factor */
585 time *= timebase.numer;
586 time /= timebase.denom;
587
588 *tp = time;
589
590 if (info) {
591 info->implementation = "mach_absolute_time()";
592 info->resolution = (double)timebase.numer / timebase.denom * 1e-9;
593 info->monotonic = 1;
594 info->adjustable = 0;
595 }
596
597#else
598 struct timespec ts;
599#ifdef CLOCK_HIGHRES
600 const clockid_t clk_id = CLOCK_HIGHRES;
601 const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
602#else
603 const clockid_t clk_id = CLOCK_MONOTONIC;
604 const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
605#endif
606
607 assert(info == NULL || raise);
608
609 if (clock_gettime(clk_id, &ts) != 0) {
610 if (raise) {
611 PyErr_SetFromErrno(PyExc_OSError);
612 return -1;
613 }
614 return -1;
615 }
616
617 if (info) {
618 struct timespec res;
619 info->monotonic = 1;
620 info->implementation = implementation;
621 info->adjustable = 0;
622 if (clock_getres(clk_id, &res) != 0) {
623 PyErr_SetFromErrno(PyExc_OSError);
624 return -1;
625 }
626 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
627 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100628 if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
Victor Stinnercb29f012015-03-27 13:31:18 +0100629 return -1;
630#endif
631#ifdef Py_DEBUG
632 /* monotonic clock cannot go backward */
633 assert(!last_set || last <= *tp);
634 last = *tp;
635 last_set = 1;
636#endif
637 return 0;
638}
639
640_PyTime_t
641_PyTime_GetMonotonicClock(void)
642{
643 _PyTime_t t;
644 if (pymonotonic_new(&t, NULL, 0) < 0) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100645 /* should not happen, _PyTime_Init() checked that monotonic clock at
646 startup */
Victor Stinnercb29f012015-03-27 13:31:18 +0100647 assert(0);
Victor Stinnercb0c6022015-03-28 05:24:19 +0100648
649 /* use a fixed value instead of a random value from the stack */
Victor Stinnercb29f012015-03-27 13:31:18 +0100650 t = 0;
651 }
652 return t;
653}
654
Victor Stinner00111242014-08-29 16:31:59 +0200655int
Victor Stinner4bfb4602015-03-27 22:27:24 +0100656_PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
657{
658 return pymonotonic_new(tp, info, 1);
659}
660
661int
Victor Stinner00111242014-08-29 16:31:59 +0200662_PyTime_Init(void)
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000663{
Victor Stinnercb29f012015-03-27 13:31:18 +0100664 _PyTime_t t;
665
Victor Stinner00111242014-08-29 16:31:59 +0200666 /* ensure that the system clock works */
Victor Stinnera47b8812015-03-27 18:16:17 +0100667 if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0)
Victor Stinner00111242014-08-29 16:31:59 +0200668 return -1;
Victor Stinnerae586492014-09-02 23:18:25 +0200669
670 /* ensure that the operating system provides a monotonic clock */
Victor Stinnera47b8812015-03-27 18:16:17 +0100671 if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0)
Victor Stinnercb29f012015-03-27 13:31:18 +0100672 return -1;
Victor Stinner13019fd2015-04-03 13:10:54 +0200673
674 /* check that _PyTime_FromSeconds() cannot overflow */
675 assert(INT_MAX <= _PyTime_MAX / SEC_TO_NS);
676 assert(INT_MIN >= _PyTime_MIN / SEC_TO_NS);
Victor Stinner00111242014-08-29 16:31:59 +0200677 return 0;
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000678}