pub struct BDatesList { /* private fields */ }
Expand description
Represents a list of business dates generated between a start and end date at a specified frequency. Provides methods to retrieve the full list, count, or dates grouped by period.
Implementations§
Source§impl BDatesList
Represents a collection of business dates generated according to specific rules.
impl BDatesList
Represents a collection of business dates generated according to specific rules.
It can be defined either by a start and end date range or by a start date and a fixed number of periods. It provides methods to retrieve the dates as a flat list, count them, or group them by their natural period (e.g., month, quarter).
Business days are typically Monday to Friday. Weekend dates are skipped or adjusted depending on the frequency rules.
§Examples
1. Using new
(Start and End Date):
use chrono::NaiveDate;
use std::error::Error;
use rustframe::utils::{BDatesList, DateFreq};
fn main() -> Result<(), Box<dyn Error>> {
let start_date = "2023-11-01".to_string(); // Wednesday
let end_date = "2023-11-07".to_string(); // Tuesday
let freq = DateFreq::Daily;
let bdates = BDatesList::new(start_date, end_date, freq);
let expected_dates = vec![
NaiveDate::from_ymd_opt(2023, 11, 1).unwrap(), // Wed
NaiveDate::from_ymd_opt(2023, 11, 2).unwrap(), // Thu
NaiveDate::from_ymd_opt(2023, 11, 3).unwrap(), // Fri
NaiveDate::from_ymd_opt(2023, 11, 6).unwrap(), // Mon
NaiveDate::from_ymd_opt(2023, 11, 7).unwrap(), // Tue
];
assert_eq!(bdates.list()?, expected_dates);
assert_eq!(bdates.count()?, 5);
Ok(())
}
2. Using from_n_periods
(Start Date and Count):
use chrono::NaiveDate;
use std::error::Error;
use rustframe::utils::{BDatesList, DateFreq};
fn main() -> Result<(), Box<dyn Error>> {
let start_date = "2024-02-28".to_string(); // Wednesday
let freq = DateFreq::WeeklyFriday;
let n_periods = 3;
let bdates = BDatesList::from_n_periods(start_date, freq, n_periods)?;
// The first Friday on or after 2024-02-28 is Mar 1.
// The next two Fridays are Mar 8 and Mar 15.
let expected_dates = vec![
NaiveDate::from_ymd_opt(2024, 3, 1).unwrap(),
NaiveDate::from_ymd_opt(2024, 3, 8).unwrap(),
NaiveDate::from_ymd_opt(2024, 3, 15).unwrap(),
];
assert_eq!(bdates.list()?, expected_dates);
assert_eq!(bdates.count()?, 3);
assert_eq!(bdates.start_date_str(), "2024-02-28"); // Keeps original start string
assert_eq!(bdates.end_date_str(), "2024-03-15"); // End date is the last generated date
Ok(())
}
3. Using groups()
:
use chrono::NaiveDate;
use std::error::Error;
use rustframe::utils::{BDatesList, DateFreq};
fn main() -> Result<(), Box<dyn Error>> {
let start_date = "2023-11-20".to_string(); // Mon, Week 47
let end_date = "2023-12-08".to_string(); // Fri, Week 49
let freq = DateFreq::WeeklyMonday;
let bdates = BDatesList::new(start_date, end_date, freq);
// Mondays in range: Nov 20, Nov 27, Dec 4
let groups = bdates.groups()?;
assert_eq!(groups.len(), 3); // One group per week containing a Monday
assert_eq!(groups[0], vec![NaiveDate::from_ymd_opt(2023, 11, 20).unwrap()]); // Week 47
assert_eq!(groups[1], vec![NaiveDate::from_ymd_opt(2023, 11, 27).unwrap()]); // Week 48
assert_eq!(groups[2], vec![NaiveDate::from_ymd_opt(2023, 12, 4).unwrap()]); // Week 49
Ok(())
}
Sourcepub fn new(start_date_str: String, end_date_str: String, freq: DateFreq) -> Self
pub fn new(start_date_str: String, end_date_str: String, freq: DateFreq) -> Self
Creates a new BDatesList
instance defined by a start and end date.
§Arguments
start_date_str
- The inclusive start date as a string (e.g., “YYYY-MM-DD”).end_date_str
- The inclusive end date as a string (e.g., “YYYY-MM-DD”).freq
- The frequency for generating dates.
Sourcepub fn from_n_periods(
start_date_str: String,
freq: DateFreq,
n_periods: usize,
) -> Result<Self, Box<dyn Error>>
pub fn from_n_periods( start_date_str: String, freq: DateFreq, n_periods: usize, ) -> Result<Self, Box<dyn Error>>
Creates a new BDatesList
instance defined by a start date, frequency,
and the number of periods (dates) to generate.
This calculates the required dates using a BDatesGenerator
and determines
the effective end date based on the last generated date.
§Arguments
start_date_str
- The start date as a string (e.g., “YYYY-MM-DD”). The first generated date will be on or after this date.freq
- The frequency for generating dates.n_periods
- The exact number of business dates to generate according to the frequency.
§Errors
Returns an error if:
start_date_str
cannot be parsed.n_periods
is 0 (as this would result in an empty list and no defined end date).
Sourcepub fn list(&self) -> Result<Vec<NaiveDate>, Box<dyn Error>>
pub fn list(&self) -> Result<Vec<NaiveDate>, Box<dyn Error>>
Returns the flat list of business dates within the specified range and frequency.
The list is guaranteed to be sorted chronologically.
§Errors
Returns an error if the start or end date strings cannot be parsed.
Sourcepub fn count(&self) -> Result<usize, Box<dyn Error>>
pub fn count(&self) -> Result<usize, Box<dyn Error>>
Returns the count of business dates within the specified range and frequency.
§Errors
Returns an error if the start or end date strings cannot be parsed.
Sourcepub fn groups(&self) -> Result<Vec<Vec<NaiveDate>>, Box<dyn Error>>
pub fn groups(&self) -> Result<Vec<Vec<NaiveDate>>, Box<dyn Error>>
Returns a list of date lists, where each inner list contains dates belonging to the same period (determined by frequency).
The outer list (groups) is sorted chronologically by period, and the inner lists (dates within each period) are also sorted.
§Errors
Returns an error if the start or end date strings cannot be parsed.
Sourcepub fn start_date(&self) -> Result<NaiveDate, Box<dyn Error>>
pub fn start_date(&self) -> Result<NaiveDate, Box<dyn Error>>
Returns the start date parsed as a NaiveDate
.
§Errors
Returns an error if the start date string is not in “YYYY-MM-DD” format.
Sourcepub fn start_date_str(&self) -> &str
pub fn start_date_str(&self) -> &str
Returns the start date string.
Sourcepub fn end_date(&self) -> Result<NaiveDate, Box<dyn Error>>
pub fn end_date(&self) -> Result<NaiveDate, Box<dyn Error>>
Returns the end date parsed as a NaiveDate
.
§Errors
Returns an error if the end date string is not in “YYYY-MM-DD” format.
Sourcepub fn end_date_str(&self) -> &str
pub fn end_date_str(&self) -> &str
Returns the end date string.
Trait Implementations§
Source§impl Clone for BDatesList
impl Clone for BDatesList
Source§fn clone(&self) -> BDatesList
fn clone(&self) -> BDatesList
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more