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

//! Provides functions to perform business days calculation between dates,
//! given a Holiday Calendar.
//!
//! A Business Day is defined as a weekday that is not a holiday.
//! To check if a date is a holiday, you must provide an implementation of the `HolidayCalendar` trait.
//!
//! This crate is a port of [BusinessDays.jl](https://github.com/felipenoris/BusinessDays.jl) to the Rust programming language.
//!
//! # Provided Holiday Calendars
//!
//! This crate provides a set of built in holiday calendars in the `bdays::calendars` submodule.
//!
//! * `bdays::calendars::WeekendsOnly` : accounts only weekends
//! * `bdays::calendars::brazil::BRSettlement` : Brazilian banking holidays
//!
//! # Usage
//!
//! ```rust
//! extern crate bdays;
//! extern crate chrono;
//! use chrono::NaiveDate;
//! use bdays::HolidayCalendar;
//!
//! // creates a holiday calendar instance
//! let cal = bdays::calendars::WeekendsOnly;
//!
//! let d0 = NaiveDate::from_ymd(2018, 11, 22);
//! let d1 = NaiveDate::from_ymd(2018, 11, 24);
//! let d2 = NaiveDate::from_ymd(2018, 11, 26);
//!
//! // checks if a date is a holiday
//! assert_eq!( cal.is_holiday(d0), false );
//!
//! // checks if a date is a business day
//! assert_eq!( cal.is_bday(d0), true);
//! assert_eq!( cal.is_bday(d1), false);
//!
//! // adjusts to the last/next businessdays
//! assert_eq!(cal.to_bday(d1, false), NaiveDate::from_ymd(2018, 11, 23));
//! assert_eq!(cal.to_bday(d1, true), d2);
//!
//! // advances a number of business days
//! assert_eq!(cal.advance_bdays(d0, 2), d2);
//! assert_eq!(cal.advance_bdays(d2, -2), d0);
//!
//! // returns the number of business days between dates
//! assert_eq!( cal.bdays(d0, d2), 2);
//! ```
//!
//! # HolidayCalendarCache
//!
//! As a motivation, this example might take some time to finish.
//!
//! ```rust
//! extern crate bdays;
//! extern crate chrono;
//! use chrono::NaiveDate;
//! use bdays::HolidayCalendar;
//!
//! let cal = bdays::calendars::brazil::BRSettlement;
//! let d0 = NaiveDate::from_ymd(2001, 2, 1);
//! let d1 = NaiveDate::from_ymd(2100, 2, 1);
//!
//! for _i in 0..30 {
//!     cal.bdays(d0, d1);
//! }
//! ```
//!
//! You can use `HolidayCalendarCache` to perform fast business days calculation
//! for a given range of dates.
//!
//! ```rust
//! extern crate bdays;
//! extern crate chrono;
//! use chrono::NaiveDate;
//! use bdays::HolidayCalendar;
//!
//! let cal = bdays::HolidayCalendarCache::new(bdays::calendars::brazil::BRSettlement, NaiveDate::from_ymd(1980, 1, 1), NaiveDate::from_ymd(2100, 12, 31));
//! let d0 = NaiveDate::from_ymd(2001, 2, 1);
//! let d1 = NaiveDate::from_ymd(2100, 2, 1);
//!
//! for _i in 0..30 {
//!     cal.bdays(d0, d1);
//! }
//! ```

extern crate chrono;

use std::fmt::Display;
use chrono::Datelike;
use chrono::Weekday;
use std::cmp::PartialOrd;

/// Algorithms to calculate easter dates.
pub mod easter;

/// A set of holiday calendars built into bdays crate.
pub mod calendars;

#[cfg(test)]
mod tests;

/// Returns `true` if `date` occurs on a Saturday or a Sunday.
pub fn is_weekend<T: Datelike + Copy>(date: T) -> bool {
    match date.weekday() {
        Weekday::Sat | Weekday::Sun => true,
        _ => false
    }
}

/// Returns `true` if `date` does not occur on a weekend.
pub fn is_weekday<T: Datelike + Copy>(date: T) -> bool {
    !is_weekend(date)
}

fn next_date<T: Datelike + Copy>(date: T, fwd: bool) -> T {
    let inc: i32 = {
        if fwd {
            1
        } else {
            -1
        }
    };

    match date.with_ordinal((date.ordinal() as i32 + inc) as u32) {
        Some(dt) => dt,
        None =>  {
            if fwd {
                date
                    .with_year(date.year() + 1).unwrap()
                    .with_ordinal(1).unwrap()
            } else {
                date
                    .with_year(date.year() - 1).unwrap()
                    .with_month(12).unwrap()
                    .with_day(31).unwrap()
            }
        }
    }
}

/// Abstraction for a Holiday Calendar.
pub trait HolidayCalendar<T: Datelike + Copy + PartialOrd> {

    /// Returns `true` if `date` is a holiday.
    fn is_holiday(&self, date: T) -> bool;

    /// Returns `true` if `date` is a Business Day.
    /// A Business Day is defined as a weekday that is not a holiday.
    fn is_bday(&self, date: T) -> bool {
        !(is_weekend(date) || self.is_holiday(date))
    }

