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
Graduation tracking issue: issue #3964.
✨ Enabled with the unstable Cargo feature.
Fields§
§is_negative: boolWhether 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: u32The number of years
months: u32The number of months
weeks: u32The number of weeks
days: u64The number of days
Implementations§
Source§impl DateDuration
impl DateDuration
Sourcepub fn for_years(years: i32) -> Self
pub fn for_years(years: i32) -> Self
Returns a new DateDuration representing a number of years.
Sourcepub fn for_months(months: i32) -> Self
pub fn for_months(months: i32) -> Self
Returns a new DateDuration representing a number of months.
Sourcepub fn for_weeks(weeks: i32) -> Self
pub fn for_weeks(weeks: i32) -> Self
Returns a new DateDuration representing a number of weeks.
Sourcepub fn for_days(days: i64) -> Self
pub fn for_days(days: i64) -> Self
Returns a new DateDuration representing a number of days.
Sourcepub(crate) fn from_signed_ymwd(
years: i64,
months: i64,
weeks: i64,
days: i64,
) -> Self
pub(crate) fn from_signed_ymwd( years: i64, months: i64, weeks: i64, days: i64, ) -> Self
Do NOT pass this function values of mixed signs!
pub(crate) fn add_years_to(&self, year: i32) -> i32
pub(crate) fn add_months_to(&self, month: u8) -> i64
pub(crate) fn add_weeks_and_days_to(&self, day: u8) -> i64
Trait Implementations§
Source§impl Clone for DateDuration
impl Clone for DateDuration
Source§fn clone(&self) -> DateDuration
fn clone(&self) -> DateDuration
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for DateDuration
impl Debug for DateDuration
Source§impl Default for DateDuration
impl Default for DateDuration
Source§fn default() -> DateDuration
fn default() -> DateDuration
Source§impl PartialEq for DateDuration
impl PartialEq for DateDuration
Source§fn eq(&self, other: &DateDuration) -> bool
fn eq(&self, other: &DateDuration) -> bool
self and other values to be equal, and is used by ==.