Crate date_calculations

Source
Expand description

This crate provides helper functions for calculating shifts in Chrono’s NaiveDate values for various periods (week, month, quarter, year) for common shifts in direction (beginning_of_, end_of_, previous_, and next_).

The dates passed to these functions should be Gregorian dates to ensure proper calcuation.

use chrono::prelude::*;
use date_calculations::*;

let twenty_twenty_one = NaiveDate::from_ymd_opt(2021, 1, 31).unwrap();

assert_eq!(next_year(&twenty_twenty_one).unwrap().year(), 2022);
assert_eq!(next_year(&twenty_twenty_one).unwrap().month(), 1);
assert_eq!(next_year(&twenty_twenty_one).unwrap().day(), 1);

assert_eq!(previous_quarter(&twenty_twenty_one).unwrap().year(), 2020);
assert_eq!(previous_quarter(&twenty_twenty_one).unwrap().month(), 10);
assert_eq!(previous_quarter(&twenty_twenty_one).unwrap().day(), 1);

Functions§

beginning_of_month
Returns the first day of the current month and year.
beginning_of_quarter
Returns the first day of the current quarter and year.
beginning_of_week
Returns the beginning of the week relative to the provided date.
beginning_of_year
Returns the first day of the year (January 1) of the current year.
end_of_month
Returns the last day of the current month and year.
end_of_quarter
Returns the last day of the current quarter and year.
end_of_week
Returns the end of the week relative to the provided date.
end_of_year
Returns the last day of the year (December 31) of the current year.
next_month
Returns the first day of the next month.
next_quarter
Returns the first day of the next quarter.
next_week
Returns the beginning of the next week.
next_year
Returns the first day of the year (January 1) of the next year.
previous_month
Returns the first day of the previous month.
previous_quarter
Returns the first day of the previous quarter.
previous_week
Returns the end of the next week.
previous_year
Returns the first day of the year (January 1) of the previous year.