1 unstable release

0.1.0 Oct 4, 2022

#567 in Date and time

MIT license

18KB
237 lines

Workflow Status Maintenance

business

A crate for doing business day calculations. It is a Rust implementation of GoCardless' business Ruby gem.

Let's dive right in with an example. For more details, see Calendar.

use chrono::NaiveDate;

let xmas = NaiveDate::from_ymd(2020, 12, 25); // Friday

let cal = business::Calendar::with_holidays(&[xmas]);

assert_eq!(cal.is_business_day(xmas), false);

// The earliest business day
assert_eq!(cal.roll_forward(xmas), NaiveDate::from_ymd(2020, 12, 28));

let xmas_eve = NaiveDate::from_ymd(2020, 12, 24);
assert_eq!(cal.is_business_day(xmas_eve), true);

// Skips over weekend and business holidays
assert_eq!(cal.add_business_days(xmas_eve, 2), NaiveDate::from_ymd(2020, 12, 29));

Building a Calendar from YAML

The YAML has to be in the following format:

# Defaults to Mon-Fri if omitted
working_days:
  - monday
  - tuesday
  - wednesday
  - thursday
  - friday
# ISO 8601 dates, defaults to no holidays if omitted
holidays:
  - 2017-12-25
  - 2017-12-26

A calendar can be built as such:

let yml = std::fs::read_to_string("examples/basic/cal.yml").unwrap();
let cal: Calendar = serde_yaml::from_str(&yml).unwrap();

Dependencies

~2.8–4MB
~74K SLoC