GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cpython/Python/pytime.c at main · python/cpython Expand file tree/ Copy pathpytime.cMore file actions 1418 lines (1194 loc) · 33.7 KB / Copy pathpytime.cFile metadata and controls1418 lines (1194 loc) · 33.7 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 #include "Python.h" #include "pycore_initconfig.h" // _PyStatus_ERR #include "pycore_pystate.h" // _Py_AssertHoldsTstate() #include "pycore_runtime.h" // _PyRuntime #include "pycore_time.h" // export _PyLong_FromTime_t() #include <time.h> // gmtime_r() #ifdef HAVE_SYS_TIME_H # include <sys/time.h> // gettimeofday() #endif #ifdef MS_WINDOWS # include <winsock2.h> // struct timeval #endif #if defined(__APPLE__) # include <mach/mach_time.h> // mach_absolute_time(), mach_timebase_info() #if defined(__APPLE__) && defined(__has_builtin) # if __has_builtin(__builtin_available) # define HAVE_CLOCK_GETTIME_RUNTIME __builtin_available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) # endif #endif #endif /* To millisecond (10^-3) */ #define SEC_TO_MS 1000 /* To microseconds (10^-6) */ #define MS_TO_US 1000 #define SEC_TO_US (SEC_TO_MS * MS_TO_US) /* To nanoseconds (10^-9) */ #define US_TO_NS 1000 #define MS_TO_NS (MS_TO_US * US_TO_NS) #define SEC_TO_NS (SEC_TO_MS * MS_TO_NS) /* Conversion from nanoseconds */ #define NS_TO_MS (1000 * 1000) #define NS_TO_US (1000) #define NS_TO_100NS (100) #if SIZEOF_TIME_T == SIZEOF_LONG_LONG # define PY_TIME_T_MAX LLONG_MAX # define PY_TIME_T_MIN LLONG_MIN #elif SIZEOF_TIME_T == SIZEOF_LONG # define PY_TIME_T_MAX LONG_MAX # define PY_TIME_T_MIN LONG_MIN #else # error "unsupported time_t size" #endif #if PY_TIME_T_MAX + PY_TIME_T_MIN != -1 # error "time_t is not a two's complement integer type" #endif #if PyTime_MIN + PyTime_MAX != -1 # error "PyTime_t is not a two's complement integer type" #endif static PyTime_t _PyTime_GCD(PyTime_t x, PyTime_t y) { // Euclidean algorithm assert(x >= 1); assert(y >= 1); while (y != 0) { PyTime_t tmp = y; y = x % y; x = tmp; } assert(x >= 1); return x; } int _PyTimeFraction_Set(_PyTimeFraction *frac, PyTime_t numer, PyTime_t denom) { if (numer < 1 || denom < 1) { return -1; } PyTime_t gcd = _PyTime_GCD(numer, denom); frac->numer = numer / gcd; frac->denom = denom / gcd; return 0; } double _PyTimeFraction_Resolution(const _PyTimeFraction *frac) { return (double)frac->numer / (double)frac->denom / 1e9; } static void pytime_time_t_overflow(void) { PyErr_SetString(PyExc_OverflowError, "timestamp out of range for platform time_t"); } static void pytime_overflow(void) { PyErr_SetString(PyExc_OverflowError, "timestamp out of range for C PyTime_t"); } // Compute t1 + t2. Clamp to [PyTime_MIN; PyTime_MAX] on overflow. static inline int pytime_add(PyTime_t *t1, PyTime_t t2) { if (t2 > 0 && *t1 > PyTime_MAX - t2) { *t1 = PyTime_MAX; return -1; } else if (t2 < 0 && *t1 < PyTime_MIN - t2) { *t1 = PyTime_MIN; return -1; } else { *t1 += t2; return 0; } } PyTime_t _PyTime_Add(PyTime_t t1, PyTime_t t2) { (void)pytime_add(&t1, t2); return t1; } static inline int pytime_mul_check_overflow(PyTime_t a, PyTime_t b) { if (b != 0) { assert(b > 0); return ((a < PyTime_MIN / b) || (PyTime_MAX / b < a)); } else { return 0; } } // Compute t * k. Clamp to [PyTime_MIN; PyTime_MAX] on overflow. static inline int pytime_mul(PyTime_t *t, PyTime_t k) { assert(k >= 0); if (pytime_mul_check_overflow(*t, k)) { *t = (*t >= 0) ? PyTime_MAX : PyTime_MIN; return -1; } else { *t *= k; return 0; } } // Compute t * k. Clamp to [PyTime_MIN; PyTime_MAX] on overflow. static inline PyTime_t _PyTime_Mul(PyTime_t t, PyTime_t k) { (void)pytime_mul(&t, k); return t; } PyTime_t _PyTimeFraction_Mul(PyTime_t ticks, const _PyTimeFraction *frac) { const PyTime_t mul = frac->numer; const PyTime_t div = frac->denom; if (div == 1) { // Fast-path taken by mach_absolute_time() with 1/1 time base. return _PyTime_Mul(ticks, mul); } /* Compute (ticks * mul / div) in two parts to reduce the risk of integer overflow: compute the integer part, and then the remaining part. (ticks * mul) / div == (ticks / div) * mul + (ticks % div) * mul / div */ PyTime_t intpart, remaining; intpart = ticks / div; ticks %= div; remaining = _PyTime_Mul(ticks, mul) / div; // intpart * mul + remaining return _PyTime_Add(_PyTime_Mul(intpart, mul), remaining); } time_t _PyLong_AsTime_t(PyObject *obj) { #if SIZEOF_TIME_T == SIZEOF_LONG_LONG long long val = PyLong_AsLongLong(obj); #elif SIZEOF_TIME_T <= SIZEOF_LONG long val = PyLong_AsLong(obj); #else # error "unsupported time_t size" #endif if (val == -1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { pytime_time_t_overflow(); } return -1; } return (time_t)val; } PyObject * _PyLong_FromTime_t(time_t t) { #if SIZEOF_TIME_T == SIZEOF_LONG_LONG return PyLong_FromLongLong((long long)t); #elif SIZEOF_TIME_T <= SIZEOF_LONG return PyLong_FromLong((long)t); #else # error "unsupported time_t size" #endif } // Convert PyTime_t to time_t. // Return 0 on success. Return -1 and clamp the value on overflow. static int _PyTime_AsTime_t(PyTime_t t, time_t *t2) { #if SIZEOF_TIME_T < _SIZEOF_PYTIME_T if ((PyTime_t)PY_TIME_T_MAX < t) { *t2 = PY_TIME_T_MAX; return -1; } if (t < (PyTime_t)PY_TIME_T_MIN) { *t2 = PY_TIME_T_MIN; return -1; } #endif *t2 = (time_t)t; return 0; } #ifdef MS_WINDOWS // Convert PyTime_t to long. // Return 0 on success. Return -1 and clamp the value on overflow. static int _PyTime_AsCLong(PyTime_t t, long *t2) { #if SIZEOF_LONG < _SIZEOF_PYTIME_T if ((PyTime_t)LONG_MAX < t) { *t2 = LONG_MAX; return -1; } if (t < (PyTime_t)LONG_MIN) { *t2 = LONG_MIN; return -1; } #endif *t2 = (long)t; return 0; } // Seconds between 1601-01-01 and 1970-01-01: // 369 years + 89 leap days. #define SECS_BETWEEN_EPOCHS 11644473600LL #define HUNDRED_NS_PER_SEC 10000000LL // Calculate day of year (0-365) from SYSTEMTIME static int _PyTime_calc_yday(const SYSTEMTIME *st) { // Cumulative days before each month (non-leap year) static const int days_before_month[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; int yday = days_before_month[st->wMonth - 1] + st->wDay - 1; // Account for leap day if we're past February in a leap year. if (st->wMonth > 2) { // Leap year rules (Gregorian calendar): // - Years divisible by 4 are leap years // - EXCEPT years divisible by 100 are NOT leap years // - EXCEPT years divisible by 400 ARE leap years int year = st->wYear; int is_leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); yday += is_leap; } return yday; } // Convert time_t to struct tm using Windows FILETIME API. // If is_local is true, convert to local time. // Fallback for negative timestamps that localtime_s/gmtime_s cannot handle. // Return 0 on success. Return -1 on error. static int _PyTime_windows_filetime(time_t timer, struct tm *tm, int is_local) { /* Check for underflow - FILETIME epoch is 1601-01-01 */ if (timer < -SECS_BETWEEN_EPOCHS) { PyErr_SetString(PyExc_OverflowError, "timestamp out of range for Windows FILETIME"); return -1; } /* Convert time_t to FILETIME (100-nanosecond intervals since 1601-01-01) */ ULONGLONG ticks = ((ULONGLONG)timer + SECS_BETWEEN_EPOCHS) * HUNDRED_NS_PER_SEC; FILETIME ft; ft.dwLowDateTime = (DWORD)(ticks); // cast to DWORD truncates to low 32 bits ft.dwHighDateTime = (DWORD)(ticks >> 32); /* Convert FILETIME to SYSTEMTIME (UTC) */ SYSTEMTIME st_utc; if (!FileTimeToSystemTime(&ft, &st_utc)) { PyErr_SetFromWindowsErr(0); return -1; } SYSTEMTIME st_result; if (is_local) { /* Convert UTC SYSTEMTIME to local SYSTEMTIME. * We use SystemTimeToTzSpecificLocalTime instead of * FileTimeToLocalFileTime because the latter always applies the * _current_ DST bias, whereas the former applies the correct * DST rules for the date being converted (gh-80620). */ if (!SystemTimeToTzSpecificLocalTime(NULL, &st_utc, &st_result)) { PyErr_SetFromWindowsErr(0); return -1; } } else { st_result = st_utc; } /* Convert SYSTEMTIME to struct tm */ tm->tm_year = st_result.wYear - 1900; tm->tm_mon = st_result.wMonth - 1; /* SYSTEMTIME: 1-12, tm: 0-11 */ tm->tm_mday = st_result.wDay; tm->tm_hour = st_result.wHour; tm->tm_min = st_result.wMinute; tm->tm_sec = st_result.wSecond; tm->tm_wday = st_result.wDayOfWeek; /* 0=Sunday */ // `time.gmtime` and `time.localtime` will return `struct_time` containing this tm->tm_yday = _PyTime_calc_yday(&st_result); /* DST flag: -1 (unknown) for local time on historical dates, 0 for UTC */ tm->tm_isdst = is_local ? -1 : 0; return 0; } #endif /* Round to nearest with ties going to nearest even integer (_PyTime_ROUND_HALF_EVEN) */ static double pytime_round_half_even(double x) { double rounded = round(x); if (fabs(x-rounded) == 0.5) { /* halfway case: round to even */ rounded = 2.0 * round(x / 2.0); } return rounded; } static double pytime_round(double x, _PyTime_round_t round) { /* volatile avoids optimization changing how numbers are rounded */ volatile double d; d = x; if (round == _PyTime_ROUND_HALF_EVEN) { d = pytime_round_half_even(d); } else if (round == _PyTime_ROUND_CEILING) { d = ceil(d); } else if (round == _PyTime_ROUND_FLOOR) { d = floor(d); } else { assert(round == _PyTime_ROUND_UP); d = (d >= 0.0) ? ceil(d) : floor(d); } return d; } static int pytime_double_to_denominator(double d, time_t *sec, long *numerator, long idenominator, _PyTime_round_t round) { double denominator = (double)idenominator; double intpart; /* volatile avoids optimization changing how numbers are rounded */ volatile double floatpart; floatpart = modf(d, &intpart); floatpart *= denominator; floatpart = pytime_round(floatpart, round); if (floatpart >= denominator) { floatpart -= denominator; intpart += 1.0; } else if (floatpart < 0) { floatpart += denominator; intpart -= 1.0; } assert(0.0 <= floatpart && floatpart < denominator); /* Conversion of an out-of-range value to time_t gives undefined behaviour (C99 §6.3.1.4p1), so we must guard against it. However, checking that `intpart` is in range is delicate: the obvious expression `intpart <= PY_TIME_T_MAX` will first convert the value `PY_TIME_T_MAX` to a double, potentially changing its value and leading to us failing to catch some UB-inducing values. The code below works correctly under the mild assumption that time_t is a two's complement integer type with no trap representation, and that `PY_TIME_T_MIN` is within the representable range of a C double. Note: we want the `if` condition below to be true for NaNs; therefore, resist any temptation to simplify by applying De Morgan's laws. */ if (!((double)PY_TIME_T_MIN <= intpart && intpart < -(double)PY_TIME_T_MIN)) { pytime_time_t_overflow(); return -1; } *sec = (time_t)intpart; *numerator = (long)floatpart; assert(0 <= *numerator && *numerator < idenominator); return 0; } static int pytime_object_to_denominator(PyObject *obj, time_t *sec, long *numerator, long denominator, _PyTime_round_t round) { assert(denominator >= 1); if (PyIndex_Check(obj)) { *sec = _PyLong_AsTime_t(obj); *numerator = 0; if (*sec == (time_t)-1 && PyErr_Occurred()) { return -1; } return 0; } else { double d = PyFloat_AsDouble(obj); if (d == -1 && PyErr_Occurred()) { *numerator = 0; return -1; } if (isnan(d)) { *numerator = 0; PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)"); return -1; } return pytime_double_to_denominator(d, sec, numerator, denominator, round); } } int _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round) { if (PyIndex_Check(obj)) { *sec = _PyLong_AsTime_t(obj); if (*sec == (time_t)-1 && PyErr_Occurred()) { return -1; } return 0; } else { double intpart; /* volatile avoids optimization changing how numbers are rounded */ volatile double d; d = PyFloat_AsDouble(obj); if (d == -1 && PyErr_Occurred()) { return -1; } if (isnan(d)) { PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)"); return -1; } d = pytime_round(d, round); (void)modf(d, &intpart); /* See comments in pytime_double_to_denominator */ if (!((double)PY_TIME_T_MIN <= intpart && intpart < -(double)PY_TIME_T_MIN)) { pytime_time_t_overflow(); return -1; } *sec = (time_t)intpart; return 0; } } int _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec, _PyTime_round_t round) { return pytime_object_to_denominator(obj, sec, nsec, SEC_TO_NS, round); } int _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec, _PyTime_round_t round) { return pytime_object_to_denominator(obj, sec, usec, SEC_TO_US, round); } PyTime_t _PyTime_FromSeconds(int seconds) { /* ensure that integer overflow cannot happen, int type should have 32 bits, whereas PyTime_t type has at least 64 bits (SEC_TO_NS takes 30 bits). */ static_assert(INT_MAX <= PyTime_MAX / SEC_TO_NS, "PyTime_t overflow"); static_assert(INT_MIN >= PyTime_MIN / SEC_TO_NS, "PyTime_t underflow"); PyTime_t t = (PyTime_t)seconds; assert((t >= 0 && t <= PyTime_MAX / SEC_TO_NS) || (t < 0 && t >= PyTime_MIN / SEC_TO_NS)); t *= SEC_TO_NS; return t; } PyTime_t _PyTime_FromMicrosecondsClamp(PyTime_t us) { PyTime_t ns = _PyTime_Mul(us, US_TO_NS); return ns; } #ifdef HAVE_CLOCK_GETTIME static int pytime_fromtimespec(PyTime_t *tp, const struct timespec *ts, int raise_exc) { PyTime_t t, tv_nsec; static_assert(sizeof(ts->tv_sec) <= sizeof(PyTime_t), "timespec.tv_sec is larger than PyTime_t"); t = (PyTime_t)ts->tv_sec; int res1 = pytime_mul(&t, SEC_TO_NS); tv_nsec = ts->tv_nsec; int res2 = pytime_add(&t, tv_nsec); *tp = t; if (raise_exc && (res1 < 0 || res2 < 0)) { pytime_overflow(); return -1; } return 0; } int _PyTime_FromTimespec(PyTime_t *tp, const struct timespec *ts) { return pytime_fromtimespec(tp, ts, 1); } #endif #ifndef MS_WINDOWS static int pytime_fromtimeval(PyTime_t *tp, struct timeval *tv, int raise_exc) { static_assert(sizeof(tv->tv_sec) <= sizeof(PyTime_t), "timeval.tv_sec is larger than PyTime_t"); PyTime_t t = (PyTime_t)tv->tv_sec; int res1 = pytime_mul(&t, SEC_TO_NS); PyTime_t usec = (PyTime_t)tv->tv_usec * US_TO_NS; int res2 = pytime_add(&t, usec); *tp = t; if (raise_exc && (res1 < 0 || res2 < 0)) { pytime_overflow(); return -1; } return 0; } int _PyTime_FromTimeval(PyTime_t *tp, struct timeval *tv) { return pytime_fromtimeval(tp, tv, 1); } #endif static int pytime_from_double(PyTime_t *tp, double value, _PyTime_round_t round, long unit_to_ns) { /* volatile avoids optimization changing how numbers are rounded */ volatile double d; /* convert to a number of nanoseconds */ d = value; d *= (double)unit_to_ns; d = pytime_round(d, round); /* See comments in pytime_double_to_denominator */ if (!((double)PyTime_MIN <= d && d < -(double)PyTime_MIN)) { pytime_overflow(); *tp = 0; return -1; } PyTime_t ns = (PyTime_t)d; *tp = ns; return 0; } static int pytime_from_object(PyTime_t *tp, PyObject *obj, _PyTime_round_t round, long unit_to_ns) { if (PyIndex_Check(obj)) { long long sec = PyLong_AsLongLong(obj); if (sec == -1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { pytime_overflow(); } return -1; } static_assert(sizeof(long long) <= sizeof(PyTime_t), "PyTime_t is smaller than long long"); PyTime_t ns = (PyTime_t)sec; if (pytime_mul(&ns, unit_to_ns) < 0) { pytime_overflow(); return -1; } *tp = ns; return 0; } else { double d; d = PyFloat_AsDouble(obj); if (d == -1 && PyErr_Occurred()) { return -1; } if (isnan(d)) { PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)"); return -1; } return pytime_from_double(tp, d, round, unit_to_ns); } } int _PyTime_FromSecondsObject(PyTime_t *tp, PyObject *obj, _PyTime_round_t round) { return pytime_from_object(tp, obj, round, SEC_TO_NS); } int _PyTime_FromMillisecondsObject(PyTime_t *tp, PyObject *obj, _PyTime_round_t round) { return pytime_from_object(tp, obj, round, MS_TO_NS); } double PyTime_AsSecondsDouble(PyTime_t ns) { /* volatile avoids optimization changing how numbers are rounded */ volatile double d; if (ns % SEC_TO_NS == 0) { /* Divide using integers to avoid rounding issues on the integer part. 1e-9 cannot be stored exactly in IEEE 64-bit. */ PyTime_t secs = ns / SEC_TO_NS; d = (double)secs; } else { d = (double)ns; d /= 1e9; } return d; } int _PyTime_FromSecondsDouble(double seconds, _PyTime_round_t round, PyTime_t *result) { return pytime_from_double(result, seconds, round, SEC_TO_NS); } static PyTime_t pytime_divide_round_up(const PyTime_t t, const PyTime_t k) { assert(k > 1); if (t >= 0) { // Don't use (t + k - 1) / k to avoid integer overflow // if t is equal to PyTime_MAX PyTime_t q = t / k; if (t % k) { q += 1; } return q; } else { // Don't use (t - (k - 1)) / k to avoid integer overflow // if t is equals to PyTime_MIN. PyTime_t q = t / k; if (t % k) { q -= 1; } return q; } } static PyTime_t pytime_divide(const PyTime_t t, const PyTime_t k, const _PyTime_round_t round) { assert(k > 1); if (round == _PyTime_ROUND_HALF_EVEN) { PyTime_t x = t / k; PyTime_t r = t % k; PyTime_t abs_r = Py_ABS(r); if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) { if (t >= 0) { x++; } else { x--; } } return x; } else if (round == _PyTime_ROUND_CEILING) { if (t >= 0) { return pytime_divide_round_up(t, k); } else { return t / k; } } else if (round == _PyTime_ROUND_FLOOR){ if (t >= 0) { return t / k; } else { return pytime_divide_round_up(t, k); } } else { assert(round == _PyTime_ROUND_UP); return pytime_divide_round_up(t, k); } } // Compute (t / k, t % k) in (pq, pr). // Make sure that 0 <= pr < k. // Return 0 on success. // Return -1 on underflow and store (PyTime_MIN, 0) in (pq, pr). static int pytime_divmod(const PyTime_t t, const PyTime_t k, PyTime_t *pq, PyTime_t *pr) { assert(k > 1); PyTime_t q = t / k; PyTime_t r = t % k; if (r < 0) { if (q == PyTime_MIN) { *pq = PyTime_MIN; *pr = 0; return -1; } r += k; q -= 1; } assert(0 <= r && r < k); *pq = q; *pr = r; return 0; } #ifdef MS_WINDOWS PyTime_t _PyTime_As100Nanoseconds(PyTime_t ns, _PyTime_round_t round) { return pytime_divide(ns, NS_TO_100NS, round); } #endif PyTime_t _PyTime_AsMicroseconds(PyTime_t ns, _PyTime_round_t round) { return pytime_divide(ns, NS_TO_US, round); } PyTime_t _PyTime_AsMilliseconds(PyTime_t ns, _PyTime_round_t round) { return pytime_divide(ns, NS_TO_MS, round); } static int pytime_as_timeval(PyTime_t ns, PyTime_t *ptv_sec, int *ptv_usec, _PyTime_round_t round) { PyTime_t us = pytime_divide(ns, US_TO_NS, round); PyTime_t tv_sec, tv_usec; int res = pytime_divmod(us, SEC_TO_US, &tv_sec, &tv_usec); *ptv_sec = tv_sec; *ptv_usec = (int)tv_usec; return res; } static int pytime_as_timeval_struct(PyTime_t t, struct timeval *tv, _PyTime_round_t round, int raise_exc) { PyTime_t tv_sec; int tv_usec; int res = pytime_as_timeval(t, &tv_sec, &tv_usec, round); int res2; #ifdef MS_WINDOWS // On Windows, timeval.tv_sec type is long res2 = _PyTime_AsCLong(tv_sec, &tv->tv_sec); #else res2 = _PyTime_AsTime_t(tv_sec, &tv->tv_sec); #endif if (res2 < 0) { tv_usec = 0; } tv->tv_usec = tv_usec; if (raise_exc && (res < 0 || res2 < 0)) { pytime_time_t_overflow(); return -1; } return 0; } int _PyTime_AsTimeval(PyTime_t t, struct timeval *tv, _PyTime_round_t round) { return pytime_as_timeval_struct(t, tv, round, 1); } void _PyTime_AsTimeval_clamp(PyTime_t t, struct timeval *tv, _PyTime_round_t round) { (void)pytime_as_timeval_struct(t, tv, round, 0); } int _PyTime_AsTimevalTime_t(PyTime_t t, time_t *p_secs, int *us, _PyTime_round_t round) { PyTime_t secs; if (pytime_as_timeval(t, &secs, us, round) < 0) { pytime_time_t_overflow(); return -1; } if (_PyTime_AsTime_t(secs, p_secs) < 0) { pytime_time_t_overflow(); return -1; } return 0; } #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE) static int pytime_as_timespec(PyTime_t ns, struct timespec *ts, int raise_exc) { PyTime_t tv_sec, tv_nsec; int res = pytime_divmod(ns, SEC_TO_NS, &tv_sec, &tv_nsec); int res2 = _PyTime_AsTime_t(tv_sec, &ts->tv_sec); if (res2 < 0) { tv_nsec = 0; } ts->tv_nsec = tv_nsec; if (raise_exc && (res < 0 || res2 < 0)) { pytime_time_t_overflow(); return -1; } return 0; } void _PyTime_AsTimespec_clamp(PyTime_t t, struct timespec *ts) { (void)pytime_as_timespec(t, ts, 0); } int _PyTime_AsTimespec(PyTime_t t, struct timespec *ts) { return pytime_as_timespec(t, ts, 1); } #endif // N.B. If raise_exc=0, this may be called without a thread state. static int py_get_system_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc) { assert(info == NULL || raise_exc); if (raise_exc) { // raise_exc requires to hold a thread state _Py_AssertHoldsTstate(); } #ifdef MS_WINDOWS FILETIME system_time; ULARGE_INTEGER large; GetSystemTimePreciseAsFileTime(&system_time); large.u.LowPart = system_time.dwLowDateTime; large.u.HighPart = system_time.dwHighDateTime; PyTime_t ns = (large.QuadPart - SECS_BETWEEN_EPOCHS * HUNDRED_NS_PER_SEC) * 100; *tp = ns; if (info) { // GetSystemTimePreciseAsFileTime() is implemented using // QueryPerformanceCounter() internally. info->implementation = "GetSystemTimePreciseAsFileTime()"; info->monotonic = 0; info->resolution = _PyTimeFraction_Resolution(&_PyRuntime.time.base); info->adjustable = 1; } #else /* MS_WINDOWS */ int err; #if defined(HAVE_CLOCK_GETTIME) struct timespec ts; #endif #if !defined(HAVE_CLOCK_GETTIME) || defined(__APPLE__) struct timeval tv; #endif #ifdef HAVE_CLOCK_GETTIME #ifdef HAVE_CLOCK_GETTIME_RUNTIME if (HAVE_CLOCK_GETTIME_RUNTIME) { #endif err = clock_gettime(CLOCK_REALTIME, &ts); |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |