blob: 81682caa968a3fec970b5081a8a2534919ef340e [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
Larry Hastings76ad59b2012-05-03 00:30:07 -070038time_t
Victor Stinner5d272cc2012-03-13 13:35:55 +010039_PyLong_AsTime_t(PyObject *obj)
40{
41#if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
42 PY_LONG_LONG val;
43 val = PyLong_AsLongLong(obj);
44#else
45 long val;
Serhiy Storchakafad85aa2015-11-07 15:42:38 +020046 Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
Victor Stinner5d272cc2012-03-13 13:35:55 +010047 val = PyLong_AsLong(obj);
48#endif
49 if (val == -1 && PyErr_Occurred()) {
50 if (PyErr_ExceptionMatches(PyExc_OverflowError))
51 error_time_t_overflow();
52 return -1;
53 }
54 return (time_t)val;
55}
56
Larry Hastings6fe20b32012-04-19 15:07:49 -070057PyObject *
58_PyLong_FromTime_t(time_t t)
59{
60#if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
61 return PyLong_FromLongLong((PY_LONG_LONG)t);
62#else
Serhiy Storchakafad85aa2015-11-07 15:42:38 +020063 Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
Larry Hastings6fe20b32012-04-19 15:07:49 -070064 return PyLong_FromLong((long)t);
65#endif
66}
67
Victor Stinnerce6aa742015-09-09 22:28:09 +020068/* Round to nearest with ties going to nearest even integer
69 (_PyTime_ROUND_HALF_EVEN) */
70static double
Victor Stinner7667f582015-09-09 01:02:23 +020071_PyTime_RoundHalfEven(double x)
Victor Stinner74474232015-09-02 01:43:56 +020072{
Victor Stinner7667f582015-09-09 01:02:23 +020073 double rounded = round(x);
74 if (fabs(x-rounded) == 0.5)
75 /* halfway case: round to even */
76 rounded = 2.0*round(x/2.0);
77 return rounded;
Victor Stinner74474232015-09-02 01:43:56 +020078}
79
Victor Stinner9ae47df2015-09-09 22:28:58 +020080static double
81_PyTime_Round(double x, _PyTime_round_t round)
82{
Victor Stinner1efbeba2015-09-10 11:48:00 +020083 /* volatile avoids optimization changing how numbers are rounded */
84 volatile double d;
85
86 d = x;
Victor Stinner9ae47df2015-09-09 22:28:58 +020087 if (round == _PyTime_ROUND_HALF_EVEN)
Victor Stinner1efbeba2015-09-10 11:48:00 +020088 d = _PyTime_RoundHalfEven(d);
Victor Stinner9ae47df2015-09-09 22:28:58 +020089 else if (round == _PyTime_ROUND_CEILING)
Victor Stinner1efbeba2015-09-10 11:48:00 +020090 d = ceil(d);
Victor Stinner9ae47df2015-09-09 22:28:58 +020091 else
Victor Stinner1efbeba2015-09-10 11:48:00 +020092 d = floor(d);
93 return d;
Victor Stinner9ae47df2015-09-09 22:28:58 +020094}
95
Victor Stinner5d272cc2012-03-13 13:35:55 +010096static int
Victor Stinner53e137c2015-09-02 00:49:16 +020097_PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
98 double denominator, _PyTime_round_t round)
99{
100 double intpart, err;
Victor Stinner24b822e2015-09-02 11:58:56 +0200101 /* volatile avoids optimization changing how numbers are rounded */
Victor Stinner53e137c2015-09-02 00:49:16 +0200102 volatile double floatpart;
103
104 floatpart = modf(d, &intpart);
Victor Stinner53e137c2015-09-02 00:49:16 +0200105
106 floatpart *= denominator;
Victor Stinner9ae47df2015-09-09 22:28:58 +0200107 floatpart = _PyTime_Round(floatpart, round);
Victor Stinner67edcc92015-09-02 10:37:46 +0200108 if (floatpart >= denominator) {
109 floatpart -= denominator;
110 intpart += 1.0;
Victor Stinner53e137c2015-09-02 00:49:16 +0200111 }
Victor Stinneradfefa52015-09-04 23:57:25 +0200112 else if (floatpart < 0) {
113 floatpart += denominator;
114 intpart -= 1.0;
115 }
Victor Stinner67edcc92015-09-02 10:37:46 +0200116 assert(0.0 <= floatpart && floatpart < denominator);
Victor Stinner53e137c2015-09-02 00:49:16 +0200117
118 *sec = (time_t)intpart;
Victor Stinner67edcc92015-09-02 10:37:46 +0200119 *numerator = (long)floatpart;
120
Victor Stinner53e137c2015-09-02 00:49:16 +0200121 err = intpart - (double)*sec;
122 if (err <= -1.0 || err >= 1.0) {
123 error_time_t_overflow();
124 return -1;
125 }
Victor Stinner53e137c2015-09-02 00:49:16 +0200126 return 0;
127}
128
129static int
Victor Stinner5d272cc2012-03-13 13:35:55 +0100130_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
Victor Stinner3c1b3792014-02-17 00:02:43 +0100131 double denominator, _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100132{
Victor Stinnerbbdda212015-09-02 00:50:43 +0200133 assert(denominator <= (double)LONG_MAX);
134
Victor Stinner643cd682012-03-02 22:54:03 +0100135 if (PyFloat_Check(obj)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200136 double d = PyFloat_AsDouble(obj);
137 return _PyTime_DoubleToDenominator(d, sec, numerator,
138 denominator, round);
Victor Stinner643cd682012-03-02 22:54:03 +0100139 }
140 else {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100141 *sec = _PyLong_AsTime_t(obj);
Victor Stinner67edcc92015-09-02 10:37:46 +0200142 *numerator = 0;
Victor Stinner5d272cc2012-03-13 13:35:55 +0100143 if (*sec == (time_t)-1 && PyErr_Occurred())
144 return -1;
Victor Stinner643cd682012-03-02 22:54:03 +0100145 return 0;
146 }
Victor Stinner5d272cc2012-03-13 13:35:55 +0100147}
Victor Stinner643cd682012-03-02 22:54:03 +0100148
Victor Stinner5d272cc2012-03-13 13:35:55 +0100149int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100150_PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100151{
152 if (PyFloat_Check(obj)) {
Victor Stinner5786aef2015-09-03 16:33:16 +0200153 double intpart, err;
Victor Stinner24b822e2015-09-02 11:58:56 +0200154 /* volatile avoids optimization changing how numbers are rounded */
Victor Stinner5786aef2015-09-03 16:33:16 +0200155 volatile double d;
Victor Stinner5d272cc2012-03-13 13:35:55 +0100156
Victor Stinner5d272cc2012-03-13 13:35:55 +0100157 d = PyFloat_AsDouble(obj);
Victor Stinner9ae47df2015-09-09 22:28:58 +0200158 d = _PyTime_Round(d, round);
Victor Stinner5d272cc2012-03-13 13:35:55 +0100159 (void)modf(d, &intpart);
160
161 *sec = (time_t)intpart;
162 err = intpart - (double)*sec;
163 if (err <= -1.0 || err >= 1.0) {
164 error_time_t_overflow();
165 return -1;
166 }
167 return 0;
168 }
169 else {
170 *sec = _PyLong_AsTime_t(obj);
171 if (*sec == (time_t)-1 && PyErr_Occurred())
172 return -1;
173 return 0;
174 }
175}
176
177int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100178_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
179 _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100180{
Victor Stinnerbbdda212015-09-02 00:50:43 +0200181 int res;
182 res = _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
Victor Stinner67edcc92015-09-02 10:37:46 +0200183 assert(0 <= *nsec && *nsec < SEC_TO_NS);
Victor Stinnerbbdda212015-09-02 00:50:43 +0200184 return res;
Victor Stinner5d272cc2012-03-13 13:35:55 +0100185}
186
187int
Victor Stinner3c1b3792014-02-17 00:02:43 +0100188_PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
189 _PyTime_round_t round)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100190{
Victor Stinnerbbdda212015-09-02 00:50:43 +0200191 int res;
192 res = _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
Victor Stinner67edcc92015-09-02 10:37:46 +0200193 assert(0 <= *usec && *usec < SEC_TO_US);
Victor Stinnerbbdda212015-09-02 00:50:43 +0200194 return res;
Victor Stinner643cd682012-03-02 22:54:03 +0100195}
196
Victor Stinnercb29f012015-03-27 13:31:18 +0100197static void
198_PyTime_overflow(void)
199{
200 PyErr_SetString(PyExc_OverflowError,
201 "timestamp too large to convert to C _PyTime_t");
202}
203
Victor Stinner4bfb4602015-03-27 22:27:24 +0100204_PyTime_t
Victor Stinner13019fd2015-04-03 13:10:54 +0200205_PyTime_FromSeconds(int seconds)
206{
207 _PyTime_t t;
Victor Stinnerbbdda212015-09-02 00:50:43 +0200208 t = (_PyTime_t)seconds;
Victor Stinner13019fd2015-04-03 13:10:54 +0200209 /* ensure that integer overflow cannot happen, int type should have 32
210 bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
211 bits). */
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200212 Py_BUILD_ASSERT(INT_MAX <= _PyTime_MAX / SEC_TO_NS);
213 Py_BUILD_ASSERT(INT_MIN >= _PyTime_MIN / SEC_TO_NS);
Victor Stinnerbbdda212015-09-02 00:50:43 +0200214 assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
215 || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
216 t *= SEC_TO_NS;
Victor Stinner13019fd2015-04-03 13:10:54 +0200217 return t;
218}
219
220_PyTime_t
Victor Stinner4bfb4602015-03-27 22:27:24 +0100221_PyTime_FromNanoseconds(PY_LONG_LONG ns)
222{
223 _PyTime_t t;
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200224 Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
Victor Stinner4bfb4602015-03-27 22:27:24 +0100225 t = Py_SAFE_DOWNCAST(ns, PY_LONG_LONG, _PyTime_t);
226 return t;
227}
228
Victor Stinnera47b8812015-03-27 18:16:17 +0100229#ifdef HAVE_CLOCK_GETTIME
Victor Stinnercb29f012015-03-27 13:31:18 +0100230static int
Victor Stinnercb0c6022015-03-28 05:24:19 +0100231_PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100232{
233 _PyTime_t t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100234 int res = 0;
235
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200236 Py_BUILD_ASSERT(sizeof(ts->tv_sec) <= sizeof(_PyTime_t));
Victor Stinnerc60542b2015-09-10 15:55:07 +0200237 t = (_PyTime_t)ts->tv_sec;
238
239 if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100240 if (raise)
241 _PyTime_overflow();
242 res = -1;
Victor Stinnercb29f012015-03-27 13:31:18 +0100243 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200244 t = t * SEC_TO_NS;
Victor Stinnercb29f012015-03-27 13:31:18 +0100245
246 t += ts->tv_nsec;
247
248 *tp = t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100249 return res;
Victor Stinnercb29f012015-03-27 13:31:18 +0100250}
Victor Stinner1bd18ba2015-03-30 00:25:38 +0200251#elif !defined(MS_WINDOWS)
Victor Stinnera47b8812015-03-27 18:16:17 +0100252static int
Victor Stinnercb0c6022015-03-28 05:24:19 +0100253_PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
Victor Stinnera47b8812015-03-27 18:16:17 +0100254{
255 _PyTime_t t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100256 int res = 0;
Victor Stinnera47b8812015-03-27 18:16:17 +0100257
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200258 Py_BUILD_ASSERT(sizeof(tv->tv_sec) <= sizeof(_PyTime_t));
Victor Stinnerc60542b2015-09-10 15:55:07 +0200259 t = (_PyTime_t)tv->tv_sec;
260
261 if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100262 if (raise)
263 _PyTime_overflow();
264 res = -1;
Victor Stinnera47b8812015-03-27 18:16:17 +0100265 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200266 t = t * SEC_TO_NS;
Victor Stinnera47b8812015-03-27 18:16:17 +0100267
268 t += (_PyTime_t)tv->tv_usec * US_TO_NS;
269
270 *tp = t;
Victor Stinnercb0c6022015-03-28 05:24:19 +0100271 return res;
Victor Stinnera47b8812015-03-27 18:16:17 +0100272}
Victor Stinnercb29f012015-03-27 13:31:18 +0100273#endif
274
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200275static int
Victor Stinner53e137c2015-09-02 00:49:16 +0200276_PyTime_FromFloatObject(_PyTime_t *t, double value, _PyTime_round_t round,
Victor Stinner9ae47df2015-09-09 22:28:58 +0200277 long unit_to_ns)
Victor Stinner53e137c2015-09-02 00:49:16 +0200278{
Victor Stinner5786aef2015-09-03 16:33:16 +0200279 double err;
Victor Stinner24b822e2015-09-02 11:58:56 +0200280 /* volatile avoids optimization changing how numbers are rounded */
Victor Stinner5786aef2015-09-03 16:33:16 +0200281 volatile double d;
Victor Stinner53e137c2015-09-02 00:49:16 +0200282
283 /* convert to a number of nanoseconds */
284 d = value;
Victor Stinner9ae47df2015-09-09 22:28:58 +0200285 d *= (double)unit_to_ns;
286 d = _PyTime_Round(d, round);
Victor Stinner53e137c2015-09-02 00:49:16 +0200287
288 *t = (_PyTime_t)d;
289 err = d - (double)*t;
290 if (fabs(err) >= 1.0) {
291 _PyTime_overflow();
292 return -1;
293 }
294 return 0;
295}
296
297static int
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200298_PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
Victor Stinner9ae47df2015-09-09 22:28:58 +0200299 long unit_to_ns)
Victor Stinnercb29f012015-03-27 13:31:18 +0100300{
301 if (PyFloat_Check(obj)) {
Victor Stinner53e137c2015-09-02 00:49:16 +0200302 double d;
Victor Stinnercb29f012015-03-27 13:31:18 +0100303 d = PyFloat_AsDouble(obj);
Victor Stinner9ae47df2015-09-09 22:28:58 +0200304 return _PyTime_FromFloatObject(t, d, round, unit_to_ns);
Victor Stinnercb29f012015-03-27 13:31:18 +0100305 }
306 else {
307#ifdef HAVE_LONG_LONG
308 PY_LONG_LONG sec;
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200309 Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200310
311 sec = PyLong_AsLongLong(obj);
Victor Stinnercb29f012015-03-27 13:31:18 +0100312#else
313 long sec;
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200314 Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200315
316 sec = PyLong_AsLong(obj);
Victor Stinnercb29f012015-03-27 13:31:18 +0100317#endif
318 if (sec == -1 && PyErr_Occurred()) {
319 if (PyErr_ExceptionMatches(PyExc_OverflowError))
320 _PyTime_overflow();
321 return -1;
322 }
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200323
Victor Stinnerc60542b2015-09-10 15:55:07 +0200324 if (_PyTime_check_mul_overflow(sec, unit_to_ns)) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100325 _PyTime_overflow();
326 return -1;
327 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200328 *t = sec * unit_to_ns;
Victor Stinnercb29f012015-03-27 13:31:18 +0100329 return 0;
330 }
331}
332
Victor Stinnerfa09beb2015-03-30 21:36:10 +0200333int
334_PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
335{
336 return _PyTime_FromObject(t, obj, round, SEC_TO_NS);
337}
338
339int
340_PyTime_FromMillisecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
341{
342 return _PyTime_FromObject(t, obj, round, MS_TO_NS);
343}
344
Victor Stinner4bfb4602015-03-27 22:27:24 +0100345double
346_PyTime_AsSecondsDouble(_PyTime_t t)
347{
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200348 /* volatile avoids optimization changing how numbers are rounded */
349 volatile double d;
350
Victor Stinner3e2c8d82015-09-09 22:32:48 +0200351 if (t % SEC_TO_NS == 0) {
352 _PyTime_t secs;
353 /* Divide using integers to avoid rounding issues on the integer part.
354 1e-9 cannot be stored exactly in IEEE 64-bit. */
355 secs = t / SEC_TO_NS;
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200356 d = (double)secs;
Victor Stinner3e2c8d82015-09-09 22:32:48 +0200357 }
358 else {
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200359 d = (double)t;
360 d /= 1e9;
Victor Stinner3e2c8d82015-09-09 22:32:48 +0200361 }
Victor Stinnerff0ed3e2015-09-10 13:25:17 +0200362 return d;
Victor Stinner4bfb4602015-03-27 22:27:24 +0100363}
364
Victor Stinner992c43f2015-03-27 17:12:45 +0100365PyObject *
366_PyTime_AsNanosecondsObject(_PyTime_t t)
367{
368#ifdef HAVE_LONG_LONG
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200369 Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) >= sizeof(_PyTime_t));
Victor Stinner992c43f2015-03-27 17:12:45 +0100370 return PyLong_FromLongLong((PY_LONG_LONG)t);
371#else
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200372 Py_BUILD_ASSERT(sizeof(long) >= sizeof(_PyTime_t));
Victor Stinner992c43f2015-03-27 17:12:45 +0100373 return PyLong_FromLong((long)t);
374#endif
375}
376
Victor Stinnercb29f012015-03-27 13:31:18 +0100377static _PyTime_t
Victor Stinner7667f582015-09-09 01:02:23 +0200378_PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
379 const _PyTime_round_t round)
Victor Stinnercb29f012015-03-27 13:31:18 +0100380{
Victor Stinner62d1c702015-04-01 17:47:07 +0200381 assert(k > 1);
Victor Stinner7667f582015-09-09 01:02:23 +0200382 if (round == _PyTime_ROUND_HALF_EVEN) {
383 _PyTime_t x, r, abs_r;
Victor Stinner74474232015-09-02 01:43:56 +0200384 x = t / k;
385 r = t % k;
Victor Stinner7667f582015-09-09 01:02:23 +0200386 abs_r = Py_ABS(r);
387 if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) {
Victor Stinner74474232015-09-02 01:43:56 +0200388 if (t >= 0)
389 x++;
390 else
391 x--;
392 }
393 return x;
394 }
395 else if (round == _PyTime_ROUND_CEILING) {
Victor Stinner62d1c702015-04-01 17:47:07 +0200396 if (t >= 0)
Victor Stinnercb29f012015-03-27 13:31:18 +0100397 return (t + k - 1) / k;
398 else
Victor Stinner7667f582015-09-09 01:02:23 +0200399 return t / k;
400 }
401 else {
402 if (t >= 0)
403 return t / k;
404 else
Victor Stinner62d1c702015-04-01 17:47:07 +0200405 return (t - (k - 1)) / k;
Victor Stinnercb29f012015-03-27 13:31:18 +0100406 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100407}
408
409_PyTime_t
410_PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
411{
Victor Stinner62d1c702015-04-01 17:47:07 +0200412 return _PyTime_Divide(t, NS_TO_MS, round);
Victor Stinnercb29f012015-03-27 13:31:18 +0100413}
414
Victor Stinnerf5faad22015-03-28 03:52:05 +0100415_PyTime_t
416_PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
417{
Victor Stinner62d1c702015-04-01 17:47:07 +0200418 return _PyTime_Divide(t, NS_TO_US, round);
Victor Stinnerf5faad22015-03-28 03:52:05 +0100419}
420
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200421static int
Victor Stinner1e2b6882015-09-18 13:23:02 +0200422_PyTime_AsTimeval_impl(_PyTime_t t, _PyTime_t *p_secs, int *p_us,
423 _PyTime_round_t round)
Victor Stinnercb29f012015-03-27 13:31:18 +0100424{
425 _PyTime_t secs, ns;
Victor Stinner74474232015-09-02 01:43:56 +0200426 int usec;
Victor Stinner1e2b6882015-09-18 13:23:02 +0200427 int res = 0;
Victor Stinnercb29f012015-03-27 13:31:18 +0100428
429 secs = t / SEC_TO_NS;
430 ns = t % SEC_TO_NS;
431
Victor Stinner1e2b6882015-09-18 13:23:02 +0200432 usec = (int)_PyTime_Divide(ns, US_TO_NS, round);
433 if (usec < 0) {
434 usec += SEC_TO_US;
435 if (secs != _PyTime_MIN)
436 secs -= 1;
437 else
438 res = -1;
439 }
440 else if (usec >= SEC_TO_US) {
441 usec -= SEC_TO_US;
442 if (secs != _PyTime_MAX)
443 secs += 1;
444 else
445 res = -1;
446 }
447 assert(0 <= usec && usec < SEC_TO_US);
448
449 *p_secs = secs;
450 *p_us = usec;
451
452 return res;
453}
454
455static int
456_PyTime_AsTimevalStruct_impl(_PyTime_t t, struct timeval *tv,
457 _PyTime_round_t round, int raise)
458{
Victor Stinnerb7a8af22015-10-01 08:44:03 +0200459 _PyTime_t secs, secs2;
Victor Stinner1e2b6882015-09-18 13:23:02 +0200460 int us;
461 int res;
462
463 res = _PyTime_AsTimeval_impl(t, &secs, &us, round);
464
Victor Stinnercb29f012015-03-27 13:31:18 +0100465#ifdef MS_WINDOWS
Victor Stinnercb29f012015-03-27 13:31:18 +0100466 tv->tv_sec = (long)secs;
467#else
Victor Stinnercb29f012015-03-27 13:31:18 +0100468 tv->tv_sec = secs;
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200469#endif
Victor Stinner1e2b6882015-09-18 13:23:02 +0200470 tv->tv_usec = us;
Victor Stinnercb29f012015-03-27 13:31:18 +0100471
Victor Stinnerb7a8af22015-10-01 08:44:03 +0200472 secs2 = (_PyTime_t)tv->tv_sec;
473 if (res < 0 || secs2 != secs) {
Victor Stinner1e2b6882015-09-18 13:23:02 +0200474 if (raise)
475 error_time_t_overflow();
476 return -1;
Victor Stinner74474232015-09-02 01:43:56 +0200477 }
Victor Stinner1e2b6882015-09-18 13:23:02 +0200478 return 0;
Victor Stinnercb29f012015-03-27 13:31:18 +0100479}
480
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200481int
482_PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
483{
Victor Stinner1e2b6882015-09-18 13:23:02 +0200484 return _PyTime_AsTimevalStruct_impl(t, tv, round, 1);
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200485}
486
487int
488_PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
489{
Victor Stinner1e2b6882015-09-18 13:23:02 +0200490 return _PyTime_AsTimevalStruct_impl(t, tv, round, 0);
Victor Stinnerea9c0dd2015-03-30 02:51:13 +0200491}
492
Victor Stinner1e2b6882015-09-18 13:23:02 +0200493int
494_PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us,
495 _PyTime_round_t round)
496{
497 _PyTime_t secs;
498 int res;
499
500 res = _PyTime_AsTimeval_impl(t, &secs, us, round);
501
502 *p_secs = secs;
503
504 if (res < 0 || (_PyTime_t)*p_secs != secs) {
505 error_time_t_overflow();
506 return -1;
507 }
508 return 0;
509}
510
511
Victor Stinnerc3378382015-03-28 05:07:51 +0100512#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
Victor Stinner34dc0f42015-03-27 18:19:03 +0100513int
514_PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
515{
Victor Stinner95e9cef2015-03-28 01:26:47 +0100516 _PyTime_t secs, nsec;
517
518 secs = t / SEC_TO_NS;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100519 nsec = t % SEC_TO_NS;
520 if (nsec < 0) {
521 nsec += SEC_TO_NS;
Victor Stinner95e9cef2015-03-28 01:26:47 +0100522 secs -= 1;
Victor Stinner34dc0f42015-03-27 18:19:03 +0100523 }
Victor Stinner95e9cef2015-03-28 01:26:47 +0100524 ts->tv_sec = (time_t)secs;
Victor Stinner29ee6742015-09-03 16:25:45 +0200525 assert(0 <= nsec && nsec < SEC_TO_NS);
526 ts->tv_nsec = nsec;
527
Victor Stinner95e9cef2015-03-28 01:26:47 +0100528 if ((_PyTime_t)ts->tv_sec != secs) {
Victor Stinner9c72f9b2015-09-10 09:10:14 +0200529 error_time_t_overflow();
Victor Stinner34dc0f42015-03-27 18:19:03 +0100530 return -1;
531 }
Victor Stinner34dc0f42015-03-27 18:19:03 +0100532 return 0;
533}
534#endif
535
Victor Stinnercb29f012015-03-27 13:31:18 +0100536static int
Victor Stinnerc379ade2015-11-10 12:11:39 +0100537pygettimeofday(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
Victor Stinnera47b8812015-03-27 18:16:17 +0100538{
539#ifdef MS_WINDOWS
540 FILETIME system_time;
541 ULARGE_INTEGER large;
542
543 assert(info == NULL || raise);
544
545 GetSystemTimeAsFileTime(&system_time);
546 large.u.LowPart = system_time.dwLowDateTime;
547 large.u.HighPart = system_time.dwHighDateTime;
548 /* 11,644,473,600,000,000,000: number of nanoseconds between
549 the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
550 days). */
551 *tp = large.QuadPart * 100 - 11644473600000000000;
552 if (info) {
553 DWORD timeAdjustment, timeIncrement;
554 BOOL isTimeAdjustmentDisabled, ok;
555
556 info->implementation = "GetSystemTimeAsFileTime()";
557 info->monotonic = 0;
558 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
559 &isTimeAdjustmentDisabled);
560 if (!ok) {
561 PyErr_SetFromWindowsErr(0);
562 return -1;
563 }
564 info->resolution = timeIncrement * 1e-7;
565 info->adjustable = 1;
566 }
567
568#else /* MS_WINDOWS */
569 int err;
570#ifdef HAVE_CLOCK_GETTIME
571 struct timespec ts;
572#else
573 struct timeval tv;
574#endif
575
576 assert(info == NULL || raise);
577
578#ifdef HAVE_CLOCK_GETTIME
579 err = clock_gettime(CLOCK_REALTIME, &ts);
580 if (err) {
581 if (raise)
582 PyErr_SetFromErrno(PyExc_OSError);
583 return -1;
584 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100585 if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
Victor Stinnera47b8812015-03-27 18:16:17 +0100586 return -1;
587
588 if (info) {
589 struct timespec res;
590 info->implementation = "clock_gettime(CLOCK_REALTIME)";
591 info->monotonic = 0;
592 info->adjustable = 1;
593 if (clock_getres(CLOCK_REALTIME, &res) == 0)
594 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
595 else
596 info->resolution = 1e-9;
597 }
598#else /* HAVE_CLOCK_GETTIME */
599
600 /* test gettimeofday() */
601#ifdef GETTIMEOFDAY_NO_TZ
602 err = gettimeofday(&tv);
603#else
604 err = gettimeofday(&tv, (struct timezone *)NULL);
605#endif
606 if (err) {
607 if (raise)
608 PyErr_SetFromErrno(PyExc_OSError);
609 return -1;
610 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100611 if (_PyTime_FromTimeval(tp, &tv, raise) < 0)
Victor Stinnera47b8812015-03-27 18:16:17 +0100612 return -1;
613
614 if (info) {
615 info->implementation = "gettimeofday()";
616 info->resolution = 1e-6;
617 info->monotonic = 0;
618 info->adjustable = 1;
619 }
620#endif /* !HAVE_CLOCK_GETTIME */
621#endif /* !MS_WINDOWS */
622 return 0;
623}
624
Victor Stinner09e5cf22015-03-30 00:09:18 +0200625_PyTime_t
626_PyTime_GetSystemClock(void)
627{
628 _PyTime_t t;
Victor Stinnerc379ade2015-11-10 12:11:39 +0100629 if (pygettimeofday(&t, NULL, 0) < 0) {
Victor Stinner09e5cf22015-03-30 00:09:18 +0200630 /* should not happen, _PyTime_Init() checked the clock at startup */
631 assert(0);
632
633 /* use a fixed value instead of a random value from the stack */
634 t = 0;
635 }
636 return t;
637}
638
Victor Stinnera47b8812015-03-27 18:16:17 +0100639int
640_PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
641{
Victor Stinnerc379ade2015-11-10 12:11:39 +0100642 return pygettimeofday(t, info, 1);
Victor Stinnera47b8812015-03-27 18:16:17 +0100643}
644
Victor Stinnera47b8812015-03-27 18:16:17 +0100645static int
Victor Stinner5ad58212015-09-03 00:14:58 +0200646pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
Victor Stinnercb29f012015-03-27 13:31:18 +0100647{
Victor Stinnercb29f012015-03-27 13:31:18 +0100648#if defined(MS_WINDOWS)
Victor Stinnerc60542b2015-09-10 15:55:07 +0200649 ULONGLONG ticks;
650 _PyTime_t t;
Victor Stinnercb29f012015-03-27 13:31:18 +0100651
652 assert(info == NULL || raise);
653
Victor Stinnerc60542b2015-09-10 15:55:07 +0200654 ticks = GetTickCount64();
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200655 Py_BUILD_ASSERT(sizeof(ticks) <= sizeof(_PyTime_t));
Victor Stinnerc60542b2015-09-10 15:55:07 +0200656 t = (_PyTime_t)ticks;
Victor Stinnercb29f012015-03-27 13:31:18 +0100657
Victor Stinnerc60542b2015-09-10 15:55:07 +0200658 if (_PyTime_check_mul_overflow(t, MS_TO_NS)) {
Victor Stinnercb29f012015-03-27 13:31:18 +0100659 if (raise) {
660 _PyTime_overflow();
661 return -1;
662 }
663 /* Hello, time traveler! */
664 assert(0);
665 }
Victor Stinnerc60542b2015-09-10 15:55:07 +0200666 *tp = t * MS_TO_NS;
Victor Stinnercb29f012015-03-27 13:31:18 +0100667
668 if (info) {
669 DWORD timeAdjustment, timeIncrement;
670 BOOL isTimeAdjustmentDisabled, ok;
Victor Stinnereb352292015-03-27 14:12:08 +0100671 info->implementation = "GetTickCount64()";
Victor Stinnercb29f012015-03-27 13:31:18 +0100672 info->monotonic = 1;
673 ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
674 &isTimeAdjustmentDisabled);
675 if (!ok) {
676 PyErr_SetFromWindowsErr(0);
677 return -1;
678 }
679 info->resolution = timeIncrement * 1e-7;
680 info->adjustable = 0;
681 }
682
683#elif defined(__APPLE__)
684 static mach_timebase_info_data_t timebase;
685 uint64_t time;
686
687 if (timebase.denom == 0) {
688 /* According to the Technical Q&A QA1398, mach_timebase_info() cannot
689 fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
690 (void)mach_timebase_info(&timebase);
691 }
692
693 time = mach_absolute_time();
694
695 /* apply timebase factor */
696 time *= timebase.numer;
697 time /= timebase.denom;
698
699 *tp = time;
700
701 if (info) {
702 info->implementation = "mach_absolute_time()";
703 info->resolution = (double)timebase.numer / timebase.denom * 1e-9;
704 info->monotonic = 1;
705 info->adjustable = 0;
706 }
707
708#else
709 struct timespec ts;
710#ifdef CLOCK_HIGHRES
711 const clockid_t clk_id = CLOCK_HIGHRES;
712 const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
713#else
714 const clockid_t clk_id = CLOCK_MONOTONIC;
715 const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
716#endif
717
718 assert(info == NULL || raise);
719
720 if (clock_gettime(clk_id, &ts) != 0) {
721 if (raise) {
722 PyErr_SetFromErrno(PyExc_OSError);
723 return -1;
724 }
725 return -1;
726 }
727
728 if (info) {
729 struct timespec res;
730 info->monotonic = 1;
731 info->implementation = implementation;
732 info->adjustable = 0;
733 if (clock_getres(clk_id, &res) != 0) {
734 PyErr_SetFromErrno(PyExc_OSError);
735 return -1;
736 }
737 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
738 }
Victor Stinnercb0c6022015-03-28 05:24:19 +0100739 if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
Victor Stinnercb29f012015-03-27 13:31:18 +0100740 return -1;
741#endif
Victor Stinnercb29f012015-03-27 13:31:18 +0100742 return 0;
743}
744
745_PyTime_t
746_PyTime_GetMonotonicClock(void)
747{
748 _PyTime_t t;
Victor Stinner5ad58212015-09-03 00:14:58 +0200749 if (pymonotonic(&t, NULL, 0) < 0) {
Victor Stinnercb0c6022015-03-28 05:24:19 +0100750 /* should not happen, _PyTime_Init() checked that monotonic clock at
751 startup */
Victor Stinnercb29f012015-03-27 13:31:18 +0100752 assert(0);
Victor Stinnercb0c6022015-03-28 05:24:19 +0100753
754 /* use a fixed value instead of a random value from the stack */
Victor Stinnercb29f012015-03-27 13:31:18 +0100755 t = 0;
756 }
757 return t;
758}
759
Victor Stinner00111242014-08-29 16:31:59 +0200760int
Victor Stinner4bfb4602015-03-27 22:27:24 +0100761_PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
762{
Victor Stinner5ad58212015-09-03 00:14:58 +0200763 return pymonotonic(tp, info, 1);
Victor Stinner4bfb4602015-03-27 22:27:24 +0100764}
765
766int
Victor Stinner00111242014-08-29 16:31:59 +0200767_PyTime_Init(void)
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000768{
Victor Stinnercb29f012015-03-27 13:31:18 +0100769 _PyTime_t t;
770
Victor Stinner00111242014-08-29 16:31:59 +0200771 /* ensure that the system clock works */
Victor Stinnera47b8812015-03-27 18:16:17 +0100772 if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0)
Victor Stinner00111242014-08-29 16:31:59 +0200773 return -1;
Victor Stinnerae586492014-09-02 23:18:25 +0200774
775 /* ensure that the operating system provides a monotonic clock */
Victor Stinnera47b8812015-03-27 18:16:17 +0100776 if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0)
Victor Stinnercb29f012015-03-27 13:31:18 +0100777 return -1;
Victor Stinner13019fd2015-04-03 13:10:54 +0200778
Victor Stinner00111242014-08-29 16:31:59 +0200779 return 0;
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000780}