Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/felipenoris/bdays
Business Days calendars for Rustaceans.
https://github.com/felipenoris/bdays
bank-holidays calendar days-calculator holidays
Last synced: 3 months ago
JSON representation
Business Days calendars for Rustaceans.
- Host: GitHub
- URL: https://github.com/felipenoris/bdays
- Owner: felipenoris
- License: mit
- Created: 2018-11-25T00:06:43.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-12T12:16:50.000Z (8 months ago)
- Last Synced: 2024-10-13T13:29:30.537Z (3 months ago)
- Topics: bank-holidays, calendar, days-calculator, holidays
- Language: Rust
- Homepage: https://docs.rs/bdays
- Size: 92.8 KB
- Stars: 17
- Watchers: 4
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bdays
[![License][license-img]](LICENSE)
[![CI][ci-img]][ci-url]
[![docs][docs-img]][docs-url]
[![version][version-img]][version-url][license-img]: https://img.shields.io/crates/l/bdays.svg
[ci-img]: https://github.com/felipenoris/bdays/workflows/CI/badge.svg
[ci-url]: https://github.com/felipenoris/bdays/actions
[docs-img]: https://docs.rs/bdays/badge.svg
[docs-url]: https://docs.rs/bdays
[version-img]: https://img.shields.io/crates/v/bdays.svg
[version-url]: https://crates.io/crates/bdaysProvides 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
* `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.
```toml
[dependencies]
bdays = "0.1"
chrono = "0.4"
```The following example shows the basic functions from this package.
```rust
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);
}
```
## HolidayCalendarCacheAs a motivation, this example might take some time to finish.
```rust
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
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);
}
```