pub struct DateUtils;Expand description
Utility functions for working with dates.
Implementations§
Source§impl DateUtils
impl DateUtils
Sourcepub fn date_only(app: &App, date: &DateTime) -> DateTime
pub fn date_only(app: &App, date: &DateTime) -> DateTime
Returns a DateTime with the date of the original, but time set to midnight.
Sourcepub fn dates_only(app: &App, range: &DateTimeRange) -> DateTimeRange
pub fn dates_only(app: &App, range: &DateTimeRange) -> DateTimeRange
Returns a DateTimeRange with the dates of the original, but with times set to
midnight.
See also: date_only, which does the same thing for a single date.
Sourcepub fn is_same_day(
app: &App,
date_a: Option<&DateTime>,
date_b: Option<&DateTime>,
) -> bool
pub fn is_same_day( app: &App, date_a: Option<&DateTime>, date_b: Option<&DateTime>, ) -> bool
Returns true if the two DateTime objects have the same day, month, and year, or are
both null.
Sourcepub fn is_same_month(
app: &App,
date_a: Option<&DateTime>,
date_b: Option<&DateTime>,
) -> bool
pub fn is_same_month( app: &App, date_a: Option<&DateTime>, date_b: Option<&DateTime>, ) -> bool
Returns true if the two DateTime objects have the same month and year, or are both
null.
Sourcepub fn month_delta(app: &App, start_date: &DateTime, end_date: &DateTime) -> i32
pub fn month_delta(app: &App, start_date: &DateTime, end_date: &DateTime) -> i32
Determines the number of months between two DateTime objects.
For example:
let date1 = DateTime::new(&app, 2019, 6, 15);
let date2 = DateTime::new(&app, 2020, 1, 15);
let difference = DateUtils::month_delta(&app, &date1, &date2);
assert_eq!(difference, 7);The days are not taken into account, so the difference is always an integer number of months.
Sourcepub fn add_months_to_month_date(
app: &App,
month_date: &DateTime,
months_to_add: i32,
) -> DateTime
pub fn add_months_to_month_date( app: &App, month_date: &DateTime, months_to_add: i32, ) -> DateTime
Returns a DateTime that is month_date with the added number of months and the day
set to 1 and time set to midnight.
For example:
let date = DateTime::new(&app, 2019, 1, 15);
let next_month = DateUtils::add_months_to_month_date(&app, &date, 1);
assert_eq!(next_month, DateTime::new(&app, 2019, 2, 1));month_date is assumed to already be on the first of the month.
Sourcepub fn add_days_to_date(app: &App, date: &DateTime, days: i32) -> DateTime
pub fn add_days_to_date(app: &App, date: &DateTime, days: i32) -> DateTime
Returns a DateTime with the added number of days and time set to midnight.
Sourcepub fn first_day_offset(
app: &App,
year: i32,
month: i32,
localizations: &dyn MaterialLocalizations,
) -> i32
pub fn first_day_offset( app: &App, year: i32, month: i32, localizations: &dyn MaterialLocalizations, ) -> i32
Computes the offset from the first day of the week that the first day of the month
falls on.
For example, September 1, 2017 falls on a Friday, which in the calendar localized for United States English appears as:
S M T W T F S
_ _ _ _ _ 1 2The offset for the first day of the months is the number of leading blanks in the calendar, i.e. 5.
The same date localized for the Russian calendar has a different offset, because the first day of week is Monday rather than Sunday:
M T W T F S S
_ _ _ _ 1 2 3So the offset is 4, rather than 5.
This code consolidates the following:
DateTime::weekdayprovides a 1-based index into days of week, with 1 falling on Monday.MaterialLocalizations::first_day_of_week_indexprovides a 0-based index into theMaterialLocalizations::narrow_weekdayslist.MaterialLocalizations::narrow_weekdayslist provides localized names of days of week, always starting with Sunday and ending with Saturday.
Sourcepub fn get_days_in_month(year: i32, month: i32) -> i32
pub fn get_days_in_month(year: i32, month: i32) -> i32
Returns the number of days in a month, according to the proleptic Gregorian calendar.
This applies the leap year logic introduced by the Gregorian reforms of 1582. It will not give valid results for dates prior to that time.