Skip to content

Commit

Permalink
linear time WeekendsOnly bdays impl
Browse files Browse the repository at this point in the history
  • Loading branch information
felipenoris committed Sep 15, 2019
1 parent 3e5d4db commit aaa4ab1
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/calendars.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

use crate::HolidayCalendar;
use ::chrono::Datelike;
use ::chrono::{Datelike, NaiveDate};

/// Holiday Calendars for Brazil.
pub mod brazil;
Expand All @@ -16,4 +16,52 @@ impl<T: Datelike + Copy + PartialOrd> HolidayCalendar<T> for WeekendsOnly {
fn is_holiday(&self, _date: T) -> bool {
false
}

fn bdays(&self, d0: T, d1: T) -> i32 {

if d0 == d1 {
0
} else {

let from: NaiveDate;
let to: NaiveDate;
let swapped: bool;

if d1 < d0 {
from = NaiveDate::from_num_days_from_ce(d1.num_days_from_ce());
to = NaiveDate::from_num_days_from_ce(d0.num_days_from_ce());
swapped = true;
} else {
from = NaiveDate::from_num_days_from_ce(d0.num_days_from_ce());
to = NaiveDate::from_num_days_from_ce(d1.num_days_from_ce());
swapped = false;
}

let mut result: i32 = 0;
let days = to.num_days_from_ce() - from.num_days_from_ce();
let whole_weeks = days / 7;
result += whole_weeks * 5;

let mut current_date = NaiveDate::from_num_days_from_ce(from.num_days_from_ce() + whole_weeks * 7);

if current_date < to {
let mut day_of_week = current_date.weekday();

while current_date < to {
if day_of_week.number_from_monday() < 6 {
result += 1;
}

current_date = NaiveDate::from_num_days_from_ce(current_date.num_days_from_ce() + 1);
day_of_week = day_of_week.succ();
}
}

if swapped {
result = -result;
}

result
}
}
}

0 comments on commit aaa4ab1

Please sign in to comment.