* Applies a single business day count operation. See the function * business_day_count for the meaning of all the parameters. * * Returns 0 on success, -1 on failure. */
| 359 | * Returns 0 on success, -1 on failure. |
| 360 | */ |
| 361 | static int |
| 362 | apply_business_day_count(npy_datetime date_begin, npy_datetime date_end, |
| 363 | npy_int64 *out, |
| 364 | const npy_bool *weekmask, int busdays_in_weekmask, |
| 365 | npy_datetime *holidays_begin, npy_datetime *holidays_end) |
| 366 | { |
| 367 | npy_int64 count, whole_weeks; |
| 368 | |
| 369 | int day_of_week = 0; |
| 370 | int swapped = 0; |
| 371 | |
| 372 | /* If we get a NaT, raise an error */ |
| 373 | if (date_begin == NPY_DATETIME_NAT || date_end == NPY_DATETIME_NAT) { |
| 374 | PyErr_SetString(PyExc_ValueError, |
| 375 | "Cannot compute a business day count with a NaT (not-a-time) " |
| 376 | "date"); |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | /* Trivial empty date range */ |
| 381 | if (date_begin == date_end) { |
| 382 | *out = 0; |
| 383 | return 0; |
| 384 | } |
| 385 | else if (date_begin > date_end) { |
| 386 | npy_datetime tmp = date_begin; |
| 387 | date_begin = date_end; |
| 388 | date_end = tmp; |
| 389 | swapped = 1; |
| 390 | // we swapped date_begin and date_end, so we need to correct for the |
| 391 | // original date_end that should not be included. gh-23197 |
| 392 | date_begin++; |
| 393 | date_end++; |
| 394 | } |
| 395 | |
| 396 | /* Remove any earlier holidays */ |
| 397 | holidays_begin = find_earliest_holiday_on_or_after(date_begin, |
| 398 | holidays_begin, holidays_end); |
| 399 | /* Remove any later holidays */ |
| 400 | holidays_end = find_earliest_holiday_on_or_after(date_end, |
| 401 | holidays_begin, holidays_end); |
| 402 | |
| 403 | /* Start the count as negative the number of holidays in the range */ |
| 404 | count = -(holidays_end - holidays_begin); |
| 405 | |
| 406 | /* Add the whole weeks between date_begin and date_end */ |
| 407 | whole_weeks = (date_end - date_begin) / 7; |
| 408 | count += whole_weeks * busdays_in_weekmask; |
| 409 | date_begin += whole_weeks * 7; |
| 410 | |
| 411 | if (date_begin < date_end) { |
| 412 | /* Get the day of the week for 'date_begin' */ |
| 413 | day_of_week = get_day_of_week(date_begin); |
| 414 | |
| 415 | /* Count the remaining days one by one */ |
| 416 | while (date_begin < date_end) { |
| 417 | if (weekmask[day_of_week]) { |
| 418 | count++; |
no test coverage detected