Crate yearfrac

source ·
Expand description

yearfrac

yearfrac is a rust crate used to calculate fraction of a year between two dates(chrono::NaiveDate).

Supports all five Excel’s daycount conventions:

nasd30/360

act/act

act360

act365

eur30/360

Tested to match Excel’s YEARFRAC function

Examples

use yearfrac::DayCountConvention;
use chrono::{NaiveDate, Datelike};
let start = NaiveDate::from_ymd(1978, 2, 28);
let end = NaiveDate::from_ymd(2020, 5, 17);
let yf = DayCountConvention::from_int(0).unwrap()
               .yearfrac(start, end);
assert!((yf - 42.21388888889).abs() < 1e-9);

let yf = DayCountConvention::from_str("act/act").unwrap()
            .yearfrac(start, end);
assert!((yf - 42.21424933147).abs() < 1e-9);

use yearfrac::is_leap_year;
assert_eq!(is_leap_year(start.year()) as i32, 0);

let yf = DayCountConvention::US30360.yearfrac_signed(end, start);
assert!((yf + 42.21388888889).abs() < 1e-9);

Enums

Functions