Struct BDatesList

Source
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.

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(())
}
Source

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.
Source

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).
Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn start_date_str(&self) -> &str

Returns the start date string.

Source

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.

Source

pub fn end_date_str(&self) -> &str

Returns the end date string.

Source

pub fn freq(&self) -> DateFreq

Returns the frequency enum.

Source

pub fn freq_str(&self) -> String

Returns the canonical string representation of the frequency.

Trait Implementations§

Source§

impl Clone for BDatesList

Source§

fn clone(&self) -> BDatesList

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BDatesList

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Broadcastable<T> for T
where T: Clone,

Source§

fn to_vec(&self, rows: usize, cols: usize) -> Vec<T>

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.