-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a559ea
commit 79d87d2
Showing
3 changed files
with
51 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/// Ported from: | ||
/// https://github.com/numpy/numpy/blob/e59c074842e3f73483afa5ddef031e856b9fd313/numpy/_core/src/multiarray/datetime_busday.c#L355-L433 | ||
#[doc(hidden)] | ||
pub fn business_day_count_impl( | ||
mut start_date: i32, | ||
mut end_date: i32, | ||
week_mask: &[bool; 7], | ||
n_business_days_in_week_mask: i32, | ||
) -> i32 { | ||
let swapped = start_date > end_date; | ||
if swapped { | ||
(start_date, end_date) = (end_date, start_date); | ||
start_date += 1; | ||
end_date += 1; | ||
} | ||
|
||
let mut start_weekday = weekday(start_date); | ||
let diff = end_date - start_date; | ||
let whole_weeks = diff / 7; | ||
let mut count = 0; | ||
count += whole_weeks * n_business_days_in_week_mask; | ||
start_date += whole_weeks * 7; | ||
while start_date < end_date { | ||
if unsafe { *week_mask.get_unchecked(start_weekday) } { | ||
count += 1; | ||
} | ||
start_date += 1; | ||
start_weekday += 1; | ||
if start_weekday >= 7 { | ||
start_weekday = 0; | ||
} | ||
} | ||
if swapped { | ||
-count | ||
} else { | ||
count | ||
} | ||
} | ||
|
||
pub(crate) fn weekday(x: i32) -> usize { | ||
// the first modulo might return a negative number, so we add 7 and take | ||
// the modulo again so we're sure we have something between 0 (Monday) | ||
// and 6 (Sunday) | ||
(((x - 4) % 7 + 7) % 7) as usize | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters