You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

840 lines
19 KiB

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