7 releases

0.1.4 May 12, 2024
0.1.3 Apr 21, 2022
0.1.2 Jan 29, 2021
0.1.1 Sep 15, 2019
0.0.1 Nov 26, 2018

#2 in #finance

Download history 2256/week @ 2024-03-03 2126/week @ 2024-03-10 2278/week @ 2024-03-17 1960/week @ 2024-03-24 2187/week @ 2024-03-31 2144/week @ 2024-04-07 3195/week @ 2024-04-14 2630/week @ 2024-04-21 2093/week @ 2024-04-28 2284/week @ 2024-05-05 2292/week @ 2024-05-12 2180/week @ 2024-05-19 2046/week @ 2024-05-26 2360/week @ 2024-06-02 2178/week @ 2024-06-09 2092/week @ 2024-06-16

8,720 downloads per month
Used in 2 crates

MIT license

105KB
1.5K SLoC

bdays

License CI docs version

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 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

  • bdays::calendars::brazil::BrazilExchange : B3 Exchange holidays (http://www.b3.com.br)

  • bdays::calendars::us::USSettlement : United States federal holidays

Usage

Add these dependencies to your Cargo.toml file.

[dependencies]
bdays = "0.1"
chrono = "0.4"

The following example shows the basic functions from this package.

use chrono::NaiveDate;
use bdays::HolidayCalendar;

fn main() {
    // 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 business day
    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);
    assert_eq!( cal.bdays(d2, d0), -2);
}

HolidayCalendarCache

As a motivation, this example might take some time to finish.

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.

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);
}

Dependencies

~1MB
~18K SLoC