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: i32The multiplier (may be negative to express a backward tenor).
unit: PeriodEnumThe time unit.
Implementations§
§impl Period
impl Period
pub fn new(multiplier: i32, unit: PeriodEnum) -> Self
pub fn new(multiplier: i32, unit: PeriodEnum) -> Self
Constructs a period from a multiplier and unit.
pub fn in_months(self) -> Option<i32>
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>
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
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>
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)>
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)
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>
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 Copy for Period
impl Eq for Period
§impl FromStr for Period
impl FromStr for Period
§fn from_str(s: &str) -> Result<Self, Self::Err>
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
type Err = VocabularyParseError
§impl PartialEq for Period
impl PartialEq for Period
§impl PartialOrd for Period
impl PartialOrd for Period
§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
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.