Skip to main content

DateUtils

Struct DateUtils 

Source
pub struct DateUtils;
Expand description

Utility functions for working with dates.

Implementations§

Source§

impl DateUtils

Source

pub fn date_only(app: &App, date: &DateTime) -> DateTime

Returns a DateTime with the date of the original, but time set to midnight.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 2

The 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 3

So the offset is 4, rather than 5.

This code consolidates the following:

Source

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.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,