Struct icu::calendar::DateDuration

source ·
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§

source§

impl<C> DateDuration<C>where C: Calendar + ?Sized,

source

pub fn new(years: i32, months: i32, weeks: i32, days: i32) -> DateDuration<C>

Construct a DateDuration

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

pub fn cast_unit<C2>(self) -> DateDuration<C2>where C2: Calendar + ?Sized,

Explicitly cast duration to one for a different calendar

Trait Implementations§

source§

impl<C> Clone for DateDuration<C>where C: Clone + Calendar + ?Sized,

source§

fn clone(&self) -> DateDuration<C>

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<C> Debug for DateDuration<C>where C: Calendar,

source§

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

Formats the value using the given formatter. Read more
source§

impl<C> Default for DateDuration<C>where C: Calendar + ?Sized,

source§

fn default() -> DateDuration<C>

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

impl<C> PartialEq for DateDuration<C>where C: PartialEq + Calendar + ?Sized,

source§

fn eq(&self, other: &DateDuration<C>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<C> Copy for DateDuration<C>where C: Copy + Calendar + ?Sized,

source§

impl<C> Eq for DateDuration<C>where C: Eq + Calendar + ?Sized,

source§

impl<C> StructuralEq for DateDuration<C>where C: Calendar + ?Sized,

source§

impl<C> StructuralPartialEq for DateDuration<C>where C: Calendar + ?Sized,

Auto Trait Implementations§

§

impl<C: ?Sized> RefUnwindSafe for DateDuration<C>where C: RefUnwindSafe,

§

impl<C: ?Sized> Send for DateDuration<C>where C: Send,

§

impl<C: ?Sized> Sync for DateDuration<C>where C: Sync,

§

impl<C: ?Sized> Unpin for DateDuration<C>where C: Unpin,

§

impl<C: ?Sized> UnwindSafe for DateDuration<C>where C: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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

impl<T> ErasedDestructor for Twhere T: 'static,

source§

impl<T> MaybeSendSync for Twhere T: Send + Sync,