Skip to main content

DateDuration

Struct DateDuration 

Source
pub struct DateDuration {
    pub is_negative: bool,
    pub years: u32,
    pub months: u32,
    pub weeks: u32,
    pub days: u64,
}
Expand description

A signed length of time in terms of days, weeks, months, and years.

This type represents the abstract concept of a date duration. For example, a duration of “1 month” is represented as “1 month” in the data model, without any context of how many days the month might be.

Use DateDuration for calculating the difference between two Dates and adding date units to a Date.

§Example

use icu::calendar::options::DateDifferenceOptions;
use icu::calendar::types::DateDuration;
use icu::calendar::types::DateDurationUnit;
use icu::calendar::types::Weekday;
use icu::calendar::Date;

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

assert_eq!(date_iso.day_of_week(), Weekday::Wednesday);
assert_eq!(date_iso.era_year().year, 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
    .try_add_with_options(
        DateDuration {
            is_negative: false,
            years: 1,
            months: 2,
            weeks: 3,
            days: 4,
        },
        Default::default(),
    )
    .unwrap();
assert_eq!(date_iso.era_year().year, 1993);
assert_eq!(date_iso.month().ordinal, 11);
assert_eq!(date_iso.day_of_month().0, 27);

// Reverse date advancement.
date_iso
    .try_add_with_options(
        DateDuration {
            is_negative: true,
            years: 1,
            months: 2,
            weeks: 3,
            days: 4,
        },
        Default::default(),
    )
    .unwrap();
assert_eq!(date_iso.era_year().year, 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(2022, 10, 30)
    .expect("Failed to initialize ISO Date instance.");

// Comparing dates: 2022-01-30 and 1992-09-02.
let mut options = DateDifferenceOptions::default();
options.largest_unit = Some(DateDurationUnit::Years);
let Ok(duration) =
    newer_date_iso.try_until_with_options(&date_iso, options);
assert_eq!(duration.years, 30);
assert_eq!(duration.months, 1);
assert_eq!(duration.days, 28);

// Create new date with date advancement. Reassign to new variable.
let mutated_date_iso = date_iso
    .try_added_with_options(
        DateDuration {
            is_negative: false,
            years: 1,
            months: 2,
            weeks: 3,
            days: 4,
        },
        Default::default(),
    )
    .unwrap();
assert_eq!(mutated_date_iso.era_year().year, 1993);
assert_eq!(mutated_date_iso.month().ordinal, 11);
assert_eq!(mutated_date_iso.day_of_month().0, 27);

Currently unstable for ICU4X 1.0

🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, including in SemVer minor releases. Do not use this type unless you are prepared for things to occasionally break.

Graduation tracking issue: issue #3964.

Enabled with the unstable Cargo feature.

Fields§

§is_negative: bool

Whether the duration is negative.

A negative duration is an abstract concept that could result, for example, from taking the difference between two Dates.

The fields of the duration are either all positive or all negative. Mixed signs are not allowed.

By convention, this field should be false if the duration is zero.

§years: u32

The number of years

§months: u32

The number of months

§weeks: u32

The number of weeks

§days: u64

The number of days

Implementations§

Source§

impl DateDuration

Source

pub fn for_years(years: i32) -> Self

Returns a new DateDuration representing a number of years.

Source

pub fn for_months(months: i32) -> Self

Returns a new DateDuration representing a number of months.

Source

pub fn for_weeks(weeks: i32) -> Self

Returns a new DateDuration representing a number of weeks.

Source

pub fn for_days(days: i64) -> Self

Returns a new DateDuration representing a number of days.

Source

pub(crate) fn from_signed_ymwd( years: i64, months: i64, weeks: i64, days: i64, ) -> Self

Do NOT pass this function values of mixed signs!

Source

pub(crate) fn add_years_to(&self, year: i32) -> i32

Source

pub(crate) fn add_months_to(&self, month: u8) -> i64

Source

pub(crate) fn add_weeks_and_days_to(&self, day: u8) -> i64

Trait Implementations§

Source§

impl Clone for DateDuration

Source§

fn clone(&self) -> DateDuration

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DateDuration

Source§

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

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

impl Default for DateDuration

Source§

fn default() -> DateDuration

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

impl PartialEq for DateDuration

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for DateDuration

Source§

impl Eq for DateDuration

Source§

impl StructuralPartialEq for DateDuration

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 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> ToOwned for T
where T: Clone,

Source§

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

Source§

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 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> ErasedDestructor for T
where T: 'static,