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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
|
#include "Python.h"
#include "pycore_initconfig.h" // _PyStatus_ERR
#include "pycore_pystate.h" // _Py_AssertHoldsTstate()
#include "pycore_runtime.h" // _PyRuntime
#include "pycore_time.h" // PyTime_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 too large to convert to 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;
}
#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 (PyFloat_Check(obj)) {
double d = PyFloat_AsDouble(obj);
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);
}
else {
*sec = _PyLong_AsTime_t(obj);
*numerator = 0;
if (*sec == (time_t)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
PyErr_Format(PyExc_TypeError,
"argument must be int or float, not %T", obj);
}
return -1;
}
return 0;
}
}
int
_PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
{
if (PyFloat_Check(obj)) {
double intpart;
/* volatile avoids optimization changing how numbers are rounded */
volatile double d;
d = PyFloat_AsDouble(obj);
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;
}
else {
*sec = _PyLong_AsTime_t(obj);
if (*sec == (time_t)-1 && PyErr_Occurred()) {
return -1;
}
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;
}
int
_PyTime_FromLong(PyTime_t *tp, PyObject *obj)
{
if (!PyLong_Check(obj)) {
PyErr_Format(PyExc_TypeError, "expect int, got %s",
Py_TYPE(obj)->tp_name);
return -1;
}
static_assert(sizeof(long long) == sizeof(PyTime_t),
"PyTime_t is not long long");
long long nsec = PyLong_AsLongLong(obj);
if (nsec == -1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
pytime_overflow();
}
return -1;
}
PyTime_t t = (PyTime_t)nsec;
*tp = t;
return 0;
}
#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_time_t_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 (PyFloat_Check(obj)) {
double d;
d = PyFloat_AsDouble(obj);
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);
}
long long sec = PyLong_AsLongLong(obj);
if (sec == -1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
pytime_overflow();
}
else if (PyErr_ExceptionMatches(PyExc_TypeError)) {
PyErr_Format(PyExc_TypeError,
"'%T' object cannot be interpreted as an integer or float",
obj);
}
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;
}
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;
}
PyObject *
_PyTime_AsLong(PyTime_t ns)
{
static_assert(sizeof(long long) >= sizeof(PyTime_t),
"PyTime_t is larger than long long");
return PyLong_FromLongLong((long long)ns);
}
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;
/* 11,644,473,600,000,000,000: number of nanoseconds between
the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
days). */
PyTime_t ns = (large.QuadPart - 116444736000000000) * 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);
if (err) {
if (raise_exc) {
PyErr_SetFromErrno(PyExc_OSError);
}
return -1;
}
if (pytime_fromtimespec(tp, &ts, raise_exc) < 0) {
return -1;
}
if (info) {
struct timespec res;
info->implementation = "clock_gettime(CLOCK_REALTIME)";
info->monotonic = 0;
info->adjustable = 1;
if (clock_getres(CLOCK_REALTIME, &res) == 0) {
info->resolution = (double)res.tv_sec + (double)res.tv_nsec * 1e-9;
}
else {
info->resolution = 1e-9;
}
}
#ifdef HAVE_CLOCK_GETTIME_RUNTIME
}
else {
#endif
#endif
#if !defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_GETTIME_RUNTIME)
/* test gettimeofday() */
err = gettimeofday(&tv, (struct timezone *)NULL);
if (err) {
if (raise_exc) {
PyErr_SetFromErrno(PyExc_OSError);
}
return -1;
}
if (pytime_fromtimeval(tp, &tv, raise_exc) < 0) {
return -1;
}
if (info) {
info->implementation = "gettimeofday()";
info->resolution = 1e-6;
info->monotonic = 0;
info->adjustable = 1;
}
#if defined(HAVE_CLOCK_GETTIME_RUNTIME) && defined(HAVE_CLOCK_GETTIME)
} /* end of availability block */
#endif
#endif /* !HAVE_CLOCK_GETTIME */
#endif /* !MS_WINDOWS */
return 0;
}
int
PyTime_Time(PyTime_t *result)
{
if (py_get_system_clock(result, NULL, 1) < 0) {
*result = 0;
return -1;
}
return 0;
}
int
PyTime_TimeRaw(PyTime_t *result)
{
if (py_get_system_clock(result, NULL, 0) < 0) {
*result = 0;
return -1;
}
return 0;
}
int
_PyTime_TimeWithInfo(PyTime_t *t, _Py_clock_info_t *info)
{
return py_get_system_clock(t, info, 1);
}
#ifdef MS_WINDOWS
static PyStatus
py_win_perf_counter_frequency(_PyTimeFraction *base)
{
LARGE_INTEGER freq;
// Since Windows XP, the function cannot fail.
(void)QueryPerformanceFrequency(&freq);
LONGLONG frequency = freq.QuadPart;
// Since Windows XP, frequency cannot be zero.
assert(frequency >= 1);
Py_BUILD_ASSERT(sizeof(PyTime_t) == sizeof(frequency));
PyTime_t denom = (PyTime_t)frequency;
// Known QueryPerformanceFrequency() values:
//
// * 10,000,000 (10 MHz): 100 ns resolution
// * 3,579,545 Hz (3.6 MHz): 279 ns resolution
if (_PyTimeFraction_Set(base, SEC_TO_NS, denom) < 0) {
return _PyStatus_ERR("invalid QueryPerformanceFrequency");
}
return PyStatus_Ok();
}
// N.B. If raise_exc=0, this may be called without the GIL.
static int
py_get_win_perf_counter(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
{
assert(info == NULL || raise_exc);
if (info) {
info->implementation = "QueryPerformanceCounter()";
info->resolution = _PyTimeFraction_Resolution(&_PyRuntime.time.base);
info->monotonic = 1;
info->adjustable = 0;
}
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
LONGLONG ticksll = now.QuadPart;
/* Make sure that casting LONGLONG to PyTime_t cannot overflow,
both types are signed */
PyTime_t ticks;
static_assert(sizeof(ticksll) <= sizeof(ticks),
"LONGLONG is larger than PyTime_t");
ticks = (PyTime_t)ticksll;
*tp = _PyTimeFraction_Mul(ticks, &_PyRuntime.time.base);
return 0;
}
#endif // MS_WINDOWS
#ifdef __APPLE__
static PyStatus
py_mach_timebase_info(_PyTimeFraction *base)
{
mach_timebase_info_data_t timebase;
// According to the Technical Q&A QA1398, mach_timebase_info() cannot
// fail: https://developer.apple.com/library/mac/#qa/qa1398/
(void)mach_timebase_info(&timebase);
// Check that timebase.numer and timebase.denom can be casted to
// PyTime_t. In practice, timebase uses uint32_t, so casting cannot
// overflow. At the end, only make sure that the type is uint32_t
// (PyTime_t is 64-bit long).
Py_BUILD_ASSERT(sizeof(timebase.numer) <= sizeof(PyTime_t));
Py_BUILD_ASSERT(sizeof(timebase.denom) <= sizeof(PyTime_t));
PyTime_t numer = (PyTime_t)timebase.numer;
PyTime_t denom = (PyTime_t)timebase.denom;
// Known time bases:
//
// * (1, 1) on Intel: 1 ns
// * (1000000000, 33333335) on PowerPC: ~30 ns
// * (1000000000, 25000000) on PowerPC: 40 ns
if (_PyTimeFraction_Set(base, numer, denom) < 0) {
return _PyStatus_ERR("invalid mach_timebase_info");
}
return PyStatus_Ok();
}
#endif
PyStatus
_PyTime_Init(struct _Py_time_runtime_state *state)
{
#ifdef MS_WINDOWS
return py_win_perf_counter_frequency(&state->base);
#elif defined(__APPLE__)
return py_mach_timebase_info(&state->base);
#else
return PyStatus_Ok();
#endif
}
// N.B. If raise_exc=0, this may be called without a thread state.
static int
py_get_monotonic_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();
}
#if defined(MS_WINDOWS)
if (py_get_win_perf_counter(tp, info, raise_exc) < 0) {
return -1;
}
#elif defined(__APPLE__)
if (info) {
info->implementation = "mach_absolute_time()";
info->resolution = _PyTimeFraction_Resolution(&_PyRuntime.time.base);
info->monotonic = 1;
info->adjustable = 0;
}
uint64_t uticks = mach_absolute_time();
// unsigned => signed
assert(uticks <= (uint64_t)PyTime_MAX);
PyTime_t ticks = (PyTime_t)uticks;
PyTime_t ns = _PyTimeFraction_Mul(ticks, &_PyRuntime.time.base);
*tp = ns;
#elif defined(__hpux)
hrtime_t time = gethrtime();
if (time == -1) {
if (raise_exc) {
PyErr_SetFromErrno(PyExc_OSError);
}
return -1;
}
*tp = time;
if (info) {
info->implementation = "gethrtime()";
info->resolution = 1e-9;
info->monotonic = 1;
info->adjustable = 0;
}
#else
#ifdef CLOCK_HIGHRES
const clockid_t clk_id = CLOCK_HIGHRES;
const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
#else
const clockid_t clk_id = CLOCK_MONOTONIC;
const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
#endif
struct timespec ts;
if (clock_gettime(clk_id, &ts) != 0) {
if (raise_exc) {
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
return -1;
}
if (pytime_fromtimespec(tp, &ts, raise_exc) < 0) {
return -1;
}
if (info) {
info->monotonic = 1;
info->implementation = implementation;
info->adjustable = 0;
struct timespec res;
if (clock_getres(clk_id, &res) != 0) {
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
}
#endif
return 0;
}
int
PyTime_Monotonic(PyTime_t *result)
{
if (py_get_monotonic_clock(result, NULL, 1) < 0) {
*result = 0;
return -1;
}
return 0;
}
int
PyTime_MonotonicRaw(PyTime_t *result)
{
if (py_get_monotonic_clock(result, NULL, 0) < 0) {
*result = 0;
return -1;
}
return 0;
}
int
_PyTime_MonotonicWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
{
return py_get_monotonic_clock(tp, info, 1);
}
int
_PyTime_PerfCounterWithInfo(PyTime_t *t, _Py_clock_info_t *info)
{
return _PyTime_MonotonicWithInfo(t, info);
}
int
PyTime_PerfCounter(PyTime_t *result)
{
return PyTime_Monotonic(result);
}
int
PyTime_PerfCounterRaw(PyTime_t *result)
{
return PyTime_MonotonicRaw(result);
}
int
_PyTime_localtime(time_t t, struct tm *tm)
{
#ifdef MS_WINDOWS
int error;
error = localtime_s(tm, &t);
if (error != 0) {
errno = error;
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
return 0;
#else /* !MS_WINDOWS */
#if defined(_AIX) && (SIZEOF_TIME_T < 8)
/* bpo-34373: AIX does not return NULL if t is too small or too large */
if (t < -2145916800 /* 1902-01-01 */
|| t > 2145916800 /* 2038-01-01 */) {
errno = EINVAL;
PyErr_SetString(PyExc_OverflowError,
"localtime argument out of range");
return -1;
}
#endif
errno = 0;
if (localtime_r(&t, tm) == NULL) {
if (errno == 0) {
errno = EINVAL;
}
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
return 0;
#endif /* MS_WINDOWS */
}
int
_PyTime_gmtime(time_t t, struct tm *tm)
{
#ifdef MS_WINDOWS
int error;
error = gmtime_s(tm, &t);
if (error != 0) {
errno = error;
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
return 0;
#else /* !MS_WINDOWS */
if (gmtime_r(&t, tm) == NULL) {
#ifdef EINVAL
if (errno == 0) {
errno = EINVAL;
}
#endif
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
return 0;
#endif /* MS_WINDOWS */
}
PyTime_t
_PyDeadline_Init(PyTime_t timeout)
{
PyTime_t now;
// silently ignore error: cannot report error to the caller
(void)PyTime_MonotonicRaw(&now);
return _PyTime_Add(now, timeout);
}
PyTime_t
_PyDeadline_Get(PyTime_t deadline)
{
PyTime_t now;
// silently ignore error: cannot report error to the caller
(void)PyTime_MonotonicRaw(&now);
return deadline - now;
}
|