pub struct CalendarDuration { /* private fields */ }
Expand description

TODO Represent fraction of week with opt f32 see https://github.com/xwde/timext/issues/6


let d0 = Date::from_calendar_date(2023, Month::January, 31).unwrap();
let d1 = Date::from_calendar_date(2023, Month::February, 28).unwrap();
assert_eq!(d0 + 1.months(), d1);

let d0 = Date::from_calendar_date(2024, Month::February, 29).unwrap();
let d1 = Date::from_calendar_date(2025, Month::February, 28).unwrap();///
assert_eq!(d0 + 1.years(), d1);

Implementations§

source§

impl CalendarDuration

source

pub const fn new(years: i32, months: i32) -> Self

Creates a new CalendarDuration with provided years and months.

let d0 = CalendarDuration::new(2, 24);
assert_eq!(d0.whole_years(), 4);
assert_eq!(d0.whole_months(), 48);
source

pub const fn years(years: i32) -> Self

Creates a new CalendarDuration with provided years.

let d0 = CalendarDuration::years(2);
assert_eq!(d0.whole_years(), 2);
assert_eq!(d0.whole_months(), 24);
source

pub const fn months(months: i32) -> Self

Creates a new CalendarDuration with provided months.

let d0 = CalendarDuration::months(24);
assert_eq!(d0.whole_years(), 2);
assert_eq!(d0.whole_months(), 24);
source

pub const MIN: Self = _

source

pub const MAX: Self = _

source§

impl CalendarDuration

source

pub const fn whole_years(self) -> i32

Returns the number of whole years in the CalendarDuration.

assert_eq!(1.years().whole_years(), 1);
assert_eq!((-1).years().whole_years(), -1);
assert_eq!(6.months().whole_years(), 0);
assert_eq!((-6).months().whole_years(), 0);
source

pub const fn whole_months(self) -> i32

Returns the number of whole months in the CalendarDuration.

assert_eq!(1.months().whole_months(), 1);
assert_eq!((-1).months().whole_months(), -1);
assert_eq!(6.months().whole_years(), 0);
assert_eq!((-6).months().whole_years(), 0);
source

pub const fn subyear_months(self) -> i32

Returns the number of months past the number of whole years.

assert_eq!(13.months().subyear_months(), 1);
assert_eq!((-13).months().subyear_months(), -1);
source

pub const fn is_zero(self) -> bool

Checks if a duration is negative.

assert!(0.months().is_zero());
assert!(!1.months().is_zero());
source

pub const fn is_positive(self) -> bool

Checks if a CalendarDuration is positive.

assert!(1.months().is_positive());
assert!(!0.months().is_positive());
assert!(!(-1).months().is_positive());
source

pub const fn is_negative(self) -> bool

Checks if a CalendarDuration is negative.

assert!((-1).months().is_negative());
assert!(!0.months().is_negative());
assert!(!1.months().is_negative());
source§

impl CalendarDuration

source

pub const fn abs(self) -> Self

Returns the absolute value of the duration.

assert_eq!(1.months().abs(), 1.months());
assert_eq!(0.months().abs(), 0.months());
assert_eq!((-1).months().abs(), 1.months());
source§

impl CalendarDuration

source

pub fn checked_date_add(date: Date, duration: Self) -> Option<Date>

Returns the sum of provided Date and CalendarDuration.

let d0 = Date::from_calendar_date(2018, September, 1).unwrap();
let d1 = Date::from_calendar_date(2018, October, 1).unwrap();
let rs = CalendarDuration::checked_date_add(d0, 1.months());
assert_eq!(rs.unwrap(), d1);
source

pub fn checked_date_sub(date: Date, duration: Self) -> Option<Date>

Returns the difference of provided Date and CalendarDuration.

let d0 = Date::from_calendar_date(2018, September, 1).unwrap();
let d1 = Date::from_calendar_date(2018, August, 1).unwrap();
let rs = CalendarDuration::checked_date_sub(d0, 1.months());
assert_eq!(rs.unwrap(), d1);
source§

impl CalendarDuration

source

