blob: 5a5cdd9c7add4306a83ef79e3949847da85a6748 [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 Stinnerec26f832015-09-18 14:21:14 +0200308_PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
309 const _PyTime_round_t round)
Victor Stinnercb29f012015-03-27 13:31:18 +0100310{
Victor Stinner62d1c702015-04-01 17:47:07 +0200311 assert(k > 1);
312 if (round == _PyTime_ROUND_CEILING) {
313 if (t >= 0)
Victor Stinnercb29f012015-03-27 13:31:18 +0100314 return (t + k - 1) / k;
315 else
Victor Stinnerec26f832015-09-18 14:21:14 +0200316 return t / k;
317 }
318 else {
319 if (t >= 0)
320 return t / k;
321 else
Victor Stinner62d1c702015-04-01 17:47:07 +0200322 return (t - (k - 1)) / k;
Victor Stinnercb29f012015-03-27 13:31:18 +0100323 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100324}
325
326_PyTime_t
327_PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
328{
Victor Stinner62d1c702015-04-01 17:47:07 +0200329 return _PyTime_Divide(t, NS_TO_MS, round);
Victor Stinnercb29f012015-03-27 13:31:18 +0100330}
331
Victor Stinnerf5faad22015-03-28 03:52:05 +0100332_PyTime_t
333_PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
334{
Victor Stinner62d1c702015-04-01 17:47:07 +0200335 return _PyTime_Divide(t, NS_TO_US, round);
Victor Stinnerf5faad22015-03-28 03:52:05 +0100336}
337
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200338static int
Victor Stinner9a8b1772015-09-18 13:36:17 +0200339_PyTime_AsTimeval_impl(_PyTime_t t, _PyTime_t *p_secs, int *p_us,
340 _PyTime_round_t round)
Victor Stinnercb29f012015-03-27 13:31:18 +0100341{
342 _PyTime_t secs, ns;
Victor Stinner9a8b1772015-09-18 13:36:17 +0200343 int usec;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100344 int res = 0;
Victor Stinnercb29f012015-03-27 13:31:18 +0100345
346 secs = t / SEC_TO_NS;
347 ns = t % SEC_TO_NS;
Victor Stinner9a8b1772015-09-18 13:36:17 +0200348
349 usec = (int)_PyTime_Divide(ns, US_TO_NS, round);
350 if (usec < 0) {
351 usec += SEC_TO_US;
352 if (secs != _PyTime_MIN)
353 secs -= 1;
354 else
355 res = -1;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100356 }
Victor Stinner9a8b1772015-09-18 13:36:17 +0200357 else if (usec >= SEC_TO_US) {
358 usec -= SEC_TO_US;
359 if (secs != _PyTime_MAX)
360 secs += 1;
361 else
362 res = -1;
363 }
364 assert(0 <= usec && usec < SEC_TO_US);
365
366 *p_secs = secs;
367 *p_us = usec;
368
369 return res;
370}
371
372static int
373_PyTime_AsTimevalStruct_impl(_PyTime_t t, struct timeval *tv,
374 _PyTime_round_t round, int raise)
375{
376 _PyTime_t secs;
377 int us;
378 int res;
379
380 res = _PyTime_AsTimeval_impl(t, &secs, &us, round);
Victor Stinnercb29f012015-03-27 13:31:18 +0100381
382#ifdef MS_WINDOWS
Victor Stinnercb29f012015-03-27 13:31:18 +0100383 tv->tv_sec = (long)secs;
384#else
Victor Stinnercb29f012015-03-27 13:31:18 +0100385 tv->tv_sec = secs;
Victor Stinnercb29f012015-03-27 13:31:18 +0100386#endif
Victor Stinner9a8b1772015-09-18 13:36:17 +0200387 tv->tv_usec = us;
Victor Stinnercb29f012015-03-27 13:31:18 +0100388
Victor Stinner9a8b1772015-09-18 13:36:17 +0200389 if (res < 0 || (_PyTime_t)tv->tv_sec != secs) {
390 if (raise)
391 error_time_t_overflow();
392 return -1;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100393 }
Victor Stinner9a8b1772015-09-18 13:36:17 +0200394 return 0;
Victor Stinnercb29f012015-03-27 13:31:18 +0100395}
396
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200397int
398_PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
399{
Victor Stinner9a8b1772015-09-18 13:36:17 +0200400 return _PyTime_AsTimevalStruct_impl(t, tv, round, 1);
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200401}
402
403int
404_PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
405{
Victor Stinner9a8b1772015-09-18 13:36:17 +0200406 return _PyTime_AsTimevalStruct_impl(t, tv, round, 0);
407}
408
409int
410_PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us,
411 _PyTime_round_t round)
412{
413 _PyTime_t secs;
414 int res;
415
416 res = _PyTime_AsTimeval_impl(t, &secs, us, round);
417
418 *p_secs = secs;
419
420 if (res < 0 || (_PyTime_t)*p_secs != secs) {
421 error_time_t_overflow();
422 return -1;
423 }
424 return 0;
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200425}
426
Victor Stinnerc3378382015-03-28 05:07:51 +0100427#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
Victor Stinner34dc0f42015-03-27 18:19:03 +0100428int
429_PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
430{
Victor Stinner95e9cef2015-03-28 01:26:47 +0100431 _PyTime_t secs, nsec;
432
433 secs = t / SEC_TO_NS;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100434 nsec = t % SEC_TO_NS;
435 if (nsec < 0) {
436 nsec += SEC_TO_NS;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100437 secs -= 1;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100438 }
Victor Stinner95e9cef2015-03-28 01:26:47 +0100439 ts->tv_sec = (time_t)secs;
440 if ((_PyTime_t)ts->tv_sec != secs) {
Victor Stinner34dc0f42015-03-27 18:19:03 +0100441 _PyTime_overflow();
442 return -1;
443 }
444 ts->tv_nsec = nsec;
Victor Stinneredddf992015-03-30 02:54:57 +0200445
446 assert(0 <= ts->tv_nsec && ts->tv_nsec <= 999999999);
Victor Stinner34dc0f42015-03-27 18:19:03 +0100447 return 0;
448}
449#endif
450
Victor Stinnercb29f012015-03-27 13:31:18 +0100451static int
Victor Stinnera47b8812015-03-27 18:16:17 +0100452pygettimeofday_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
453{
454#ifdef MS_WINDOWS
455 FILETIME system_time;
456 ULARGE_INTEGER large;
457
458 assert(info == NULL || raise);
459
460 GetSystemTimeAsFileTime(&system_time);
461 large.u.LowPart = system_time.dwLowDateTime;
462 large.u.HighPart = system_time.dwHighDateTime;
463 /* 11,644,473,600,000,000,000: number of nanoseconds between
464 the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
465 days). */
466 *tp = large.QuadPart * 100 - 11644473600000000000;
467 if (info) {
468 DWORD timeAdjustment, timeIncrement;
469 BOOL isTimeAdjustmentDisabled, ok;
470
471 info->implementation = "GetSystemTimeAsFileTime()";
472 info->monotonic = 0;
473 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
474 &isTimeAdjustmentDisabled);
475 if (!ok) {
476 PyErr_SetFromWindowsErr(0);
477 return -1;
478 }
479 info->resolution = timeIncrement * 1e-7;
480 info->adjustable = 1;
481 }
482
483#else /* MS_WINDOWS */
484 int err;
485#ifdef HAVE_CLOCK_GETTIME
486 struct timespec ts;
487#else
488 struct timeval tv;
489#endif
490
491 assert(info == NULL || raise);
492
493#ifdef HAVE_CLOCK_GETTIME
494 err = clock_gettime(CLOCK_REALTIME, &ts);
495 if (err) {
496 if (raise)
497 PyErr_SetFromErrno(PyExc_OSError);
498 return -1;
499 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100500 if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
Victor Stinnera47b8812015-03-27 18:16:17 +0100501 return -1;
502
503 if (info) {
504 struct timespec res;
505 info->implementation = "clock_gettime(CLOCK_REALTIME)";
506 info->monotonic = 0;
507 info->adjustable = 1;
508 if (clock_getres(CLOCK_REALTIME, &res) == 0)
509 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
510 else
511 info->resolution = 1e-9;
512 }
513#else /* HAVE_CLOCK_GETTIME */
514
515 /* test gettimeofday() */
516#ifdef GETTIMEOFDAY_NO_TZ
517 err = gettimeofday(&tv);
518#else
519 err = gettimeofday(&tv, (struct timezone *)NULL);
520#endif
521 if (err) {
522 if (raise)
523 PyErr_SetFromErrno(PyExc_OSError);
524 return -1;
525 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100526 if (_PyTime_FromTimeval(tp, &tv, raise) < 0)
Victor Stinnera47b8812015-03-27 18:16:17 +0100527 return -1;
528
529 if (info) {
530 info->implementation = "gettimeofday()";
531 info->resolution = 1e-6;
532 info->monotonic = 0;
533 info->adjustable = 1;
534 }
535#endif /* !HAVE_CLOCK_GETTIME */
536#endif /* !MS_WINDOWS */
537 return 0;
538}
539
Victor Stinner09e5cf22015-03-30 00:09:18 +0200540_PyTime_t
541_PyTime_GetSystemClock(void)
542{
543 _PyTime_t t;
544 if (pygettimeofday_new(&t, NULL, 0) < 0) {
545 /* should not happen, _PyTime_Init() checked the clock at startup */
546 assert(0);
547
548 /* use a fixed value instead of a random value from the stack */
549 t = 0;
550 }
551 return t;
552}
553
Victor Stinnera47b8812015-03-27 18:16:17 +0100554int
555_PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
556{
557 return pygettimeofday_new(t, info, 1);
558}
559
560
561static int
Victor Stinner5ad58212015-09-03 00:14:58 +0200562pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100563{
Victor Stinnercb29f012015-03-27 13:31:18 +0100564#if defined(MS_WINDOWS)
Victor Stinnercb29f012015-03-27 13:31:18 +0100565 ULONGLONG result;
566
567 assert(info == NULL || raise);
568
Victor Stinnereb352292015-03-27 14:12:08 +0100569 result = GetTickCount64();
Victor Stinnercb29f012015-03-27 13:31:18 +0100570
571 *tp = result * MS_TO_NS;
572 if (*tp / MS_TO_NS != result) {
573 if (raise) {
574 _PyTime_overflow();
575 return -1;
576 }
577 /* Hello, time traveler! */
578 assert(0);
579 }
580
581 if (info) {
582 DWORD timeAdjustment, timeIncrement;
583 BOOL isTimeAdjustmentDisabled, ok;
Victor Stinnereb352292015-03-27 14:12:08 +0100584 info->implementation = "GetTickCount64()";
Victor Stinnercb29f012015-03-27 13:31:18 +0100585 info->monotonic = 1;
586 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
587 &isTimeAdjustmentDisabled);
588 if (!ok) {
589 PyErr_SetFromWindowsErr(0);
590 return -1;
591 }
592 info->resolution = timeIncrement * 1e-7;
593 info->adjustable = 0;
594 }
595
596#elif defined(__APPLE__)
597 static mach_timebase_info_data_t timebase;
598 uint64_t time;
599
600 if (timebase.denom == 0) {
601 /* According to the Technical Q&A QA1398, mach_timebase_info() cannot
602 fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
603 (void)mach_timebase_info(&timebase);
604 }
605
606 time = mach_absolute_time();
607
608 /* apply timebase factor */
609 time *= timebase.numer;
610 time /= timebase.denom;
611
612 *tp = time;
613
614 if (info) {
615 info->implementation = "mach_absolute_time()";
616 info->resolution = (double)timebase.numer / timebase.denom * 1e-9;
617 info->monotonic = 1;
618 info->adjustable = 0;
619 }
620
621#else
622 struct timespec ts;
623#ifdef CLOCK_HIGHRES
624 const clockid_t clk_id = CLOCK_HIGHRES;
625 const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
626#else
627 const clockid_t clk_id = CLOCK_MONOTONIC;
628 const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
629#endif
630
631 assert(info == NULL || raise);
632
633 if (clock_gettime(clk_id, &ts) != 0) {
634 if (raise) {
635 PyErr_SetFromErrno(PyExc_OSError);
636 return -1;
637 }
638 return -1;
639 }
640
641 if (info) {
642 struct timespec res;
643 info->monotonic = 1;
644 info->implementation = implementation;
645 info->adjustable = 0;
646 if (clock_getres(clk_id, &res) != 0) {
647 PyErr_SetFromErrno(PyExc_OSError);
648 return -1;
649 }
650 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
651 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100652 if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
Victor Stinnercb29f012015-03-27 13:31:18 +0100653 return -1;
654#endif
Victor Stinnercb29f012015-03-27 13:31:18 +0100655 return 0;
656}
657
658_PyTime_t
659_PyTime_GetMonotonicClock(void)
660{
661 _PyTime_t t;
Victor Stinner5ad58212015-09-03 00:14:58 +0200662 if (pymonotonic(&t, NULL, 0) < 0) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100663 /* should not happen, _PyTime_Init() checked that monotonic clock at
664 startup */
Victor Stinnercb29f012015-03-27 13:31:18 +0100665 assert(0);
Victor Stinnercb0c6022015-03-28 05:24:19 +0100666
667 /* use a fixed value instead of a random value from the stack */
Victor Stinnercb29f012015-03-27 13:31:18 +0100668 t = 0;
669 }
670 return t;
671}
672
Victor Stinner00111242014-08-29 16:31:59 +0200673int
Victor Stinner4bfb4602015-03-27 22:27:24 +0100674_PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
675{
Victor Stinner5ad58212015-09-03 00:14:58 +0200676 return pymonotonic(tp, info, 1);
Victor Stinner4bfb4602015-03-27 22:27:24 +0100677}
678
679int
Victor Stinner00111242014-08-29 16:31:59 +0200680_PyTime_Init(void)
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000681{
Victor Stinnercb29f012015-03-27 13:31:18 +0100682 _PyTime_t t;
683
Victor Stinner00111242014-08-29 16:31:59 +0200684 /* ensure that the system clock works */
Victor Stinnera47b8812015-03-27 18:16:17 +0100685 if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0)
Victor Stinner00111242014-08-29 16:31:59 +0200686 return -1;
Victor Stinnerae586492014-09-02 23:18:25 +0200687
688 /* ensure that the operating system provides a monotonic clock */
Victor Stinnera47b8812015-03-27 18:16:17 +0100689 if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0)
Victor Stinnercb29f012015-03-27 13:31:18 +0100690 return -1;
Victor Stinner13019fd2015-04-03 13:10:54 +0200691
692 /* check that _PyTime_FromSeconds() cannot overflow */
693 assert(INT_MAX <= _PyTime_MAX / SEC_TO_NS);
694 assert(INT_MIN >= _PyTime_MIN / SEC_TO_NS);
Victor Stinner00111242014-08-29 16:31:59 +0200695 return 0;
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000696}