Skip to main content

Period

Struct Period 

pub struct Period {
    pub multiplier: i32,
    pub unit: PeriodEnum,
}
Expand description

A tenor: an integer multiplier and a PeriodEnum unit.

Equality, hashing, and ordering are semantic (see the module docs), so this type deliberately does not derive PartialEq/Eq/Hash/Ord. Compare multiplier and unit directly if you need structural equality.

Fields§

§multiplier: i32

The multiplier (may be negative to express a backward tenor).

§unit: PeriodEnum

The time unit.

Implementations§

§

impl Period

pub fn new(multiplier: i32, unit: PeriodEnum) -> Self

Constructs a period from a multiplier and unit.

pub fn in_months(self) -> Option<i32>

Length in whole months for Month/Year tenors. Returns None for Day/Week/Term, which have no exact month equivalent.

pub fn in_days(self) -> Option<i32>

Length in whole days for Day/Week tenors. Returns None for Month/Year/Term, which have no exact day equivalent.

pub fn normalize(self) -> Period

Re-expresses the tenor in the largest unit that represents it exactly, without changing its meaning. Months that are an exact multiple of 12 become Years; days that are an exact multiple of 7 become Weeks; any zero-length tenor becomes 0D; Term is returned unchanged.

pub fn compare(self, other: Period) -> Result<Ordering, PeriodError>

The order of two tenors by duration, or PeriodError::Unordered where PartialOrd has none — the refusal a caller that must order them reports.

§Errors

PeriodError::Unordered for overlapping day spans or a Term.

fn day_bounds(self) -> Option<(i64, i64)>

Inclusive [min, max] span in days, or None for Term (unbounded). Months span 28–31 days and years 365–366; Day/Week are exact. For a negative multiplier the bounds are negated and reordered so min <= max.

fn key(self) -> (i32, char)

The canonical (multiplier, unit-code) key used for semantic equality and hashing — two tenors share a key iff they denote the same duration.

fn combine(self, other: Period, sign: i32) -> Result<Period, PeriodError>

Combines two periods (sign is +1 for add, -1 for subtract). Closed within the Month/Year and Week/Day families; cross-family or Term operands yield PeriodError::IncompatibleUnits. The result is normalized.

Trait Implementations§

§

impl Add for Period

§

type Output = Result<Period, PeriodError>

The resulting type after applying the + operator.
§

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

Performs the + operation. Read more
§

impl Clone for Period

§

fn clone(&self) -> Period

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
§

impl Copy for Period

§

impl Debug for Period

§

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

Formats the value using the given formatter. Read more
§

impl Display for Period

§

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

Formats the value using the given formatter. Read more
§

impl Eq for Period

§

impl FromStr for Period

§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a compact tenor such as "3M", "-1Y", "2W", "7D", or "1T", or a compound one in descending units: "1Y3M" is 15M and "2W3D" is 17D, a leading sign applying to the whole. Months and days do not combine into one period, so a spelling mixing the two families is refused, as is a repeated or ascending unit.

§

type Err = VocabularyParseError

The associated error which can be returned from parsing.
§

impl Hash for Period

§

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

Hashes the canonical form, so equal tenors hash equally.

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
§

impl PartialEq for Period

§

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

Two tenors are equal when they denote the same duration, regardless of the unit they are written in (12M == 1Y, 7D == 1W). Never panics.

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

impl PartialOrd for Period

§

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

Orders by duration using day bounds. Returns None when the comparison is undecidable — the day-bound ranges overlap without being equal (e.g. 1M vs 30D), or either operand is Term.

1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

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

impl Sub for Period

§

type Output = Result<Period, PeriodError>

The resulting type after applying the - operator.
§

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

Performs the - operation. Read more

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.