pub fn checked_add(self, rhs: Self) -> Option<Self>

Computes self + rhs, returning None if an overflow occurred.

assert_eq!(5.months().checked_add(5.months()), Some(10.months()));
assert_eq!(CalendarDuration::MAX.checked_add(1.months()), None);
assert_eq!((-5).months().checked_add(5.months()), Some(0.months()));
source

pub fn checked_sub(self, rhs: Self) -> Option<Self>

Computes self - rhs, returning None if an overflow occurred.

assert_eq!(5.months().checked_sub(5.months()), Some(0.months()));
assert_eq!(CalendarDuration::MIN.checked_sub(1.months()), None);
assert_eq!(5.months().checked_sub(5.months()), Some(0.months()));
source

pub fn checked_mul(self, rhs: i32) -> Option<Self>

Computes self * rhs, returning None if an overflow occurred.

assert_eq!(5.months().checked_mul(2), Some(10.months()));
assert_eq!(5.months().checked_mul(-2), Some((-10).months()));
assert_eq!(5.months().checked_mul(0), Some(0.months()));
assert_eq!(CalendarDuration::MAX.checked_mul(2), None);
assert_eq!(CalendarDuration::MIN.checked_mul(2), None);
source

pub fn checked_div(self, rhs: i32) -> Option<Self>

Computes self / rhs, returning None if rhs == 0 or if the result would overflow.

assert_eq!(10.months().checked_div(2), Some(5.months()));
assert_eq!(10.months().checked_div(-2), Some((-5).months()));
assert_eq!(1.months().checked_div(0), None);
source

pub fn checked_neg(self) -> Option<Self>

Computes -self, returning None if an overflow occurred.

assert_eq!(10.months().checked_neg(), Some((-10).months()));
assert_eq!(CalendarDuration::MIN.checked_neg(), None);

Trait Implementations§

source§

impl Add<CalendarDuration> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
source§

impl Add<CalendarDuration> for Date

§

type Output = Date

The resulting type after applying the + operator.
source§

fn add(self, rhs: CalendarDuration) -> Self::Output

Performs the + operation. Read more
source§

impl Add<CalendarDuration> for PrimitiveDateTime

§

type Output = PrimitiveDateTime

The resulting type after applying the + operator.
source§

fn add(self, rhs: CalendarDuration) -> Self::Output

Performs the + operation. Read more
source§

impl AddAssign<CalendarDuration> for CalendarDuration

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl AddAssign<CalendarDuration> for Date

source§

fn add_assign(&mut self, rhs: CalendarDuration)

Performs the += operation. Read more
source§

impl AddAssign<CalendarDuration> for PrimitiveDateTime

source§

fn add_assign(&mut self, rhs: CalendarDuration)

Performs the += operation. Read more
source§

impl Clone for CalendarDuration

source§

fn clone(&self) -> CalendarDuration

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 Debug for CalendarDuration

source§

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

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

impl Default for CalendarDuration

source§

fn default() -> CalendarDuration

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

impl Display for CalendarDuration

source§

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

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

impl Div<i16> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the / operator.
source§

fn div(self, rhs: i16) -> Self::Output

Performs the / operation. Read more
source§

impl Div<i32> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the / operator.
source§

fn div(self, rhs: i32) -> Self::Output

Performs the / operation. Read more
source§

impl Div<i8> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the / operator.
source§

fn div(self, rhs: i8) -> Self::Output

Performs the / operation. Read more
source§

impl Div<u16> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the / operator.
source§

fn div(self, rhs: u16) -> Self::Output

Performs the / operation. Read more
source§

impl Div<u32> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the / operator.
source§

fn div(self, rhs: u32) -> Self::Output

Performs the / operation. Read more
source§

impl Div<u8> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the / operator.
source§

fn div(self, rhs: u8) -> Self::Output

Performs the / operation. Read more
source§

impl DivAssign<i16> for CalendarDuration

source§

fn div_assign(&mut self, rhs: i16)

Performs the /= operation. Read more
source§

impl DivAssign<i32> for CalendarDuration

source§

