pub enum Period {
Days(i32),
Weeks(i32),
Months(i32),
Years(i32),
}Expand description
A signed duration tagged by its calendar unit.
Each variant carries an i32 length; the unit is the variant.
use fasti::Period;
let p = Period::Months(3);
match p {
Period::Days(n) => unreachable!("not days, n={n}"),
Period::Months(n) => assert_eq!(n, 3),
_ => unreachable!(),
}
// `12M` normalizes to `1Y`.
assert_eq!(Period::Months(12).normalized(), Period::Years(1));
// Scalar multiplication scales the length.
assert_eq!(Period::Months(3) * 4, Period::Months(12));Variants§
Days(i32)
Calendar days.
Weeks(i32)
Weeks — 7 calendar days, no calendar dependency.
Months(i32)
Calendar months — variable length (28..=31 days).
Years(i32)
Calendar years — variable length (365 or 366 days).
Implementations§
Source§impl Period
impl Period
Sourcepub const fn length(self) -> i32
pub const fn length(self) -> i32
The signed length component.
use fasti::Period;
assert_eq!(Period::Months(3).length(), 3);
assert_eq!(Period::Days(-7).length(), -7);Sourcepub const fn is_zero(self) -> bool
pub const fn is_zero(self) -> bool
true iff the period has zero length, regardless of unit.
use fasti::Period;
assert!(Period::ZERO.is_zero());
assert!(Period::Months(0).is_zero());
assert!(!Period::Days(1).is_zero());Sourcepub const fn normalized(self) -> Self
pub const fn normalized(self) -> Self
Canonicalize the period — 12 Months → 1 Year, 7 Days → 1 Week;
non-multiples are unchanged and zero normalizes to 0 Days.
use fasti::Period;
assert_eq!(Period::Months(24).normalized(), Period::Years(2));
assert_eq!(Period::Days(14).normalized(), Period::Weeks(2));
// Non-multiples stay put.
assert_eq!(Period::Months(5).normalized(), Period::Months(5));
// Zero normalizes to 0 Days regardless of input unit.
assert_eq!(Period::Years(0).normalized(), Period::Days(0));Sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Negate the length, returning None on overflow
(i32::MIN has no positive counterpart).
use fasti::Period;
assert_eq!(Period::Months(3).checked_neg(), Some(Period::Months(-3)));
assert_eq!(Period::Days(i32::MIN).checked_neg(), None);Sourcepub const fn checked_mul(self, n: i32) -> Option<Self>
pub const fn checked_mul(self, n: i32) -> Option<Self>
Scale the length by n, returning None on overflow.
use fasti::Period;
assert_eq!(Period::Months(3).checked_mul(4), Some(Period::Months(12)));
assert_eq!(Period::Days(i32::MAX).checked_mul(2), None);Trait Implementations§
Source§impl Add<Period> for Date
Step a Date forward by a Period. Returns
TimeError::DateOutOfRange for out-of-range results; Months/Years
clamp the day-of-month (see Date::add_months).
impl Add<Period> for Date
Step a Date forward by a Period. Returns
TimeError::DateOutOfRange for out-of-range results; Months/Years
clamp the day-of-month (see Date::add_months).
use fasti::{Date, Month, Period};
let d = Date::from_ymd(2026, Month::Jan, 15)?;
assert_eq!((d + Period::Months(6))?, Date::from_ymd(2026, Month::Jul, 15)?);
assert_eq!((d + Period::Years(1))?, Date::from_ymd(2027, Month::Jan, 15)?);
assert_eq!((d + Period::Days(7))?, Date::from_ymd(2026, Month::Jan, 22)?);
// Negative periods step backward.
assert_eq!((d + (-Period::Months(1)))?, Date::from_ymd(2025, Month::Dec, 15)?);impl Copy for Period
Source§impl<'de> Deserialize<'de> for Period
impl<'de> Deserialize<'de> for Period
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for Period
impl StructuralPartialEq for Period
Source§impl Sub<Period> for Date
Step a Date backward by a Period. Uses Period::checked_neg,
surfacing i32::MIN overflow as TimeError::DateOutOfRange.
impl Sub<Period> for Date
Step a Date backward by a Period. Uses Period::checked_neg,
surfacing i32::MIN overflow as TimeError::DateOutOfRange.
use fasti::{Date, Month, Period};
let d = Date::from_ymd(2026, Month::Jul, 15)?;
assert_eq!((d - Period::Months(6))?, Date::from_ymd(2026, Month::Jan, 15)?);