pub struct DateDuration<C> where
    C: Calendar + ?Sized
{ pub years: i32, pub months: i32, pub weeks: i32, pub days: i32, pub marker: PhantomData<C>, }
Expand description

A duration between two dates

Can be used to perform date arithmetic

Example

use icu_calendar::{
    types::IsoWeekday, Date, DateDuration, DateDurationUnit,
};

// Creating ISO date: 1992-09-02.
let mut date_iso = Date::try_new_iso_date(1992, 9, 2)
    .expect("Failed to initialize ISO Date instance.");

assert_eq!(date_iso.day_of_week(), IsoWeekday::Wednesday);
assert_eq!(date_iso.year().number, 1992);
assert_eq!(date_iso.month().ordinal, 9);
assert_eq!(date_iso.day_of_month().0, 2);

// Answering questions about days in month and year.
assert_eq!(date_iso.days_in_year(), 366);
assert_eq!(date_iso.days_in_month(), 30);

// Advancing date in-place by 1 year, 2 months, 3 weeks, 4 days.
date_iso.add(DateDuration::new(1, 2, 3, 4));
assert_eq!(date_iso.year().number, 1993);
assert_eq!(date_iso.month().ordinal, 11);
assert_eq!(date_iso.day_of_month().0, 27);

// Reverse date advancement.
date_iso.add(DateDuration::new(-1, -2, -3, -4));
assert_eq!(date_iso.year().number, 1992);
assert_eq!(date_iso.month().ordinal, 9);
assert_eq!(date_iso.day_of_month().0, 2);

// Creating ISO date: 2022-01-30.
let newer_date_iso = Date::try_new_iso_date(2022, 1, 30)
    .expect("Failed to initialize ISO Date instance.");

// Comparing dates: 2022-01-30 and 1992-09-02.
let duration = newer_date_iso.until(
    &date_iso,
    DateDurationUnit::Years,
    DateDurationUnit::Days,
);
assert_eq!(duration.years, 30);
assert_eq!(duration.months, -8);
assert_eq!(duration.days, 28);

// Create new date with date advancement. Reassign to new variable.
let mutated_date_iso = date_iso.added(DateDuration::new(1, 2, 3, 4));
assert_eq!(mutated_date_iso.year().number, 1993);
assert_eq!(mutated_date_iso.month().ordinal, 11);
assert_eq!(mutated_date_iso.day_of_month().0, 27);

Fields

years: i32

The number of years

months: i32

The number of months

weeks: i32

The number of weeks

days: i32

The number of days

marker: PhantomData<C>

A marker for the calendar

Implementations

Construct a DateDuration

// two years, three months, and five days
let duration: DateDuration<Iso> = DateDuration::new(2, 3, 0, 5);

Explicitly cast duration to one for a different calendar

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.