use crate::time::date::Date;
use crate::time::day_counter::DayCounter;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Thirty360Convention {
#[default]
BondBasis,
European,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Thirty360 {
convention: Thirty360Convention,
}
impl Thirty360 {
pub const fn new(convention: Thirty360Convention) -> Self {
Self { convention }
}
}
impl DayCounter for Thirty360 {
fn name(&self) -> &'static str {
match self.convention {
Thirty360Convention::BondBasis => "30/360 (Bond Basis)",
Thirty360Convention::European => "30E/360 (Eurobond Basis)",
}
}
fn day_count(&self, start: Date, end: Date) -> i64 {
let (y1, m1, mut d1) = start.ymd();
let (y2, m2, mut d2) = end.ymd();
match self.convention {
Thirty360Convention::BondBasis => {
if d1 == 31 {
d1 = 30;
}
if d2 == 31 && d1 >= 30 {
d2 = 30;
}
}
Thirty360Convention::European => {
if d1 == 31 {
d1 = 30;
}
if d2 == 31 {
d2 = 30;
}
}
}
360 * (y2 as i64 - y1 as i64)
+ 30 * (m2.as_number() as i64 - m1.as_number() as i64)
+ (d2 as i64 - d1 as i64)
}
#[inline]
fn year_fraction(&self, start: Date, end: Date) -> f64 {
self.day_count(start, end) as f64 / 360.0
}
}