Skip to main content

Calendar

Trait Calendar 

pub trait Calendar: Send + Sync {
Show 17 methods // Provided methods fn name(&self) -> &str { ... } fn is_weekend(&self, date: NaiveDate) -> bool { ... } fn is_holiday(&self, _date: NaiveDate) -> bool { ... } fn is_business_day(&self, date: NaiveDate) -> bool { ... } fn adjust( &self, date: NaiveDate, convention: BusinessDayConvention, ) -> NaiveDate { ... } fn advance( &self, date: NaiveDate, period: Period, convention: BusinessDayConvention, end_of_month: bool, ) -> NaiveDate { ... } fn advance_with_roll( &self, date: NaiveDate, period: Period, roll: RollConvention, convention: BusinessDayConvention, end_of_month: bool, ) -> NaiveDate { ... } fn end_of_month(&self, date: NaiveDate) -> NaiveDate { ... } fn is_end_of_month(&self, date: NaiveDate) -> bool { ... } fn is_month_end_anchor(&self, date: NaiveDate) -> bool { ... } fn next_business_day(&self, date: NaiveDate) -> NaiveDate { ... } fn previous_business_day(&self, date: NaiveDate) -> NaiveDate { ... } fn add_business_days(&self, date: NaiveDate, n: i64) -> NaiveDate { ... } fn business_days_between(&self, start: NaiveDate, end: NaiveDate) -> i64 { ... } fn business_days_between_inc( &self, from: NaiveDate, to: NaiveDate, include_first: bool, include_last: bool, ) -> i64 { ... } fn holidays( &self, from: NaiveDate, to: NaiveDate, include_weekends: bool, ) -> Vec<NaiveDate> { ... } fn business_days(&self, from: NaiveDate, to: NaiveDate) -> Vec<NaiveDate> { ... }
}
Expand description

A business-day calendar: which dates are good business days, plus the date-rolling helpers built on that predicate.

Calendars are immutable shared reference data, so the trait requires Send + Sync — this lets jurisdiction calendars be cached in statics behind Arc<dyn Calendar>.

Provided Methods§

fn name(&self) -> &str

A short name for the calendar (diagnostic).

fn is_weekend(&self, date: NaiveDate) -> bool

Whether date is a weekend (Saturday or Sunday by default).

fn is_holiday(&self, _date: NaiveDate) -> bool

Whether date is closed for a reason beyond the weekend: a listed closure, not a Saturday. A weekend day carrying no listed closure is not a holiday, and the default answers false for every date accordingly.

The trait states the reading rather than leaving it to each impl, because the two readings differ on roughly 104 days a year and disagreed here for a long time — a data-backed calendar answered “not a business day” while a joint one answered “closed beyond the weekend”, so the same question put to two calendars over the same closures got two answers.

What settles it is Calendar::is_business_day’s own default, !is_weekend(date) && !is_holiday(date): that composition partitions the closed days into weekend and holiday, and its !is_weekend term is only non-redundant under this reading. It is also what Calendar::holidays already means by a holiday when weekends are excluded.

fn is_business_day(&self, date: NaiveDate) -> bool

Whether date is a good business day (not a weekend, not a holiday).

fn adjust( &self, date: NaiveDate, convention: BusinessDayConvention, ) -> NaiveDate

Rolls date to a business day per convention.

fn advance( &self, date: NaiveDate, period: Period, convention: BusinessDayConvention, end_of_month: bool, ) -> NaiveDate

Advances date by period (calendar arithmetic), then adjusts the result per convention.

When end_of_month is set, the seed is a business end-of-month (see Calendar::is_end_of_month), and the period unit is Month or Year, the result lands on the business end-of-month of the target month (the end-of-month roll rule: 28-Feb +6M → 31-Aug). Otherwise the result is the calendar arithmetic adjusted by convention.

fn advance_with_roll( &self, date: NaiveDate, period: Period, roll: RollConvention, convention: BusinessDayConvention, end_of_month: bool, ) -> NaiveDate

Advances date by period, snaps the result by roll, then adjusts it per convention — the roll-aware form of Calendar::advance, which is this method under RollConvention::None.

The roll is what makes a tenor advance reproduce a schedule boundary: a month step clamps into short months (31 Jan + 1M = 28 Feb), and a day-of-month roll restores the day the schedule anchors on rather than ratcheting it down. Schedule generation applies RollConvention::apply, the same operation this does.

end_of_month supersedes roll, matching schedule generation: when the mode fires — the seed is a business month-end anchor and the period unit is Month or Year — the result is the business end-of-month of the target month and the roll is not applied.

fn end_of_month(&self, date: NaiveDate) -> NaiveDate

The last business day of date’s month — the latest good business day in the month (weekends and holidays rolled back).

This is the business month end. The calendar month end is last_day_of_month, the day this rolls back from; the two differ whenever a month ends on a weekend or a holiday.

fn is_end_of_month(&self, date: NaiveDate) -> bool

Whether date is the last business day of its month (its business end-of-month). Strict: a trailing weekend day after the last business day is not end-of-month (use Calendar::is_month_end_anchor for the end-of-month roll gate, which also fires on those trailing days).

fn is_month_end_anchor(&self, date: NaiveDate) -> bool

Whether date anchors the end-of-month roll: it is on or after its month’s last business day. This fires for the last business day and any trailing weekend/holiday days (so a month-end falling on a weekend, e.g. Sat 28-Feb, still anchors the rule), matching the end-of-month advance behaviour in Calendar::advance.

fn next_business_day(&self, date: NaiveDate) -> NaiveDate

The first business day on or after date.

fn previous_business_day(&self, date: NaiveDate) -> NaiveDate

The first business day on or before date.

fn add_business_days(&self, date: NaiveDate, n: i64) -> NaiveDate

Shifts date by n good business days, skipping weekends and holidays. Negative n looks backward; n == 0 returns date unchanged (even if it is itself a holiday). Each counted step lands on a business day — this is the primitive a fixing lag (e.g. -2 business days off the reset anchor) is built on.

fn business_days_between(&self, start: NaiveDate, end: NaiveDate) -> i64

The number of business days in the half-open interval [start, end). Returns 0 when start >= end. This is the count BUS/252 divides by 252. The default iterates day by day; the data-backed calendars (DataCalendar, JointCalendar) override it with an O(1) prefix-sum subtraction over their precomputed business-day grid.

fn business_days_between_inc( &self, from: NaiveDate, to: NaiveDate, include_first: bool, include_last: bool, ) -> i64

The number of business days between from and to with caller-controlled inclusion of each endpoint. When to < from the count is the negation of the forward count with the inclusion flags swapped; when to == from the result is 1 only if both endpoints are included and the date is a business day. The two-argument Calendar::business_days_between is the [start, end) case (include_first = true, include_last = false).

fn holidays( &self, from: NaiveDate, to: NaiveDate, include_weekends: bool, ) -> Vec<NaiveDate>

The holidays in the inclusive range [from, to], ascending. Weekends are included only when include_weekends is set; a “holiday” is any non-business day.

fn business_days(&self, from: NaiveDate, to: NaiveDate) -> Vec<NaiveDate>

The business days in the inclusive range [from, to], ascending.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§