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.

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