Implementation:Avhz RustQuant Time Utilities
| Knowledge Sources | |
|---|---|
| Domains | Calendar, Time_Value_of_Money |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete tool for general calendar, date, and holiday utility functions provided by the RustQuant library.
Description
The Time Utilities module provides a comprehensive collection of standalone helper functions for date manipulation, calendar queries, and holiday detection that underpin the rest of the RustQuant time/calendar system. These functions are used internally by the Calendar, DayCountConvention, and country-specific holiday modules.
Date unpacking and Easter computation:
unpack_date(date, is_orthodox)-- decomposes aDateinto a tuple of (year, month, day, weekday, ordinal day, Easter Monday ordinal). Supports both Western and Orthodox Easter computation via a lookup table inEASTER_MONDAYS.
Year fraction and day counting:
year_fraction(start, end)-- computes a default year fraction as actual days / 365.25.days_between(start, end)-- computes the absolute number of days between two dates.leap_year_count(start, end)-- counts the number of leap years in a date range.contains_leap_year(start, end)-- checks if any year in the range is a leap year.get_years_in_range(start, end)-- returns a vector of all years spanned by the date range.get_days_in_years_in_range(start, end)-- returns the number of days (365 or 366) for each year in the range.
Weekend and weekday detection:
is_weekend(date)-- returnstruefor Saturday or Sunday.is_weekday(date)-- returnstruefor Monday through Friday.
Month boundary checks:
is_first_day_of_month(date)-- checks if the day is 1.is_last_day_of_month(date)-- checks if the day equals the month's length.is_last_day_of_february(date)-- checks for Feb 28 (non-leap) or Feb 29 (leap).
Business day navigation:
next_business_day(date, calendar)-- advances forward to the next business day.previous_business_day(date, calendar)-- moves backward to the previous business day.
Date sequence generation:
date_sequence(start, end)-- generates a vector of all dates from start to end inclusive.
First/Last weekday of month: A complete set of 14 functions to find the first or last occurrence of any weekday (Monday through Sunday) in a given month and year:
get_first_monday_of_monththroughget_first_sunday_of_monthget_last_monday_of_monththroughget_last_sunday_of_month
Common holiday checks (crate-internal):
is_christmas_day,is_christmas_eve,is_new_years_day,is_new_years_eve
Current date:
today()-- returns the current UTC date.
Usage
Use these utility functions when implementing custom calendars, performing date arithmetic, checking date properties (weekend, month boundary, leap year), navigating to business days, or generating date sequences for schedule construction.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_time/src/utilities.rs
- Lines: 1-555
Signature
pub fn unpack_date(date: Date, is_orthodox: bool) -> (i32, Month, u8, Weekday, u16, u16);
pub fn year_fraction(start: Date, end: Date) -> f64;
pub fn is_weekend(date: Date) -> bool;
pub fn is_weekday(date: Date) -> bool;
pub fn get_years_in_range(start: Date, end: Date) -> Vec<i32>;
pub fn today() -> Date;
pub fn get_days_in_years_in_range(start: Date, end: Date) -> Vec<u16>;
pub fn contains_leap_year(start: Date, end: Date) -> bool;
pub fn days_between(start: Date, end: Date) -> i64;
pub fn leap_year_count(start: Date, end: Date) -> i64;
pub fn is_first_day_of_month(date: Date) -> bool;
pub fn is_last_day_of_month(date: Date) -> bool;
pub fn is_last_day_of_february(date: Date) -> bool;
pub fn next_business_day(date: Date, calendar: &Calendar) -> Date;
pub fn previous_business_day(date: Date, calendar: &Calendar) -> Date;
pub fn date_sequence(start: Date, end: Date) -> Vec<Date>;
pub fn get_first_day_of_month(year: i32, month: Month) -> Result<Weekday, Error>;
pub fn get_last_day_of_month(year: i32, month: Month) -> Result<Weekday, Error>;
pub fn get_first_monday_of_month(year: i32, month: Month) -> Result<Date, Error>;
// ... get_first_{tuesday..sunday}_of_month ...
pub fn get_last_monday_of_month(year: i32, month: Month) -> Result<Date, Error>;
// ... get_last_{tuesday..sunday}_of_month ...
Import
use RustQuant::time::utilities::{
unpack_date, year_fraction, is_weekend, days_between,
leap_year_count, next_business_day, date_sequence,
get_first_monday_of_month, get_last_friday_of_month,
};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| date | Date | Yes (single-date functions) | The date to inspect or navigate from |
| start | Date | Yes (range functions) | The start of the date range |
| end | Date | Yes (range functions) | The end of the date range |
| is_orthodox | bool | Yes (unpack_date) | Whether to use Orthodox Easter Monday table |
| calendar | &Calendar | Yes (business day navigation) | The calendar to use for business day determination |
| year | i32 | Yes (first/last weekday functions) | The year to query |
| month | Month | Yes (first/last weekday functions) | The month to query |
Outputs
| Name | Type | Description |
|---|---|---|
| tuple | (i32, Month, u8, Weekday, u16, u16) | Unpacked date components including Easter Monday ordinal |
| f64 | f64 | Year fraction (from year_fraction) |
| bool | bool | Result of weekend, weekday, month boundary, or leap year checks |
| i64 | i64 | Day count (from days_between) or leap year count |
| Vec<Date> | Vec<Date> | Generated date sequence |
| Vec<i32> | Vec<i32> | Years in range |
| Vec<u16> | Vec<u16> | Days per year in range |
| Date | Date | Next or previous business day, or first/last weekday of month |
Usage Examples
use time::{Date, Month};
use RustQuant::time::utilities::{
days_between, leap_year_count, get_days_in_years_in_range,
get_first_monday_of_month, is_weekend, date_sequence,
};
// Count days between two dates.
let start = Date::from_calendar_date(2023, Month::January, 1).unwrap();
let end = Date::from_calendar_date(2024, Month::January, 1).unwrap();
assert_eq!(days_between(start, end), 365);
// Count leap years in a range.
let end2 = Date::from_calendar_date(2025, Month::January, 1).unwrap();
assert_eq!(leap_year_count(start, end2), 1);
// Get days in each year across a range.
let days = get_days_in_years_in_range(
Date::from_calendar_date(2023, Month::July, 1).unwrap(),
Date::from_calendar_date(2025, Month::January, 1).unwrap(),
);
assert_eq!(days, vec![365, 366, 365]);
// Find the first Monday of a month.
let first_monday = get_first_monday_of_month(2024, Month::January).unwrap();
assert_eq!(first_monday, Date::from_calendar_date(2024, Month::January, 1).unwrap());
// Check if a date is a weekend.
let saturday = Date::from_calendar_date(2024, Month::March, 9).unwrap();
assert!(is_weekend(saturday));