    /// Adjusts `date` to the last/next business day if it's not a business day.
    fn to_bday(&self, mut date: T, adjust_next: bool) -> T {
        while !self.is_bday(date) {
            date = next_date(date, adjust_next);
        }
        date
    }

    /// Advances `bdays_count` number of business days from `date`.
    fn advance_bdays(&self, mut date: T, bdays_count: i32) -> T {
        date = self.to_bday(date, true);

        let inc_fwd = match bdays_count.signum() {
            0 => return date, // nothing to do
            1 => true, // bdays_count is positive
            -1 => false, // bdays_count is negative
            _ => panic!("signum function is expected to return 0, 1 or -1."),
        };

        let mut num_iterations = bdays_count.abs();

        while num_iterations > 0 {
            date = next_date(date, inc_fwd);

            // Looks for previous / next Business Day
            while !self.is_bday(date) {
                date = next_date(date, inc_fwd);
            }

            num_iterations += -1;
        }

        date
    }

    /// Returns the number of business days between `d0` and `d1`.
    fn bdays(&self, mut d0: T, mut d1: T) -> i32 {
        d0 = self.to_bday(d0, true);
        d1 = self.to_bday(d1, true);

        let mut from: T;
        let to: T;
        let inc: i32;
        let mut bdays_count: i32 = 0;

        if d0 <= d1 {
            inc = 1;
            from = d0;
            to = d1;
        } else {
            inc = -1;
            from = d1;
            to = d0
        }

        while from < to {
            from = self.advance_bdays(from, 1);
            bdays_count += inc;
        }

        bdays_count
    }
}

/// Caches business days calculation for a given holiday calendar
/// and a given range of dates. Implements the `HolidayCalendar` trait.
pub struct HolidayCalendarCache<T: Datelike + Copy + PartialOrd> {
    is_holiday_vec: Vec<bool>,
    is_bday_vec: Vec<bool>,
    bdays_counter_vec: Vec<i32>,
    dt_min: T,
    dt_max: T,
}

impl<T: Datelike + Copy + PartialOrd + Display> HolidayCalendarCache<T> {

    /// Creates `HolidayCalendarCache` that caches business days calculation
    /// in the range of dates from `dt_min` to `dt_max`.
    pub fn new<H: HolidayCalendar<T>>(calendar: H, dt_min: T, dt_max: T) -> HolidayCalendarCache<T> {
        if dt_min > dt_max {
            panic!("dt_min {} should not be greater than dt_max {}.", dt_min, dt_max);
        }

        let len = (dt_max.num_days_from_ce() - dt_min.num_days_from_ce() + 1) as usize;
        let mut is_holiday_vec: Vec<bool> = Vec::with_capacity(len);
        let mut is_bday_vec: Vec<bool> = Vec::with_capacity(len);
        let mut bdays_counter_vec: Vec<i32> = Vec::with_capacity(len);

        is_holiday_vec.push(calendar.is_holiday(dt_min));
        is_bday_vec.push(calendar.is_bday(dt_min));

        let mut bdays_counter = 0;
        bdays_counter_vec.push(bdays_counter);

        let mut dt = next_date(dt_min, true);
        for _i in 1..len {
            let dt_is_bday = calendar.is_bday(dt);
            is_bday_vec.push(dt_is_bday);
            is_holiday_vec.push(calendar.is_holiday(dt));

            if dt_is_bday {
                bdays_counter += 1;
            }

            bdays_counter_vec.push(bdays_counter);
            dt = next_date(dt, true);
        }

        // lengths must match
        debug_assert_eq!(is_bday_vec.len(), bdays_counter_vec.len());
        debug_assert_eq!(is_holiday_vec.len(), bdays_counter_vec.len());

        HolidayCalendarCache{
            is_holiday_vec,
            is_bday_vec,
            bdays_counter_vec,
            dt_min,
            dt_max,
        }
    }

    fn row_index(&self, date: T) -> usize {
        (date.num_days_from_ce() - self.dt_min.num_days_from_ce()) as usize
    }

    fn assert_in_bounds(&self, date: T) {
        if date < self.dt_min || self.dt_max < date {
            panic!("Date {} out of bounds of holiday calendar cache. [{}, {}].", date, self.dt_min, self.dt_max);
        }
    }
}

impl<T: Datelike + Copy + PartialOrd + Display> HolidayCalendar<T> for HolidayCalendarCache<T> {

    fn is_holiday(&self, date: T) -> bool {
        self.assert_in_bounds(date);
        self.is_holiday_vec[ self.row_index(date) ]
    }

    fn is_bday(&self, date: T) -> bool {
        self.assert_in_bounds(date);
        self.is_bday_vec[ self.row_index(date) ]
    }

    fn bdays(&self, mut d0: T, mut d1: T) -> i32 {
        d0 = self.to_bday(d0, true);
        d1 = self.to_bday(d1, true);

        self.bdays_counter_vec[ self.row_index(d1) ] - self.bdays_counter_vec[ self.row_index(d0) ]
    }
}