Skip to main content

Period

Enum Period 

Source
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

Source

pub const ZERO: Self

The zero period — 0 Days.

Source

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);
Source

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());
Source

pub const fn normalized(self) -> Self

Canonicalize the period — 12 Months1 Year, 7 Days1 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));
Source

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);
Source

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

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)?);
Source§

type Output = Result<Date, TimeError>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Clone for Period

Source§

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

impl Copy for Period

Source§

impl Debug for Period

Source§

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

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

impl<'de> Deserialize<'de> for Period

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Period

Source§

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

Format as QuantLib does: 3M, 1Y, 2W, 14D. Zero-length periods format as 0D.

Source§

impl Eq for Period

Source§

impl From<Frequency> for Period

Source§

fn from(frequency: Frequency) -> Self

Map a Frequency to its canonical Period. Annual maps to 12 Months; call Period::normalized to get 1 Year.

Source§

impl Hash for Period

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<Period> for i32

Source§

fn mul(self, period: Period) -> Self::Output

Scalar multiplication with the scalar on the left.

Source§

type Output = Period

The resulting type after applying the * operator.
Source§

impl Mul<i32> for Period

Source§

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

Scale the length by n, preserving the unit. Wraps on overflow; use Period::checked_mul to detect it.

Source§

type Output = Period

The resulting type after applying the * operator.
Source§

impl Neg for Period

Source§

fn neg(self) -> Self::Output

Negate the length. Wraps on i32::MIN; use Period::checked_neg to detect overflow.

Source§

type Output = Period

The resulting type after applying the - operator.
Source§

impl PartialEq for Period

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl Serialize for Period

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

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.

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)?);
Source§

type Output = Result<Date, TimeError>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl TryFrom<Period> for Frequency

Source§

fn try_from(period: Period) -> Result<Self, Self::Error>

Map a Period to the canonical Frequency, normalizing first. Non-canonical, zero, and negative periods return TimeError::NonCanonicalPeriod.

Source§

type Error = TimeError

The type returned in the event of a conversion error.

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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.