quantlib_rs 0.1.0

A ground-up, idiomatic Rust quantitative finance library inspired by QuantLib.
Documentation
//! Financial calendar traits and business day conventions.

use crate::time::date::{Date, DateError, Period};

/// Business day convention for rolling dates when they fall on a holiday or weekend.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum BusinessDayConvention {
    /// Advance to the first subsequent business day.
    #[default]
    Following,
    /// Advance to the first subsequent business day unless it falls in the next calendar month,
    /// in which case retreat to the first preceding business day.
    ModifiedFollowing,
    /// Retreat to the first preceding business day.
    Preceding,
    /// Retreat to the first preceding business day unless it falls in the previous calendar month,
    /// in which case advance to the first subsequent business day.
    ModifiedPreceding,
    /// Do not adjust the date.
    Unadjusted,
}

/// Trait implemented by all holiday calendars.
///
/// Implementors must be `Send + Sync` to guarantee thread safety and allow
/// multi-threaded pricing and curve bootstrapping.
pub trait Calendar: Send + Sync {
    /// Return the name of the calendar.
    fn name(&self) -> &'static str;

    /// Return true if the given date is a business day.
    fn is_business_day(&self, date: Date) -> bool;

    /// Return true if the given date is a weekend day.
    fn is_weekend(&self, date: Date) -> bool {
        date.weekday().is_weekend()
    }

    /// Return true if the given date is a holiday (including weekends).
    fn is_holiday(&self, date: Date) -> bool {
        !self.is_business_day(date)
    }

    /// Adjust a date according to the given business day convention.
    ///
    /// # Examples
    /// ```
    /// use quantlib_rs::time::calendar::{Calendar, BusinessDayConvention};
    /// use quantlib_rs::time::calendars::weekends_only::WeekendsOnly;
    /// use quantlib_rs::time::date::{Date, Month, Weekday};
    ///
    /// let cal = WeekendsOnly;
    /// // 2026-09-05 is Saturday
    /// let saturday = Date::from_ymd(2026, Month::September, 5).unwrap();
    /// assert_eq!(saturday.weekday(), Weekday::Saturday);
    ///
    /// let following = cal.adjust(saturday, BusinessDayConvention::Following);
    /// assert_eq!(following, Date::from_ymd(2026, Month::September, 7).unwrap()); // Monday
    /// ```
    fn adjust(&self, date: Date, convention: BusinessDayConvention) -> Date {
        match convention {
            BusinessDayConvention::Unadjusted => date,
            BusinessDayConvention::Following => {
                let mut d = date;
                while !self.is_business_day(d) {
                    d = d + 1;
                }
                d
            }
            BusinessDayConvention::ModifiedFollowing => {
                let mut d = date;
                while !self.is_business_day(d) {
                    d = d + 1;
                }
                if d.month() != date.month() {
                    let mut d_prev = date;
                    while !self.is_business_day(d_prev) {
                        d_prev = d_prev - 1;
                    }
                    d_prev
                } else {
                    d
                }
            }
            BusinessDayConvention::Preceding => {
                let mut d = date;
                while !self.is_business_day(d) {
                    d = d - 1;
                }
                d
            }
            BusinessDayConvention::ModifiedPreceding => {
                let mut d = date;
                while !self.is_business_day(d) {
                    d = d - 1;
                }
                if d.month() != date.month() {
                    let mut d_next = date;
                    while !self.is_business_day(d_next) {
                        d_next = d_next + 1;
                    }
                    d_next
                } else {
                    d
                }
            }
        }
    }

    /// Advance a date by a given [`Period`], applying business day convention.
    fn advance(
        &self,
        date: Date,
        period: Period,
        convention: BusinessDayConvention,
        preserve_end_of_month: bool,
    ) -> Result<Date, DateError> {
        match period {
            Period::Days(0) => Ok(self.adjust(date, convention)),
            Period::Days(days) => {
                // If convention is unadjusted or advance is in calendar days:
                // If days is positive, step day-by-day counting business days
                let mut d = date;
                if days > 0 {
                    let mut count = 0;
                    while count < days {
                        d = d + 1;
                        if self.is_business_day(d) {
                            count += 1;
                        }
                    }
                    Ok(d)
                } else {
                    let mut count = 0;
                    let target = days.abs();
                    while count < target {
                        d = d - 1;
                        if self.is_business_day(d) {
                            count += 1;
                        }
                    }
                    Ok(d)
                }
            }
            Period::Weeks(_) | Period::Months(_) | Period::Years(_) => {
                let raw_date = date.advance(period, preserve_end_of_month)?;
                Ok(self.adjust(raw_date, convention))
            }
        }
    }

    /// Count the number of business days in the range `[from, to]`.
    fn business_days_between(
        &self,
        from: Date,
        to: Date,
        include_first: bool,
        include_last: bool,
    ) -> usize {
        if from > to {
            return 0;
        }
        let start_serial = from.serial() + if include_first { 0 } else { 1 };
        let end_serial = to.serial() - if include_last { 0 } else { 1 };
        if start_serial > end_serial {
            return 0;
        }
        let mut count = 0;
        for s in start_serial..=end_serial {
            if self.is_business_day(Date::from_serial(s)) {
                count += 1;
            }
        }
        count
    }
}