pytime.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. #include "Python.h"
  2. #ifdef MS_WINDOWS
  3. # include <winsock2.h> // struct timeval
  4. #endif
  5. #if defined(__APPLE__)
  6. # include <mach/mach_time.h> // mach_absolute_time(), mach_timebase_info()
  7. #if defined(__APPLE__) && defined(__has_builtin)
  8. # if __has_builtin(__builtin_available)
  9. # define HAVE_CLOCK_GETTIME_RUNTIME __builtin_available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
  10. # endif
  11. #endif
  12. #endif
  13. /* To millisecond (10^-3) */
  14. #define SEC_TO_MS 1000
  15. /* To microseconds (10^-6) */
  16. #define MS_TO_US 1000
  17. #define SEC_TO_US (SEC_TO_MS * MS_TO_US)
  18. /* To nanoseconds (10^-9) */
  19. #define US_TO_NS 1000
  20. #define MS_TO_NS (MS_TO_US * US_TO_NS)
  21. #define SEC_TO_NS (SEC_TO_MS * MS_TO_NS)
  22. /* Conversion from nanoseconds */
  23. #define NS_TO_MS (1000 * 1000)
  24. #define NS_TO_US (1000)
  25. #define NS_TO_100NS (100)
  26. #if SIZEOF_TIME_T == SIZEOF_LONG_LONG
  27. # define PY_TIME_T_MAX LLONG_MAX
  28. # define PY_TIME_T_MIN LLONG_MIN
  29. #elif SIZEOF_TIME_T == SIZEOF_LONG
  30. # define PY_TIME_T_MAX LONG_MAX
  31. # define PY_TIME_T_MIN LONG_MIN
  32. #else
  33. # error "unsupported time_t size"
  34. #endif
  35. #if PY_TIME_T_MAX + PY_TIME_T_MIN != -1
  36. # error "time_t is not a two's complement integer type"
  37. #endif
  38. #if _PyTime_MIN + _PyTime_MAX != -1
  39. # error "_PyTime_t is not a two's complement integer type"
  40. #endif
  41. static void
  42. pytime_time_t_overflow(void)
  43. {
  44. PyErr_SetString(PyExc_OverflowError,
  45. "timestamp out of range for platform time_t");
  46. }
  47. static void
  48. pytime_overflow(void)
  49. {
  50. PyErr_SetString(PyExc_OverflowError,
  51. "timestamp too large to convert to C _PyTime_t");
  52. }
  53. static inline _PyTime_t
  54. pytime_from_nanoseconds(_PyTime_t t)
  55. {
  56. // _PyTime_t is a number of nanoseconds
  57. return t;
  58. }
  59. static inline _PyTime_t
  60. pytime_as_nanoseconds(_PyTime_t t)
  61. {
  62. // _PyTime_t is a number of nanoseconds: see pytime_from_nanoseconds()
  63. return t;
  64. }
  65. // Compute t1 + t2. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
  66. static inline int
  67. pytime_add(_PyTime_t *t1, _PyTime_t t2)
  68. {
  69. if (t2 > 0 && *t1 > _PyTime_MAX - t2) {
  70. *t1 = _PyTime_MAX;
  71. return -1;
  72. }
  73. else if (t2 < 0 && *t1 < _PyTime_MIN - t2) {
  74. *t1 = _PyTime_MIN;
  75. return -1;
  76. }
  77. else {
  78. *t1 += t2;
  79. return 0;
  80. }
  81. }
  82. _PyTime_t
  83. _PyTime_Add(_PyTime_t t1, _PyTime_t t2)
  84. {
  85. (void)pytime_add(&t1, t2);
  86. return t1;
  87. }
  88. static inline int
  89. pytime_mul_check_overflow(_PyTime_t a, _PyTime_t b)
  90. {
  91. if (b != 0) {
  92. assert(b > 0);
  93. return ((a < _PyTime_MIN / b) || (_PyTime_MAX / b < a));
  94. }
  95. else {
  96. return 0;
  97. }
  98. }
  99. // Compute t * k. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
  100. static inline int
  101. pytime_mul(_PyTime_t *t, _PyTime_t k)
  102. {
  103. assert(k >= 0);
  104. if (pytime_mul_check_overflow(*t, k)) {
  105. *t = (*t >= 0) ? _PyTime_MAX : _PyTime_MIN;
  106. return -1;
  107. }
  108. else {
  109. *t *= k;
  110. return 0;
  111. }
  112. }
  113. // Compute t * k. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
  114. static inline _PyTime_t
  115. _PyTime_Mul(_PyTime_t t, _PyTime_t k)
  116. {
  117. (void)pytime_mul(&t, k);
  118. return t;
  119. }
  120. _PyTime_t
  121. _PyTime_MulDiv(_PyTime_t ticks, _PyTime_t mul, _PyTime_t div)
  122. {
  123. /* Compute (ticks * mul / div) in two parts to reduce the risk of integer
  124. overflow: compute the integer part, and then the remaining part.
  125. (ticks * mul) / div == (ticks / div) * mul + (ticks % div) * mul / div
  126. */
  127. _PyTime_t intpart, remaining;
  128. intpart = ticks / div;
  129. ticks %= div;
  130. remaining = _PyTime_Mul(ticks, mul) / div;
  131. // intpart * mul + remaining
  132. return _PyTime_Add(_PyTime_Mul(intpart, mul), remaining);
  133. }
  134. time_t
  135. _PyLong_AsTime_t(PyObject *obj)
  136. {
  137. #if SIZEOF_TIME_T == SIZEOF_LONG_LONG
  138. long long val = PyLong_AsLongLong(obj);
  139. #elif SIZEOF_TIME_T <= SIZEOF_LONG
  140. long val = PyLong_AsLong(obj);
  141. #else
  142. # error "unsupported time_t size"
  143. #endif
  144. if (val == -1 && PyErr_Occurred()) {
  145. if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
  146. pytime_time_t_overflow();
  147. }
  148. return -1;
  149. }
  150. return (time_t)val;
  151. }
  152. PyObject *
  153. _PyLong_FromTime_t(time_t t)
  154. {
  155. #if SIZEOF_TIME_T == SIZEOF_LONG_LONG
  156. return PyLong_FromLongLong((long long)t);
  157. #elif SIZEOF_TIME_T <= SIZEOF_LONG
  158. return PyLong_FromLong((long)t);
  159. #else
  160. # error "unsupported time_t size"
  161. #endif
  162. }
  163. // Convert _PyTime_t to time_t.
  164. // Return 0 on success. Return -1 and clamp the value on overflow.
  165. static int
  166. _PyTime_AsTime_t(_PyTime_t t, time_t *t2)
  167. {
  168. #if SIZEOF_TIME_T < _SIZEOF_PYTIME_T
  169. if ((_PyTime_t)PY_TIME_T_MAX < t) {
  170. *t2 = PY_TIME_T_MAX;
  171. return -1;
  172. }
  173. if (t < (_PyTime_t)PY_TIME_T_MIN) {
  174. *t2 = PY_TIME_T_MIN;
  175. return -1;
  176. }
  177. #endif
  178. *t2 = (time_t)t;
  179. return 0;
  180. }
  181. #ifdef MS_WINDOWS
  182. // Convert _PyTime_t to long.
  183. // Return 0 on success. Return -1 and clamp the value on overflow.
  184. static int
  185. _PyTime_AsLong(_PyTime_t t, long *t2)
  186. {
  187. #if SIZEOF_LONG < _SIZEOF_PYTIME_T
  188. if ((_PyTime_t)LONG_MAX < t) {
  189. *t2 = LONG_MAX;
  190. return -1;
  191. }
  192. if (t < (_PyTime_t)LONG_MIN) {
  193. *t2 = LONG_MIN;
  194. return -1;
  195. }
  196. #endif
  197. *t2 = (long)t;
  198. return 0;
  199. }
  200. #endif
  201. /* Round to nearest with ties going to nearest even integer
  202. (_PyTime_ROUND_HALF_EVEN) */
  203. static double
  204. pytime_round_half_even(double x)
  205. {
  206. double rounded = round(x);
  207. if (fabs(x-rounded) == 0.5) {
  208. /* halfway case: round to even */
  209. rounded = 2.0 * round(x / 2.0);
  210. }
  211. return rounded;
  212. }
  213. static double
  214. pytime_round(double x, _PyTime_round_t round)
  215. {
  216. /* volatile avoids optimization changing how numbers are rounded */
  217. volatile double d;
  218. d = x;
  219. if (round == _PyTime_ROUND_HALF_EVEN) {
  220. d = pytime_round_half_even(d);
  221. }
  222. else if (round == _PyTime_ROUND_CEILING) {
  223. d = ceil(d);
  224. }
  225. else if (round == _PyTime_ROUND_FLOOR) {
  226. d = floor(d);
  227. }
  228. else {
  229. assert(round == _PyTime_ROUND_UP);
  230. d = (d >= 0.0) ? ceil(d) : floor(d);
  231. }
  232. return d;
  233. }
  234. static int
  235. pytime_double_to_denominator(double d, time_t *sec, long *numerator,
  236. long idenominator, _PyTime_round_t round)
  237. {
  238. double denominator = (double)idenominator;
  239. double intpart;
  240. /* volatile avoids optimization changing how numbers are rounded */
  241. volatile double floatpart;
  242. floatpart = modf(d, &intpart);
  243. floatpart *= denominator;
  244. floatpart = pytime_round(floatpart, round);
  245. if (floatpart >= denominator) {
  246. floatpart -= denominator;
  247. intpart += 1.0;
  248. }
  249. else if (floatpart < 0) {
  250. floatpart += denominator;
  251. intpart -= 1.0;
  252. }
  253. assert(0.0 <= floatpart && floatpart < denominator);
  254. /*
  255. Conversion of an out-of-range value to time_t gives undefined behaviour
  256. (C99 §6.3.1.4p1), so we must guard against it. However, checking that
  257. `intpart` is in range is delicate: the obvious expression `intpart <=
  258. PY_TIME_T_MAX` will first convert the value `PY_TIME_T_MAX` to a double,
  259. potentially changing its value and leading to us failing to catch some
  260. UB-inducing values. The code below works correctly under the mild
  261. assumption that time_t is a two's complement integer type with no trap
  262. representation, and that `PY_TIME_T_MIN` is within the representable
  263. range of a C double.
  264. Note: we want the `if` condition below to be true for NaNs; therefore,
  265. resist any temptation to simplify by applying De Morgan's laws.
  266. */
  267. if (!((double)PY_TIME_T_MIN <= intpart && intpart < -(double)PY_TIME_T_MIN)) {
  268. pytime_time_t_overflow();
  269. return -1;
  270. }
  271. *sec = (time_t)intpart;
  272. *numerator = (long)floatpart;
  273. assert(0 <= *numerator && *numerator < idenominator);
  274. return 0;
  275. }
  276. static int
  277. pytime_object_to_denominator(PyObject *obj, time_t *sec, long *numerator,
  278. long denominator, _PyTime_round_t round)
  279. {
  280. assert(denominator >= 1);
  281. if (PyFloat_Check(obj)) {
  282. double d = PyFloat_AsDouble(obj);
  283. if (Py_IS_NAN(d)) {
  284. *numerator = 0;
  285. PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
  286. return -1;
  287. }
  288. return pytime_double_to_denominator(d, sec, numerator,
  289. denominator, round);
  290. }
  291. else {
  292. *sec = _PyLong_AsTime_t(obj);
  293. *numerator = 0;
  294. if (*sec == (time_t)-1 && PyErr_Occurred()) {
  295. return -1;
  296. }
  297. return 0;
  298. }
  299. }
  300. int
  301. _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
  302. {
  303. if (PyFloat_Check(obj)) {
  304. double intpart;
  305. /* volatile avoids optimization changing how numbers are rounded */
  306. volatile double d;
  307. d = PyFloat_AsDouble(obj);
  308. if (Py_IS_NAN(d)) {
  309. PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
  310. return -1;
  311. }
  312. d = pytime_round(d, round);
  313. (void)modf(d, &intpart);
  314. /* See comments in pytime_double_to_denominator */
  315. if (!((double)PY_TIME_T_MIN <= intpart && intpart < -(double)PY_TIME_T_MIN)) {
  316. pytime_time_t_overflow();
  317. return -1;
  318. }
  319. *sec = (time_t)intpart;
  320. return 0;
  321. }
  322. else {
  323. *sec = _PyLong_AsTime_t(obj);
  324. if (*sec == (time_t)-1 && PyErr_Occurred()) {
  325. return -1;
  326. }
  327. return 0;
  328. }
  329. }
  330. int
  331. _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
  332. _PyTime_round_t round)
  333. {
  334. return pytime_object_to_denominator(obj, sec, nsec, SEC_TO_NS, round);
  335. }
  336. int
  337. _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
  338. _PyTime_round_t round)
  339. {
  340. return pytime_object_to_denominator(obj, sec, usec, SEC_TO_US, round);
  341. }
  342. _PyTime_t
  343. _PyTime_FromSeconds(int seconds)
  344. {
  345. /* ensure that integer overflow cannot happen, int type should have 32
  346. bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_NS takes 30
  347. bits). */
  348. static_assert(INT_MAX <= _PyTime_MAX / SEC_TO_NS, "_PyTime_t overflow");
  349. static_assert(INT_MIN >= _PyTime_MIN / SEC_TO_NS, "_PyTime_t underflow");
  350. _PyTime_t t = (_PyTime_t)seconds;
  351. assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
  352. || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
  353. t *= SEC_TO_NS;
  354. return pytime_from_nanoseconds(t);
  355. }
  356. _PyTime_t
  357. _PyTime_FromNanoseconds(_PyTime_t ns)
  358. {
  359. return pytime_from_nanoseconds(ns);
  360. }
  361. _PyTime_t
  362. _PyTime_FromMicrosecondsClamp(_PyTime_t us)
  363. {
  364. _PyTime_t ns = _PyTime_Mul(us, US_TO_NS);
  365. return pytime_from_nanoseconds(ns);
  366. }
  367. int
  368. _PyTime_FromNanosecondsObject(_PyTime_t *tp, PyObject *obj)
  369. {
  370. if (!PyLong_Check(obj)) {
  371. PyErr_Format(PyExc_TypeError, "expect int, got %s",
  372. Py_TYPE(obj)->tp_name);
  373. return -1;
  374. }
  375. static_assert(sizeof(long long) == sizeof(_PyTime_t),
  376. "_PyTime_t is not long long");
  377. long long nsec = PyLong_AsLongLong(obj);
  378. if (nsec == -1 && PyErr_Occurred()) {
  379. if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
  380. pytime_overflow();
  381. }
  382. return -1;
  383. }
  384. _PyTime_t t = (_PyTime_t)nsec;
  385. *tp = pytime_from_nanoseconds(t);
  386. return 0;
  387. }
  388. #ifdef HAVE_CLOCK_GETTIME
  389. static int
  390. pytime_fromtimespec(_PyTime_t *tp, struct timespec *ts, int raise_exc)
  391. {
  392. _PyTime_t t, tv_nsec;
  393. static_assert(sizeof(ts->tv_sec) <= sizeof(_PyTime_t),
  394. "timespec.tv_sec is larger than _PyTime_t");
  395. t = (_PyTime_t)ts->tv_sec;
  396. int res1 = pytime_mul(&t, SEC_TO_NS);
  397. tv_nsec = ts->tv_nsec;
  398. int res2 = pytime_add(&t, tv_nsec);
  399. *tp = pytime_from_nanoseconds(t);
  400. if (raise_exc && (res1 < 0 || res2 < 0)) {
  401. pytime_overflow();
  402. return -1;
  403. }
  404. return 0;
  405. }
  406. int
  407. _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts)
  408. {
  409. return pytime_fromtimespec(tp, ts, 1);
  410. }
  411. #endif
  412. #ifndef MS_WINDOWS
  413. static int
  414. pytime_fromtimeval(_PyTime_t *tp, struct timeval *tv, int raise_exc)
  415. {
  416. static_assert(sizeof(tv->tv_sec) <= sizeof(_PyTime_t),
  417. "timeval.tv_sec is larger than _PyTime_t");
  418. _PyTime_t t = (_PyTime_t)tv->tv_sec;
  419. int res1 = pytime_mul(&t, SEC_TO_NS);
  420. _PyTime_t usec = (_PyTime_t)tv->tv_usec * US_TO_NS;
  421. int res2 = pytime_add(&t, usec);
  422. *tp = pytime_from_nanoseconds(t);
  423. if (raise_exc && (res1 < 0 || res2 < 0)) {
  424. pytime_overflow();
  425. return -1;
  426. }
  427. return 0;
  428. }
  429. int
  430. _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv)
  431. {
  432. return pytime_fromtimeval(tp, tv, 1);
  433. }
  434. #endif
  435. static int
  436. pytime_from_double(_PyTime_t *tp, double value, _PyTime_round_t round,
  437. long unit_to_ns)
  438. {
  439. /* volatile avoids optimization changing how numbers are rounded */
  440. volatile double d;
  441. /* convert to a number of nanoseconds */
  442. d = value;
  443. d *= (double)unit_to_ns;
  444. d = pytime_round(d, round);
  445. /* See comments in pytime_double_to_denominator */
  446. if (!((double)_PyTime_MIN <= d && d < -(double)_PyTime_MIN)) {
  447. pytime_time_t_overflow();
  448. return -1;
  449. }
  450. _PyTime_t ns = (_PyTime_t)d;
  451. *tp = pytime_from_nanoseconds(ns);
  452. return 0;
  453. }
  454. static int
  455. pytime_from_object(_PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
  456. long unit_to_ns)
  457. {
  458. if (PyFloat_Check(obj)) {
  459. double d;
  460. d = PyFloat_AsDouble(obj);
  461. if (Py_IS_NAN(d)) {
  462. PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
  463. return -1;
  464. }
  465. return pytime_from_double(tp, d, round, unit_to_ns);
  466. }
  467. else {
  468. long long sec = PyLong_AsLongLong(obj);
  469. if (sec == -1 && PyErr_Occurred()) {
  470. if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
  471. pytime_overflow();
  472. }
  473. return -1;
  474. }
  475. static_assert(sizeof(long long) <= sizeof(_PyTime_t),
  476. "_PyTime_t is smaller than long long");
  477. _PyTime_t ns = (_PyTime_t)sec;
  478. if (pytime_mul(&ns, unit_to_ns) < 0) {
  479. pytime_overflow();
  480. return -1;
  481. }
  482. *tp = pytime_from_nanoseconds(ns);
  483. return 0;
  484. }
  485. }
  486. int
  487. _PyTime_FromSecondsObject(_PyTime_t *tp, PyObject *obj, _PyTime_round_t round)
  488. {
  489. return pytime_from_object(tp, obj, round, SEC_TO_NS);
  490. }
  491. int
  492. _PyTime_FromMillisecondsObject(_PyTime_t *tp, PyObject *obj, _PyTime_round_t round)
  493. {
  494. return pytime_from_object(tp, obj, round, MS_TO_NS);
  495. }
  496. double
  497. _PyTime_AsSecondsDouble(_PyTime_t t)
  498. {
  499. /* volatile avoids optimization changing how numbers are rounded */
  500. volatile double d;
  501. _PyTime_t ns = pytime_as_nanoseconds(t);
  502. if (ns % SEC_TO_NS == 0) {
  503. /* Divide using integers to avoid rounding issues on the integer part.
  504. 1e-9 cannot be stored exactly in IEEE 64-bit. */
  505. _PyTime_t secs = ns / SEC_TO_NS;
  506. d = (double)secs;
  507. }
  508. else {
  509. d = (double)ns;
  510. d /= 1e9;
  511. }
  512. return d;
  513. }
  514. PyObject *
  515. _PyTime_AsNanosecondsObject(_PyTime_t t)
  516. {
  517. _PyTime_t ns = pytime_as_nanoseconds(t);
  518. static_assert(sizeof(long long) >= sizeof(_PyTime_t),
  519. "_PyTime_t is larger than long long");
  520. return PyLong_FromLongLong((long long)ns);
  521. }
  522. static _PyTime_t
  523. pytime_divide_round_up(const _PyTime_t t, const _PyTime_t k)
  524. {
  525. assert(k > 1);
  526. if (t >= 0) {
  527. // Don't use (t + k - 1) / k to avoid integer overflow
  528. // if t is equal to _PyTime_MAX
  529. _PyTime_t q = t / k;
  530. if (t % k) {
  531. q += 1;
  532. }
  533. return q;
  534. }
  535. else {
  536. // Don't use (t - (k - 1)) / k to avoid integer overflow
  537. // if t is equals to _PyTime_MIN.
  538. _PyTime_t q = t / k;
  539. if (t % k) {
  540. q -= 1;
  541. }
  542. return q;
  543. }
  544. }
  545. static _PyTime_t
  546. pytime_divide(const _PyTime_t t, const _PyTime_t k,
  547. const _PyTime_round_t round)
  548. {
  549. assert(k > 1);
  550. if (round == _PyTime_ROUND_HALF_EVEN) {
  551. _PyTime_t x = t / k;
  552. _PyTime_t r = t % k;
  553. _PyTime_t abs_r = Py_ABS(r);
  554. if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) {
  555. if (t >= 0) {
  556. x++;
  557. }
  558. else {
  559. x--;
  560. }
  561. }
  562. return x;
  563. }
  564. else if (round == _PyTime_ROUND_CEILING) {
  565. if (t >= 0) {
  566. return pytime_divide_round_up(t, k);
  567. }
  568. else {
  569. return t / k;
  570. }
  571. }
  572. else if (round == _PyTime_ROUND_FLOOR){
  573. if (t >= 0) {
  574. return t / k;
  575. }
  576. else {
  577. return pytime_divide_round_up(t, k);
  578. }
  579. }
  580. else {
  581. assert(round == _PyTime_ROUND_UP);
  582. return pytime_divide_round_up(t, k);
  583. }
  584. }
  585. // Compute (t / k, t % k) in (pq, pr).
  586. // Make sure that 0 <= pr < k.
  587. // Return 0 on success.
  588. // Return -1 on underflow and store (_PyTime_MIN, 0) in (pq, pr).
  589. static int
  590. pytime_divmod(const _PyTime_t t, const _PyTime_t k,
  591. _PyTime_t *pq, _PyTime_t *pr)
  592. {
  593. assert(k > 1);
  594. _PyTime_t q = t / k;
  595. _PyTime_t r = t % k;
  596. if (r < 0) {
  597. if (q == _PyTime_MIN) {
  598. *pq = _PyTime_MIN;
  599. *pr = 0;
  600. return -1;
  601. }
  602. r += k;
  603. q -= 1;
  604. }
  605. assert(0 <= r && r < k);
  606. *pq = q;
  607. *pr = r;
  608. return 0;
  609. }
  610. _PyTime_t
  611. _PyTime_AsNanoseconds(_PyTime_t t)
  612. {
  613. return pytime_as_nanoseconds(t);
  614. }
  615. #ifdef MS_WINDOWS
  616. _PyTime_t
  617. _PyTime_As100Nanoseconds(_PyTime_t t, _PyTime_round_t round)
  618. {
  619. _PyTime_t ns = pytime_as_nanoseconds(t);
  620. return pytime_divide(ns, NS_TO_100NS, round);
  621. }
  622. #endif
  623. _PyTime_t
  624. _PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
  625. {
  626. _PyTime_t ns = pytime_as_nanoseconds(t);
  627. return pytime_divide(ns, NS_TO_US, round);
  628. }
  629. _PyTime_t
  630. _PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
  631. {
  632. _PyTime_t ns = pytime_as_nanoseconds(t);
  633. return pytime_divide(ns, NS_TO_MS, round);
  634. }
  635. static int
  636. pytime_as_timeval(_PyTime_t t, _PyTime_t *ptv_sec, int *ptv_usec,
  637. _PyTime_round_t round)
  638. {
  639. _PyTime_t ns = pytime_as_nanoseconds(t);
  640. _PyTime_t us = pytime_divide(ns, US_TO_NS, round);
  641. _PyTime_t tv_sec, tv_usec;
  642. int res = pytime_divmod(us, SEC_TO_US, &tv_sec, &tv_usec);
  643. *ptv_sec = tv_sec;
  644. *ptv_usec = (int)tv_usec;
  645. return res;
  646. }
  647. static int
  648. pytime_as_timeval_struct(_PyTime_t t, struct timeval *tv,
  649. _PyTime_round_t round, int raise_exc)
  650. {
  651. _PyTime_t tv_sec;
  652. int tv_usec;
  653. int res = pytime_as_timeval(t, &tv_sec, &tv_usec, round);
  654. int res2;
  655. #ifdef MS_WINDOWS
  656. // On Windows, timeval.tv_sec type is long
  657. res2 = _PyTime_AsLong(tv_sec, &tv->tv_sec);
  658. #else
  659. res2 = _PyTime_AsTime_t(tv_sec, &tv->tv_sec);
  660. #endif
  661. if (res2 < 0) {
  662. tv_usec = 0;
  663. }
  664. tv->tv_usec = tv_usec;
  665. if (raise_exc && (res < 0 || res2 < 0)) {
  666. pytime_time_t_overflow();
  667. return -1;
  668. }
  669. return 0;
  670. }
  671. int
  672. _PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
  673. {
  674. return pytime_as_timeval_struct(t, tv, round, 1);
  675. }
  676. void
  677. _PyTime_AsTimeval_clamp(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
  678. {
  679. (void)pytime_as_timeval_struct(t, tv, round, 0);
  680. }
  681. int
  682. _PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us,
  683. _PyTime_round_t round)
  684. {
  685. _PyTime_t secs;
  686. if (pytime_as_timeval(t, &secs, us, round) < 0) {
  687. pytime_time_t_overflow();
  688. return -1;
  689. }
  690. if (_PyTime_AsTime_t(secs, p_secs) < 0) {
  691. pytime_time_t_overflow();
  692. return -1;
  693. }
  694. return 0;
  695. }
  696. #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
  697. static int
  698. pytime_as_timespec(_PyTime_t t, struct timespec *ts, int raise_exc)
  699. {
  700. _PyTime_t ns = pytime_as_nanoseconds(t);
  701. _PyTime_t tv_sec, tv_nsec;
  702. int res = pytime_divmod(ns, SEC_TO_NS, &tv_sec, &tv_nsec);
  703. int res2 = _PyTime_AsTime_t(tv_sec, &ts->tv_sec);
  704. if (res2 < 0) {
  705. tv_nsec = 0;
  706. }
  707. ts->tv_nsec = tv_nsec;
  708. if (raise_exc && (res < 0 || res2 < 0)) {
  709. pytime_time_t_overflow();
  710. return -1;
  711. }
  712. return 0;
  713. }
  714. void
  715. _PyTime_AsTimespec_clamp(_PyTime_t t, struct timespec *ts)
  716. {
  717. (void)pytime_as_timespec(t, ts, 0);
  718. }
  719. int
  720. _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
  721. {
  722. return pytime_as_timespec(t, ts, 1);
  723. }
  724. #endif
  725. static int
  726. py_get_system_clock(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
  727. {
  728. assert(info == NULL || raise_exc);
  729. #ifdef MS_WINDOWS
  730. FILETIME system_time;
  731. ULARGE_INTEGER large;
  732. GetSystemTimeAsFileTime(&system_time);
  733. large.u.LowPart = system_time.dwLowDateTime;
  734. large.u.HighPart = system_time.dwHighDateTime;
  735. /* 11,644,473,600,000,000,000: number of nanoseconds between
  736. the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
  737. days). */
  738. _PyTime_t ns = large.QuadPart * 100 - 11644473600000000000;
  739. *tp = pytime_from_nanoseconds(ns);
  740. if (info) {
  741. DWORD timeAdjustment, timeIncrement;
  742. BOOL isTimeAdjustmentDisabled, ok;
  743. info->implementation = "GetSystemTimeAsFileTime()";
  744. info->monotonic = 0;
  745. ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
  746. &isTimeAdjustmentDisabled);
  747. if (!ok) {
  748. PyErr_SetFromWindowsErr(0);
  749. return -1;
  750. }
  751. info->resolution = timeIncrement * 1e-7;
  752. info->adjustable = 1;
  753. }
  754. #else /* MS_WINDOWS */
  755. int err;
  756. #if defined(HAVE_CLOCK_GETTIME)
  757. struct timespec ts;
  758. #endif
  759. #if !defined(HAVE_CLOCK_GETTIME) || defined(__APPLE__)
  760. struct timeval tv;
  761. #endif
  762. #ifdef HAVE_CLOCK_GETTIME
  763. #ifdef HAVE_CLOCK_GETTIME_RUNTIME
  764. if (HAVE_CLOCK_GETTIME_RUNTIME) {
  765. #endif
  766. err = clock_gettime(CLOCK_REALTIME, &ts);
  767. if (err) {
  768. if (raise_exc) {
  769. PyErr_SetFromErrno(PyExc_OSError);
  770. }
  771. return -1;
  772. }
  773. if (pytime_fromtimespec(tp, &ts, raise_exc) < 0) {
  774. return -1;
  775. }
  776. if (info) {
  777. struct timespec res;
  778. info->implementation = "clock_gettime(CLOCK_REALTIME)";
  779. info->monotonic = 0;
  780. info->adjustable = 1;
  781. if (clock_getres(CLOCK_REALTIME, &res) == 0) {
  782. info->resolution = (double)res.tv_sec + (double)res.tv_nsec * 1e-9;
  783. }
  784. else {
  785. info->resolution = 1e-9;
  786. }
  787. }
  788. #ifdef HAVE_CLOCK_GETTIME_RUNTIME
  789. }
  790. else {
  791. #endif
  792. #endif
  793. #if !defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_GETTIME_RUNTIME)
  794. /* test gettimeofday() */
  795. err = gettimeofday(&tv, (struct timezone *)NULL);
  796. if (err) {
  797. if (raise_exc) {
  798. PyErr_SetFromErrno(PyExc_OSError);
  799. }
  800. return -1;
  801. }
  802. if (pytime_fromtimeval(tp, &tv, raise_exc) < 0) {
  803. return -1;
  804. }
  805. if (info) {
  806. info->implementation = "gettimeofday()";
  807. info->resolution = 1e-6;
  808. info->monotonic = 0;
  809. info->adjustable = 1;
  810. }
  811. #if defined(HAVE_CLOCK_GETTIME_RUNTIME) && defined(HAVE_CLOCK_GETTIME)
  812. } /* end of availibity block */
  813. #endif
  814. #endif /* !HAVE_CLOCK_GETTIME */
  815. #endif /* !MS_WINDOWS */
  816. return 0;
  817. }
  818. _PyTime_t
  819. _PyTime_GetSystemClock(void)
  820. {
  821. _PyTime_t t;
  822. if (py_get_system_clock(&t, NULL, 0) < 0) {
  823. // If clock_gettime(CLOCK_REALTIME) or gettimeofday() fails:
  824. // silently ignore the failure and return 0.
  825. t = 0;
  826. }
  827. return t;
  828. }
  829. int
  830. _PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
  831. {
  832. return py_get_system_clock(t, info, 1);
  833. }
  834. #ifdef __APPLE__
  835. static int
  836. py_mach_timebase_info(_PyTime_t *pnumer, _PyTime_t *pdenom, int raise)
  837. {
  838. static mach_timebase_info_data_t timebase;
  839. /* According to the Technical Q&A QA1398, mach_timebase_info() cannot
  840. fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
  841. (void)mach_timebase_info(&timebase);
  842. /* Sanity check: should never occur in practice */
  843. if (timebase.numer < 1 || timebase.denom < 1) {
  844. if (raise) {
  845. PyErr_SetString(PyExc_RuntimeError,
  846. "invalid mach_timebase_info");
  847. }
  848. return -1;
  849. }
  850. /* Check that timebase.numer and timebase.denom can be casted to
  851. _PyTime_t. In practice, timebase uses uint32_t, so casting cannot
  852. overflow. At the end, only make sure that the type is uint32_t
  853. (_PyTime_t is 64-bit long). */
  854. static_assert(sizeof(timebase.numer) <= sizeof(_PyTime_t),
  855. "timebase.numer is larger than _PyTime_t");
  856. static_assert(sizeof(timebase.denom) <= sizeof(_PyTime_t),
  857. "timebase.denom is larger than _PyTime_t");
  858. /* Make sure that _PyTime_MulDiv(ticks, timebase_numer, timebase_denom)
  859. cannot overflow.
  860. Known time bases:
  861. * (1, 1) on Intel
  862. * (1000000000, 33333335) or (1000000000, 25000000) on PowerPC
  863. None of these time bases can overflow with 64-bit _PyTime_t, but
  864. check for overflow, just in case. */
  865. if ((_PyTime_t)timebase.numer > _PyTime_MAX / (_PyTime_t)timebase.denom) {
  866. if (raise) {
  867. PyErr_SetString(PyExc_OverflowError,
  868. "mach_timebase_info is too large");
  869. }
  870. return -1;
  871. }
  872. *pnumer = (_PyTime_t)timebase.numer;
  873. *pdenom = (_PyTime_t)timebase.denom;
  874. return 0;
  875. }
  876. #endif
  877. static int
  878. py_get_monotonic_clock(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
  879. {
  880. assert(info == NULL || raise_exc);
  881. #if defined(MS_WINDOWS)
  882. ULONGLONG ticks = GetTickCount64();
  883. static_assert(sizeof(ticks) <= sizeof(_PyTime_t),
  884. "ULONGLONG is larger than _PyTime_t");
  885. _PyTime_t t;
  886. if (ticks <= (ULONGLONG)_PyTime_MAX) {
  887. t = (_PyTime_t)ticks;
  888. }
  889. else {
  890. // GetTickCount64() maximum is larger than _PyTime_t maximum:
  891. // ULONGLONG is unsigned, whereas _PyTime_t is signed.
  892. t = _PyTime_MAX;
  893. }
  894. int res = pytime_mul(&t, MS_TO_NS);
  895. *tp = t;
  896. if (raise_exc && res < 0) {
  897. pytime_overflow();
  898. return -1;
  899. }
  900. if (info) {
  901. DWORD timeAdjustment, timeIncrement;
  902. BOOL isTimeAdjustmentDisabled, ok;
  903. info->implementation = "GetTickCount64()";
  904. info->monotonic = 1;
  905. ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
  906. &isTimeAdjustmentDisabled);
  907. if (!ok) {
  908. PyErr_SetFromWindowsErr(0);
  909. return -1;
  910. }
  911. info->resolution = timeIncrement * 1e-7;
  912. info->adjustable = 0;
  913. }
  914. #elif defined(__APPLE__)
  915. static _PyTime_t timebase_numer = 0;
  916. static _PyTime_t timebase_denom = 0;
  917. if (timebase_denom == 0) {
  918. if (py_mach_timebase_info(&timebase_numer, &timebase_denom, raise_exc) < 0) {
  919. return -1;
  920. }
  921. }
  922. if (info) {
  923. info->implementation = "mach_absolute_time()";
  924. info->resolution = (double)timebase_numer / (double)timebase_denom * 1e-9;
  925. info->monotonic = 1;
  926. info->adjustable = 0;
  927. }
  928. uint64_t uticks = mach_absolute_time();
  929. // unsigned => signed
  930. assert(uticks <= (uint64_t)_PyTime_MAX);
  931. _PyTime_t ticks = (_PyTime_t)uticks;
  932. _PyTime_t ns = _PyTime_MulDiv(ticks, timebase_numer, timebase_denom);
  933. *tp = pytime_from_nanoseconds(ns);
  934. #elif defined(__hpux)
  935. hrtime_t time;
  936. time = gethrtime();
  937. if (time == -1) {
  938. if (raise_exc) {
  939. PyErr_SetFromErrno(PyExc_OSError);
  940. }
  941. return -1;
  942. }
  943. *tp = pytime_from_nanoseconds(time);
  944. if (info) {
  945. info->implementation = "gethrtime()";
  946. info->resolution = 1e-9;
  947. info->monotonic = 1;
  948. info->adjustable = 0;
  949. }
  950. #else
  951. #ifdef CLOCK_HIGHRES
  952. const clockid_t clk_id = CLOCK_HIGHRES;
  953. const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
  954. #else
  955. const clockid_t clk_id = CLOCK_MONOTONIC;
  956. const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
  957. #endif
  958. struct timespec ts;
  959. if (clock_gettime(clk_id, &ts) != 0) {
  960. if (raise_exc) {
  961. PyErr_SetFromErrno(PyExc_OSError);
  962. return -1;
  963. }
  964. return -1;
  965. }
  966. if (pytime_fromtimespec(tp, &ts, raise_exc) < 0) {
  967. return -1;
  968. }
  969. if (info) {
  970. info->monotonic = 1;
  971. info->implementation = implementation;
  972. info->adjustable = 0;
  973. struct timespec res;
  974. if (clock_getres(clk_id, &res) != 0) {
  975. PyErr_SetFromErrno(PyExc_OSError);
  976. return -1;
  977. }
  978. info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
  979. }
  980. #endif
  981. return 0;
  982. }
  983. _PyTime_t
  984. _PyTime_GetMonotonicClock(void)
  985. {
  986. _PyTime_t t;
  987. if (py_get_monotonic_clock(&t, NULL, 0) < 0) {
  988. // If mach_timebase_info(), clock_gettime() or gethrtime() fails:
  989. // silently ignore the failure and return 0.
  990. t = 0;
  991. }
  992. return t;
  993. }
  994. int
  995. _PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
  996. {
  997. return py_get_monotonic_clock(tp, info, 1);
  998. }
  999. #ifdef MS_WINDOWS
  1000. static int
  1001. py_win_perf_counter_frequency(LONGLONG *pfrequency, int raise)
  1002. {
  1003. LONGLONG frequency;
  1004. LARGE_INTEGER freq;
  1005. // Since Windows XP, the function cannot fail.
  1006. (void)QueryPerformanceFrequency(&freq);
  1007. frequency = freq.QuadPart;
  1008. // Since Windows XP, frequency cannot be zero.
  1009. assert(frequency >= 1);
  1010. /* Make also sure that (ticks * SEC_TO_NS) cannot overflow in
  1011. _PyTime_MulDiv(), with ticks < frequency.
  1012. Known QueryPerformanceFrequency() values:
  1013. * 10,000,000 (10 MHz): 100 ns resolution
  1014. * 3,579,545 Hz (3.6 MHz): 279 ns resolution
  1015. None of these frequencies can overflow with 64-bit _PyTime_t, but
  1016. check for integer overflow just in case. */
  1017. if (frequency > _PyTime_MAX / SEC_TO_NS) {
  1018. if (raise) {
  1019. PyErr_SetString(PyExc_OverflowError,
  1020. "QueryPerformanceFrequency is too large");
  1021. }
  1022. return -1;
  1023. }
  1024. *pfrequency = frequency;
  1025. return 0;
  1026. }
  1027. static int
  1028. py_get_win_perf_counter(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
  1029. {
  1030. assert(info == NULL || raise_exc);
  1031. static LONGLONG frequency = 0;
  1032. if (frequency == 0) {
  1033. if (py_win_perf_counter_frequency(&frequency, raise_exc) < 0) {
  1034. return -1;
  1035. }
  1036. }
  1037. if (info) {
  1038. info->implementation = "QueryPerformanceCounter()";
  1039. info->resolution = 1.0 / (double)frequency;
  1040. info->monotonic = 1;
  1041. info->adjustable = 0;
  1042. }
  1043. LARGE_INTEGER now;
  1044. QueryPerformanceCounter(&now);
  1045. LONGLONG ticksll = now.QuadPart;
  1046. /* Make sure that casting LONGLONG to _PyTime_t cannot overflow,
  1047. both types are signed */
  1048. _PyTime_t ticks;
  1049. static_assert(sizeof(ticksll) <= sizeof(ticks),
  1050. "LONGLONG is larger than _PyTime_t");
  1051. ticks = (_PyTime_t)ticksll;
  1052. _PyTime_t ns = _PyTime_MulDiv(ticks, SEC_TO_NS, (_PyTime_t)frequency);
  1053. *tp = pytime_from_nanoseconds(ns);
  1054. return 0;
  1055. }
  1056. #endif // MS_WINDOWS
  1057. int
  1058. _PyTime_GetPerfCounterWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
  1059. {
  1060. #ifdef MS_WINDOWS
  1061. return py_get_win_perf_counter(t, info, 1);
  1062. #else
  1063. return _PyTime_GetMonotonicClockWithInfo(t, info);
  1064. #endif
  1065. }
  1066. _PyTime_t
  1067. _PyTime_GetPerfCounter(void)
  1068. {
  1069. _PyTime_t t;
  1070. int res;
  1071. #ifdef MS_WINDOWS
  1072. res = py_get_win_perf_counter(&t, NULL, 0);
  1073. #else
  1074. res = py_get_monotonic_clock(&t, NULL, 0);
  1075. #endif
  1076. if (res < 0) {
  1077. // If py_win_perf_counter_frequency() or py_get_monotonic_clock()
  1078. // fails: silently ignore the failure and return 0.
  1079. t = 0;
  1080. }
  1081. return t;
  1082. }
  1083. int
  1084. _PyTime_localtime(time_t t, struct tm *tm)
  1085. {
  1086. #ifdef MS_WINDOWS
  1087. int error;
  1088. error = localtime_s(tm, &t);
  1089. if (error != 0) {
  1090. errno = error;
  1091. PyErr_SetFromErrno(PyExc_OSError);
  1092. return -1;
  1093. }
  1094. return 0;
  1095. #else /* !MS_WINDOWS */
  1096. #if defined(_AIX) && (SIZEOF_TIME_T < 8)
  1097. /* bpo-34373: AIX does not return NULL if t is too small or too large */
  1098. if (t < -2145916800 /* 1902-01-01 */
  1099. || t > 2145916800 /* 2038-01-01 */) {
  1100. errno = EINVAL;
  1101. PyErr_SetString(PyExc_OverflowError,
  1102. "localtime argument out of range");
  1103. return -1;
  1104. }
  1105. #endif
  1106. errno = 0;
  1107. if (localtime_r(&t, tm) == NULL) {
  1108. if (errno == 0) {
  1109. errno = EINVAL;
  1110. }
  1111. PyErr_SetFromErrno(PyExc_OSError);
  1112. return -1;
  1113. }
  1114. return 0;
  1115. #endif /* MS_WINDOWS */
  1116. }
  1117. int
  1118. _PyTime_gmtime(time_t t, struct tm *tm)
  1119. {
  1120. #ifdef MS_WINDOWS
  1121. int error;
  1122. error = gmtime_s(tm, &t);
  1123. if (error != 0) {
  1124. errno = error;
  1125. PyErr_SetFromErrno(PyExc_OSError);
  1126. return -1;
  1127. }
  1128. return 0;
  1129. #else /* !MS_WINDOWS */
  1130. if (gmtime_r(&t, tm) == NULL) {
  1131. #ifdef EINVAL
  1132. if (errno == 0) {
  1133. errno = EINVAL;
  1134. }
  1135. #endif
  1136. PyErr_SetFromErrno(PyExc_OSError);
  1137. return -1;
  1138. }
  1139. return 0;
  1140. #endif /* MS_WINDOWS */
  1141. }
  1142. _PyTime_t
  1143. _PyDeadline_Init(_PyTime_t timeout)
  1144. {
  1145. _PyTime_t now = _PyTime_GetMonotonicClock();
  1146. return _PyTime_Add(now, timeout);
  1147. }
  1148. _PyTime_t
  1149. _PyDeadline_Get(_PyTime_t deadline)
  1150. {
  1151. _PyTime_t now = _PyTime_GetMonotonicClock();
  1152. return deadline - now;
  1153. }