fn div_assign(&mut self, rhs: i32)

Performs the /= operation. Read more
source§

impl DivAssign<i8> for CalendarDuration

source§

fn div_assign(&mut self, rhs: i8)

Performs the /= operation. Read more
source§

impl DivAssign<u16> for CalendarDuration

source§

fn div_assign(&mut self, rhs: u16)

Performs the /= operation. Read more
source§

impl DivAssign<u32> for CalendarDuration

source§

fn div_assign(&mut self, rhs: u32)

Performs the /= operation. Read more
source§

impl DivAssign<u8> for CalendarDuration

source§

fn div_assign(&mut self, rhs: u8)

Performs the /= operation. Read more
source§

impl Hash for CalendarDuration

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Mul<CalendarDuration> for i16

§

type Output = CalendarDuration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: CalendarDuration) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<CalendarDuration> for i32

§

type Output = CalendarDuration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: CalendarDuration) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<CalendarDuration> for i8

§

type Output = CalendarDuration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: CalendarDuration) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<CalendarDuration> for u16

§

type Output = CalendarDuration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: CalendarDuration) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<CalendarDuration> for u32

§

type Output = CalendarDuration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: CalendarDuration) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<CalendarDuration> for u8

§

type Output = CalendarDuration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: CalendarDuration) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<i16> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: i16) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<i32> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: i32) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<i8> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: i8) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<u16> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: u16) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<u32> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: u32) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<u8> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the * operator.
source§

fn mul(self, rhs: u8) -> Self::Output

Performs the * operation. Read more
source§

impl MulAssign<i16> for CalendarDuration

source§

fn mul_assign(&mut self, rhs: i16)

Performs the *= operation. Read more
source§

impl MulAssign<i32> for CalendarDuration

source§

fn mul_assign(&mut self, rhs: i32)

Performs the *= operation. Read more
source§

impl MulAssign<i8> for CalendarDuration

source§

fn mul_assign(&mut self, rhs: i8)

Performs the *= operation. Read more
source§

impl MulAssign<u16> for CalendarDuration

source§

fn mul_assign(&mut self, rhs: u16)

Performs the *= operation. Read more
source§

impl MulAssign<u32> for CalendarDuration

source§

fn mul_assign(&mut self, rhs: u32)

Performs the *= operation. Read more
source§

impl MulAssign<u8> for CalendarDuration

source§

fn mul_assign(&mut self, rhs: u8)

Performs the *= operation. Read more
source§

impl Neg for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl Ord for CalendarDuration

source§

fn cmp(&self, other: &CalendarDuration) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<CalendarDuration> for CalendarDuration

source§

fn eq(&self, other: &CalendarDuration) -> 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 PartialOrd<CalendarDuration> for CalendarDuration

source§

fn partial_cmp(&self, other: &CalendarDuration) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Sub<CalendarDuration> for CalendarDuration

§

type Output = CalendarDuration

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
source§

impl Sub<CalendarDuration> for Date

§

type Output = Date

The resulting type after applying the - operator.
source§

fn sub(self, rhs: CalendarDuration) -> Self::Output

Performs the - operation. Read more
source§

impl Sub<CalendarDuration> for PrimitiveDateTime

§

type Output = PrimitiveDateTime

The resulting type after applying the - operator.
source§

fn sub(self, rhs: CalendarDuration) -> Self::Output

Performs the - operation. Read more
source§

impl SubAssign<CalendarDuration> for CalendarDuration

source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
source§

impl SubAssign<CalendarDuration> for Date

source§

fn sub_assign(&mut self, rhs: CalendarDuration)

Performs the -= operation. Read more
source§

impl SubAssign<CalendarDuration> for PrimitiveDateTime

source§

fn sub_assign(&mut self, rhs: CalendarDuration)

Performs the -= operation. Read more
source§

impl Copy for CalendarDuration

source§

impl Eq for CalendarDuration

source§

impl StructuralEq for CalendarDuration

source§

impl StructuralPartialEq for CalendarDuration

Auto Trait Implementations§

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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.
const: unstable · 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.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.