ยปCore Development>Code coverage>Python/pytime.c

Python code coverage for Python/pytime.c

#countcontent
1n/a#include "Python.h"
2n/a#ifdef MS_WINDOWS
3n/a#include <windows.h>
4n/a#endif
5n/a
6n/a#if defined(__APPLE__)
7n/a#include <mach/mach_time.h> /* mach_absolute_time(), mach_timebase_info() */
8n/a#endif
9n/a
10n/a#define _PyTime_check_mul_overflow(a, b) \
11n/a (assert(b > 0), \
12n/a (_PyTime_t)(a) < _PyTime_MIN / (_PyTime_t)(b) \
13n/a || _PyTime_MAX / (_PyTime_t)(b) < (_PyTime_t)(a))
14n/a
15n/a/* To millisecond (10^-3) */
16n/a#define SEC_TO_MS 1000
17n/a
18n/a/* To microseconds (10^-6) */
19n/a#define MS_TO_US 1000
20n/a#define SEC_TO_US (SEC_TO_MS * MS_TO_US)
21n/a
22n/a/* To nanoseconds (10^-9) */
23n/a#define US_TO_NS 1000
24n/a#define MS_TO_NS (MS_TO_US * US_TO_NS)
25n/a#define SEC_TO_NS (SEC_TO_MS * MS_TO_NS)
26n/a
27n/a/* Conversion from nanoseconds */
28n/a#define NS_TO_MS (1000 * 1000)
29n/a#define NS_TO_US (1000)
30n/a
31n/astatic void
32n/aerror_time_t_overflow(void)
33n/a{
34n/a PyErr_SetString(PyExc_OverflowError,
35n/a "timestamp out of range for platform time_t");
36n/a}
37n/a
38n/atime_t
39n/a_PyLong_AsTime_t(PyObject *obj)
40n/a{
41n/a#if SIZEOF_TIME_T == SIZEOF_LONG_LONG
42n/a long long val;
43n/a val = PyLong_AsLongLong(obj);
44n/a#else
45n/a long val;
46n/a Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
47n/a val = PyLong_AsLong(obj);
48n/a#endif
49n/a if (val == -1 && PyErr_Occurred()) {
50n/a if (PyErr_ExceptionMatches(PyExc_OverflowError))
51n/a error_time_t_overflow();
52n/a return -1;
53n/a }
54n/a return (time_t)val;
55n/a}
56n/a
57n/aPyObject *
58n/a_PyLong_FromTime_t(time_t t)
59n/a{
60n/a#if SIZEOF_TIME_T == SIZEOF_LONG_LONG
61n/a return PyLong_FromLongLong((long long)t);
62n/a#else
63n/a Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
64n/a return PyLong_FromLong((long)t);
65n/a#endif
66n/a}
67n/a
68n/a/* Round to nearest with ties going to nearest even integer
69n/a (_PyTime_ROUND_HALF_EVEN) */
70n/astatic double
71n/a_PyTime_RoundHalfEven(double x)
72n/a{
73n/a double rounded = round(x);
74n/a if (fabs(x-rounded) == 0.5)
75n/a /* halfway case: round to even */
76n/a rounded = 2.0*round(x/2.0);
77n/a return rounded;
78n/a}
79n/a
80n/astatic double
81n/a_PyTime_Round(double x, _PyTime_round_t round)
82n/a{
83n/a /* volatile avoids optimization changing how numbers are rounded */
84n/a volatile double d;
85n/a
86n/a d = x;
87n/a if (round == _PyTime_ROUND_HALF_EVEN)
88n/a d = _PyTime_RoundHalfEven(d);
89n/a else if (round == _PyTime_ROUND_CEILING)
90n/a d = ceil(d);
91n/a else
92n/a d = floor(d);
93n/a return d;
94n/a}
95n/a
96n/astatic int
97n/a_PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
98n/a double denominator, _PyTime_round_t round)
99n/a{
100n/a double intpart, err;
101n/a /* volatile avoids optimization changing how numbers are rounded */
102n/a volatile double floatpart;
103n/a
104n/a floatpart = modf(d, &intpart);
105n/a
106n/a floatpart *= denominator;
107n/a floatpart = _PyTime_Round(floatpart, round);
108n/a if (floatpart >= denominator) {
109n/a floatpart -= denominator;
110n/a intpart += 1.0;
111n/a }
112n/a else if (floatpart < 0) {
113n/a floatpart += denominator;
114n/a intpart -= 1.0;
115n/a }
116n/a assert(0.0 <= floatpart && floatpart < denominator);
117n/a
118n/a *sec = (time_t)intpart;
119n/a *numerator = (long)floatpart;
120n/a
121n/a err = intpart - (double)*sec;
122n/a if (err <= -1.0 || err >= 1.0) {
123n/a error_time_t_overflow();
124n/a return -1;
125n/a }
126n/a return 0;
127n/a}
128n/a
129n/astatic int
130n/a_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
131n/a double denominator, _PyTime_round_t round)
132n/a{
133n/a assert(denominator <= (double)LONG_MAX);
134n/a
135n/a if (PyFloat_Check(obj)) {
136n/a double d = PyFloat_AsDouble(obj);
137n/a return _PyTime_DoubleToDenominator(d, sec, numerator,
138n/a denominator, round);
139n/a }
140n/a else {
141n/a *sec = _PyLong_AsTime_t(obj);
142n/a *numerator = 0;
143n/a if (*sec == (time_t)-1 && PyErr_Occurred())
144n/a return -1;
145n/a return 0;
146n/a }
147n/a}
148n/a
149n/aint
150n/a_PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
151n/a{
152n/a if (PyFloat_Check(obj)) {
153n/a double intpart, err;
154n/a /* volatile avoids optimization changing how numbers are rounded */
155n/a volatile double d;
156n/a
157n/a d = PyFloat_AsDouble(obj);
158n/a d = _PyTime_Round(d, round);
159n/a (void)modf(d, &intpart);
160n/a
161n/a *sec = (time_t)intpart;
162n/a err = intpart - (double)*sec;
163n/a if (err <= -1.0 || err >= 1.0) {
164n/a error_time_t_overflow();
165n/a return -1;
166n/a }
167n/a return 0;
168n/a }
169n/a else {
170n/a *sec = _PyLong_AsTime_t(obj);
171n/a if (*sec == (time_t)-1 && PyErr_Occurred())
172n/a return -1;
173n/a return 0;
174n/a }
175n/a}
176n/a
177n/aint
178n/a_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
179n/a _PyTime_round_t round)
180n/a{
181n/a int res;
182n/a res = _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
183n/a assert(0 <= *nsec && *nsec < SEC_TO_NS);
184n/a return res;
185n/a}
186n/a
187n/aint
188n/a_PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
189n/a _PyTime_round_t round)
190n/a{
191n/a int res;
192n/a res = _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
193n/a assert(0 <= *usec && *usec < SEC_TO_US);
194n/a return res;
195n/a}
196n/a
197n/astatic void
198n/a_PyTime_overflow(void)
199n/a{
200n/a PyErr_SetString(PyExc_OverflowError,
201n/a "timestamp too large to convert to C _PyTime_t");
202n/a}
203n/a
204n/a_PyTime_t
205n/a_PyTime_FromSeconds(int seconds)
206n/a{
207n/a _PyTime_t t;
208n/a t = (_PyTime_t)seconds;
209n/a /* ensure that integer overflow cannot happen, int type should have 32
210n/a bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
211n/a bits). */
212n/a Py_BUILD_ASSERT(INT_MAX <= _PyTime_MAX / SEC_TO_NS);
213n/a Py_BUILD_ASSERT(INT_MIN >= _PyTime_MIN / SEC_TO_NS);
214n/a assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
215n/a || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
216n/a t *= SEC_TO_NS;
217n/a return t;
218n/a}
219n/a
220n/a_PyTime_t
221n/a_PyTime_FromNanoseconds(long long ns)
222n/a{
223n/a _PyTime_t t;
224n/a Py_BUILD_ASSERT(sizeof(long long) <= sizeof(_PyTime_t));
225n/a t = Py_SAFE_DOWNCAST(ns, long long, _PyTime_t);
226n/a return t;
227n/a}
228n/a
229n/a#ifdef HAVE_CLOCK_GETTIME
230n/astatic int
231n/a_PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise)
232n/a{
233n/a _PyTime_t t;
234n/a int res = 0;
235n/a
236n/a Py_BUILD_ASSERT(sizeof(ts->tv_sec) <= sizeof(_PyTime_t));
237n/a t = (_PyTime_t)ts->tv_sec;
238n/a
239n/a if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
240n/a if (raise)
241n/a _PyTime_overflow();
242n/a res = -1;
243n/a }
244n/a t = t * SEC_TO_NS;
245n/a
246n/a t += ts->tv_nsec;
247n/a
248n/a *tp = t;
249n/a return res;
250n/a}
251n/a#elif !defined(MS_WINDOWS)
252n/astatic int
253n/a_PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
254n/a{
255n/a _PyTime_t t;
256n/a int res = 0;
257n/a
258n/a Py_BUILD_ASSERT(sizeof(tv->tv_sec) <= sizeof(_PyTime_t));
259n/a t = (_PyTime_t)tv->tv_sec;
260n/a
261n/a if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
262n/a if (raise)
263n/a _PyTime_overflow();
264n/a res = -1;
265n/a }
266n/a t = t * SEC_TO_NS;
267n/a
268n/a t += (_PyTime_t)tv->tv_usec * US_TO_NS;
269n/a
270n/a *tp = t;
271n/a return res;
272n/a}
273n/a#endif
274n/a
275n/astatic int
276n/a_PyTime_FromFloatObject(_PyTime_t *t, double value, _PyTime_round_t round,
277n/a long unit_to_ns)
278n/a{
279n/a double err;
280n/a /* volatile avoids optimization changing how numbers are rounded */
281n/a volatile double d;
282n/a
283n/a /* convert to a number of nanoseconds */
284n/a d = value;
285n/a d *= (double)unit_to_ns;
286n/a d = _PyTime_Round(d, round);
287n/a
288n/a *t = (_PyTime_t)d;
289n/a err = d - (double)*t;
290n/a if (fabs(err) >= 1.0) {
291n/a _PyTime_overflow();
292n/a return -1;
293n/a }
294n/a return 0;
295n/a}
296n/a
297n/astatic int
298n/a_PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
299n/a long unit_to_ns)
300n/a{
301n/a if (PyFloat_Check(obj)) {
302n/a double d;
303n/a d = PyFloat_AsDouble(obj);
304n/a return _PyTime_FromFloatObject(t, d, round, unit_to_ns);
305n/a }
306n/a else {
307n/a long long sec;
308n/a Py_BUILD_ASSERT(sizeof(long long) <= sizeof(_PyTime_t));
309n/a
310n/a sec = PyLong_AsLongLong(obj);
311n/a if (sec == -1 && PyErr_Occurred()) {
312n/a if (PyErr_ExceptionMatches(PyExc_OverflowError))
313n/a _PyTime_overflow();
314n/a return -1;
315n/a }
316n/a
317n/a if (_PyTime_check_mul_overflow(sec, unit_to_ns)) {
318n/a _PyTime_overflow();
319n/a return -1;
320n/a }
321n/a *t = sec * unit_to_ns;
322n/a return 0;
323n/a }
324n/a}
325n/a
326n/aint
327n/a_PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
328n/a{
329n/a return _PyTime_FromObject(t, obj, round, SEC_TO_NS);
330n/a}
331n/a
332n/aint
333n/a_PyTime_FromMillisecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
334n/a{
335n/a return _PyTime_FromObject(t, obj, round, MS_TO_NS);
336n/a}
337n/a
338n/adouble
339n/a_PyTime_AsSecondsDouble(_PyTime_t t)
340n/a{
341n/a /* volatile avoids optimization changing how numbers are rounded */
342n/a volatile double d;
343n/a
344n/a if (t % SEC_TO_NS == 0) {
345n/a _PyTime_t secs;
346n/a /* Divide using integers to avoid rounding issues on the integer part.
347n/a 1e-9 cannot be stored exactly in IEEE 64-bit. */
348n/a secs = t / SEC_TO_NS;
349n/a d = (double)secs;
350n/a }
351n/a else {
352n/a d = (double)t;
353n/a d /= 1e9;
354n/a }
355n/a return d;
356n/a}
357n/a
358n/aPyObject *
359n/a_PyTime_AsNanosecondsObject(_PyTime_t t)
360n/a{
361n/a Py_BUILD_ASSERT(sizeof(long long) >= sizeof(_PyTime_t));
362n/a return PyLong_FromLongLong((long long)t);
363n/a}
364n/a
365n/astatic _PyTime_t
366n/a_PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
367n/a const _PyTime_round_t round)
368n/a{
369n/a assert(k > 1);
370n/a if (round == _PyTime_ROUND_HALF_EVEN) {
371n/a _PyTime_t x, r, abs_r;
372n/a x = t / k;
373n/a r = t % k;
374n/a abs_r = Py_ABS(r);
375n/a if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) {
376n/a if (t >= 0)
377n/a x++;
378n/a else
379n/a x--;
380n/a }
381n/a return x;
382n/a }
383n/a else if (round == _PyTime_ROUND_CEILING) {
384n/a if (t >= 0)
385n/a return (t + k - 1) / k;
386n/a else
387n/a return t / k;
388n/a }
389n/a else {
390n/a if (t >= 0)
391n/a return t / k;
392n/a else
393n/a return (t - (k - 1)) / k;
394n/a }
395n/a}
396n/a
397n/a_PyTime_t
398n/a_PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
399n/a{
400n/a return _PyTime_Divide(t, NS_TO_MS, round);
401n/a}
402n/a
403n/a_PyTime_t
404n/a_PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
405n/a{
406n/a return _PyTime_Divide(t, NS_TO_US, round);
407n/a}
408n/a
409n/astatic int
410n/a_PyTime_AsTimeval_impl(_PyTime_t t, _PyTime_t *p_secs, int *p_us,
411n/a _PyTime_round_t round)
412n/a{
413n/a _PyTime_t secs, ns;
414n/a int usec;
415n/a int res = 0;
416n/a
417n/a secs = t / SEC_TO_NS;
418n/a ns = t % SEC_TO_NS;
419n/a
420n/a usec = (int)_PyTime_Divide(ns, US_TO_NS, round);
421n/a if (usec < 0) {
422n/a usec += SEC_TO_US;
423n/a if (secs != _PyTime_MIN)
424n/a secs -= 1;
425n/a else
426n/a res = -1;
427n/a }
428n/a else if (usec >= SEC_TO_US) {
429n/a usec -= SEC_TO_US;
430n/a if (secs != _PyTime_MAX)
431n/a secs += 1;
432n/a else
433n/a res = -1;
434n/a }
435n/a assert(0 <= usec && usec < SEC_TO_US);
436n/a
437n/a *p_secs = secs;
438n/a *p_us = usec;
439n/a
440n/a return res;
441n/a}
442n/a
443n/astatic int
444n/a_PyTime_AsTimevalStruct_impl(_PyTime_t t, struct timeval *tv,
445n/a _PyTime_round_t round, int raise)
446n/a{
447n/a _PyTime_t secs, secs2;
448n/a int us;
449n/a int res;
450n/a
451n/a res = _PyTime_AsTimeval_impl(t, &secs, &us, round);
452n/a
453n/a#ifdef MS_WINDOWS
454n/a tv->tv_sec = (long)secs;
455n/a#else
456n/a tv->tv_sec = secs;
457n/a#endif
458n/a tv->tv_usec = us;
459n/a
460n/a secs2 = (_PyTime_t)tv->tv_sec;
461n/a if (res < 0 || secs2 != secs) {
462n/a if (raise)
463n/a error_time_t_overflow();
464n/a return -1;
465n/a }
466n/a return 0;
467n/a}
468n/a
469n/aint
470n/a_PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
471n/a{
472n/a return _PyTime_AsTimevalStruct_impl(t, tv, round, 1);
473n/a}
474n/a
475n/aint
476n/a_PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
477n/a{
478n/a return _PyTime_AsTimevalStruct_impl(t, tv, round, 0);
479n/a}
480n/a
481n/aint
482n/a_PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us,
483n/a _PyTime_round_t round)
484n/a{
485n/a _PyTime_t secs;
486n/a int res;
487n/a
488n/a res = _PyTime_AsTimeval_impl(t, &secs, us, round);
489n/a
490n/a *p_secs = secs;
491n/a
492n/a if (res < 0 || (_PyTime_t)*p_secs != secs) {
493n/a error_time_t_overflow();
494n/a return -1;
495n/a }
496n/a return 0;
497n/a}
498n/a
499n/a
500n/a#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
501n/aint
502n/a_PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
503n/a{
504n/a _PyTime_t secs, nsec;
505n/a
506n/a secs = t / SEC_TO_NS;
507n/a nsec = t % SEC_TO_NS;
508n/a if (nsec < 0) {
509n/a nsec += SEC_TO_NS;
510n/a secs -= 1;
511n/a }
512n/a ts->tv_sec = (time_t)secs;
513n/a assert(0 <= nsec && nsec < SEC_TO_NS);
514n/a ts->tv_nsec = nsec;
515n/a
516n/a if ((_PyTime_t)ts->tv_sec != secs) {
517n/a error_time_t_overflow();
518n/a return -1;
519n/a }
520n/a return 0;
521n/a}
522n/a#endif
523n/a
524n/astatic int
525n/apygettimeofday(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
526n/a{
527n/a#ifdef MS_WINDOWS
528n/a FILETIME system_time;
529n/a ULARGE_INTEGER large;
530n/a
531n/a assert(info == NULL || raise);
532n/a
533n/a GetSystemTimeAsFileTime(&system_time);
534n/a large.u.LowPart = system_time.dwLowDateTime;
535n/a large.u.HighPart = system_time.dwHighDateTime;
536n/a /* 11,644,473,600,000,000,000: number of nanoseconds between
537n/a the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
538n/a days). */
539n/a *tp = large.QuadPart * 100 - 11644473600000000000;
540n/a if (info) {
541n/a DWORD timeAdjustment, timeIncrement;
542n/a BOOL isTimeAdjustmentDisabled, ok;
543n/a
544n/a info->implementation = "GetSystemTimeAsFileTime()";
545n/a info->monotonic = 0;
546n/a ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
547n/a &isTimeAdjustmentDisabled);
548n/a if (!ok) {
549n/a PyErr_SetFromWindowsErr(0);
550n/a return -1;
551n/a }
552n/a info->resolution = timeIncrement * 1e-7;
553n/a info->adjustable = 1;
554n/a }
555n/a
556n/a#else /* MS_WINDOWS */
557n/a int err;
558n/a#ifdef HAVE_CLOCK_GETTIME
559n/a struct timespec ts;
560n/a#else
561n/a struct timeval tv;
562n/a#endif
563n/a
564n/a assert(info == NULL || raise);
565n/a
566n/a#ifdef HAVE_CLOCK_GETTIME
567n/a err = clock_gettime(CLOCK_REALTIME, &ts);
568n/a if (err) {
569n/a if (raise)
570n/a PyErr_SetFromErrno(PyExc_OSError);
571n/a return -1;
572n/a }
573n/a if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
574n/a return -1;
575n/a
576n/a if (info) {
577n/a struct timespec res;
578n/a info->implementation = "clock_gettime(CLOCK_REALTIME)";
579n/a info->monotonic = 0;
580n/a info->adjustable = 1;
581n/a if (clock_getres(CLOCK_REALTIME, &res) == 0)
582n/a info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
583n/a else
584n/a info->resolution = 1e-9;
585n/a }
586n/a#else /* HAVE_CLOCK_GETTIME */
587n/a
588n/a /* test gettimeofday() */
589n/a#ifdef GETTIMEOFDAY_NO_TZ
590n/a err = gettimeofday(&tv);
591n/a#else
592n/a err = gettimeofday(&tv, (struct timezone *)NULL);
593n/a#endif
594n/a if (err) {
595n/a if (raise)
596n/a PyErr_SetFromErrno(PyExc_OSError);
597n/a return -1;
598n/a }
599n/a if (_PyTime_FromTimeval(tp, &tv, raise) < 0)
600n/a return -1;
601n/a
602n/a if (info) {
603n/a info->implementation = "gettimeofday()";
604n/a info->resolution = 1e-6;
605n/a info->monotonic = 0;
606n/a info->adjustable = 1;
607n/a }
608n/a#endif /* !HAVE_CLOCK_GETTIME */
609n/a#endif /* !MS_WINDOWS */
610n/a return 0;
611n/a}
612n/a
613n/a_PyTime_t
614n/a_PyTime_GetSystemClock(void)
615n/a{
616n/a _PyTime_t t;
617n/a if (pygettimeofday(&t, NULL, 0) < 0) {
618n/a /* should not happen, _PyTime_Init() checked the clock at startup */
619n/a assert(0);
620n/a
621n/a /* use a fixed value instead of a random value from the stack */
622n/a t = 0;
623n/a }
624n/a return t;
625n/a}
626n/a
627n/aint
628n/a_PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
629n/a{
630n/a return pygettimeofday(t, info, 1);
631n/a}
632n/a
633n/astatic int
634n/apymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
635n/a{
636n/a#if defined(MS_WINDOWS)
637n/a ULONGLONG ticks;
638n/a _PyTime_t t;
639n/a
640n/a assert(info == NULL || raise);
641n/a
642n/a ticks = GetTickCount64();
643n/a Py_BUILD_ASSERT(sizeof(ticks) <= sizeof(_PyTime_t));
644n/a t = (_PyTime_t)ticks;
645n/a
646n/a if (_PyTime_check_mul_overflow(t, MS_TO_NS)) {
647n/a if (raise) {
648n/a _PyTime_overflow();
649n/a return -1;
650n/a }
651n/a /* Hello, time traveler! */
652n/a assert(0);
653n/a }
654n/a *tp = t * MS_TO_NS;
655n/a
656n/a if (info) {
657n/a DWORD timeAdjustment, timeIncrement;
658n/a BOOL isTimeAdjustmentDisabled, ok;
659n/a info->implementation = "GetTickCount64()";
660n/a info->monotonic = 1;
661n/a ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
662n/a &isTimeAdjustmentDisabled);
663n/a if (!ok) {
664n/a PyErr_SetFromWindowsErr(0);
665n/a return -1;
666n/a }
667n/a info->resolution = timeIncrement * 1e-7;
668n/a info->adjustable = 0;
669n/a }
670n/a
671n/a#elif defined(__APPLE__)
672n/a static mach_timebase_info_data_t timebase;
673n/a uint64_t time;
674n/a
675n/a if (timebase.denom == 0) {
676n/a /* According to the Technical Q&A QA1398, mach_timebase_info() cannot
677n/a fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
678n/a (void)mach_timebase_info(&timebase);
679n/a }
680n/a
681n/a time = mach_absolute_time();
682n/a
683n/a /* apply timebase factor */
684n/a time *= timebase.numer;
685n/a time /= timebase.denom;
686n/a
687n/a *tp = time;
688n/a
689n/a if (info) {
690n/a info->implementation = "mach_absolute_time()";
691n/a info->resolution = (double)timebase.numer / timebase.denom * 1e-9;
692n/a info->monotonic = 1;
693n/a info->adjustable = 0;
694n/a }
695n/a
696n/a#else
697n/a struct timespec ts;
698n/a#ifdef CLOCK_HIGHRES
699n/a const clockid_t clk_id = CLOCK_HIGHRES;
700n/a const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
701n/a#else
702n/a const clockid_t clk_id = CLOCK_MONOTONIC;
703n/a const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
704n/a#endif
705n/a
706n/a assert(info == NULL || raise);
707n/a
708n/a if (clock_gettime(clk_id, &ts) != 0) {
709n/a if (raise) {
710n/a PyErr_SetFromErrno(PyExc_OSError);
711n/a return -1;
712n/a }
713n/a return -1;
714n/a }
715n/a
716n/a if (info) {
717n/a struct timespec res;
718n/a info->monotonic = 1;
719n/a info->implementation = implementation;
720n/a info->adjustable = 0;
721n/a if (clock_getres(clk_id, &res) != 0) {
722n/a PyErr_SetFromErrno(PyExc_OSError);
723n/a return -1;
724n/a }
725n/a info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
726n/a }
727n/a if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
728n/a return -1;
729n/a#endif
730n/a return 0;
731n/a}
732n/a
733n/a_PyTime_t
734n/a_PyTime_GetMonotonicClock(void)
735n/a{
736n/a _PyTime_t t;
737n/a if (pymonotonic(&t, NULL, 0) < 0) {
738n/a /* should not happen, _PyTime_Init() checked that monotonic clock at
739n/a startup */
740n/a assert(0);
741n/a
742n/a /* use a fixed value instead of a random value from the stack */
743n/a t = 0;
744n/a }
745n/a return t;
746n/a}
747n/a
748n/aint
749n/a_PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
750n/a{
751n/a return pymonotonic(tp, info, 1);
752n/a}
753n/a
754n/aint
755n/a_PyTime_Init(void)
756n/a{
757n/a _PyTime_t t;
758n/a
759n/a /* ensure that the system clock works */
760n/a if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0)
761n/a return -1;
762n/a
763n/a /* ensure that the operating system provides a monotonic clock */
764n/a if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0)
765n/a return -1;
766n/a
767n/a return 0;
768n/a}
769n/a
770n/aint
771n/a_PyTime_localtime(time_t t, struct tm *tm)
772n/a{
773n/a#ifdef MS_WINDOWS
774n/a int error;
775n/a
776n/a error = localtime_s(tm, &t);
777n/a if (error != 0) {
778n/a errno = error;
779n/a PyErr_SetFromErrno(PyExc_OSError);
780n/a return -1;
781n/a }
782n/a return 0;
783n/a#else /* !MS_WINDOWS */
784n/a if (localtime_r(&t, tm) == NULL) {
785n/a#ifdef EINVAL
786n/a if (errno == 0)
787n/a errno = EINVAL;
788n/a#endif
789n/a PyErr_SetFromErrno(PyExc_OSError);
790n/a return -1;
791n/a }
792n/a return 0;
793n/a#endif /* MS_WINDOWS */
794n/a}
795n/a
796n/aint
797n/a_PyTime_gmtime(time_t t, struct tm *tm)
798n/a{
799n/a#ifdef MS_WINDOWS
800n/a int error;
801n/a
802n/a error = gmtime_s(tm, &t);
803n/a if (error != 0) {
804n/a errno = error;
805n/a PyErr_SetFromErrno(PyExc_OSError);
806n/a return -1;
807n/a }
808n/a return 0;
809n/a#else /* !MS_WINDOWS */
810n/a if (gmtime_r(&t, tm) == NULL) {
811n/a#ifdef EINVAL
812n/a if (errno == 0)
813n/a errno = EINVAL;
814n/a#endif
815n/a PyErr_SetFromErrno(PyExc_OSError);
816n/a return -1;
817n/a }
818n/a return 0;
819n/a#endif /* MS_WINDOWS */
820n/a}