Skip to main content

ExpirationDate

Enum ExpirationDate 

Source
pub enum ExpirationDate {
    Days(Positive),
    DateTime(DateTime<Utc>),
}
Expand description

Represents the expiration of a financial instrument.

Variants§

§

Days(Positive)

Relative expiration in positive fractional days.

§

DateTime(DateTime<Utc>)

Absolute expiration point in UTC.

Implementations§

Source§

impl ExpirationDate

Source

pub fn get_years_with_convention<C>( &self, convention: C, ) -> Result<Positive, ExpirationDateError>
where C: DayCount,

Calculates years using a specific day count convention.

§Errors

Returns ExpirationDateError if calculation fails.

Source

pub fn get_years(&self) -> Result<Positive, ExpirationDateError>

Calculates years until expiration using standard Actual/365 Fixed.

§Errors

Returns ExpirationDateError if calculation fails.

§Examples
use chrono::{Duration, Utc};
use positive::{assert_pos_relative_eq, pos_or_panic, Positive};
use expiration_date::ExpirationDate;

let days = pos_or_panic!(365.0);
let expiration_date_days = ExpirationDate::Days(days);
let years = expiration_date_days.get_years().unwrap();
assert_pos_relative_eq!(years, Positive::ONE, pos_or_panic!(0.001));

let datetime = Utc::now() + Duration::days(365);
let expiration_date_datetime = ExpirationDate::DateTime(datetime);
let years = expiration_date_datetime.get_years().unwrap();
assert_pos_relative_eq!(years, Positive::ONE, pos_or_panic!(0.01));
Source

pub fn get_days(&self) -> Result<Positive, ExpirationDateError>

Returns the number of fractional days until expiration.

§Errors

Returns ExpirationDateError if conversion to Positive fails.

Source

pub fn get_date(&self) -> Result<DateTime<Utc>, ExpirationDateError>

Resolves expiration to an absolute DateTime<Utc>.

§Errors

Returns ExpirationDateError if construction fails.

§Examples
use chrono::{Duration, Utc};
use positive::pos_or_panic;
use expiration_date::ExpirationDate;

// Clear any stored reference datetime from previous tests so the
// relative calculation uses `Utc::now()` as its base.
ExpirationDate::set_reference_datetime(None);

let days = pos_or_panic!(30.0);
let expiration_date_days = ExpirationDate::Days(days);
let future_date = Utc::now() + Duration::days(30);
let calculated_date = expiration_date_days.get_date().unwrap();
assert_eq!(calculated_date.date_naive(), future_date.date_naive());

let datetime = Utc::now() + Duration::days(365);
let expiration_date_datetime = ExpirationDate::DateTime(datetime);
let stored_date = expiration_date_datetime.get_date().unwrap();
assert_eq!(stored_date, datetime);
Source

pub fn get_date_with_options( &self, use_fixed_time: bool, ) -> Result<DateTime<Utc>, ExpirationDateError>

Resolves datetime with advanced base-time options.

§Errors

Returns ExpirationDateError if the construction fails.

Source

pub fn get_date_string(&self) -> Result<String, ExpirationDateError>

Returns the expiration date as a formatted YYYY-MM-DD string.

§Errors

Returns error if the date cannot be resolved.

§Examples
use positive::pos_or_panic;
use expiration_date::ExpirationDate;

let days = pos_or_panic!(30.0);
let expiration_date = ExpirationDate::Days(days);
let date_string = expiration_date.get_date_string().unwrap();
assert!(date_string.len() == 10); // YYYY-MM-DD format
Source§

impl ExpirationDate

Source

pub fn from_string(s: &str) -> Result<ExpirationDate, ExpirationDateError>

Parse expiration from string using multiple formats.

Supports the following input shapes:

  1. Positive number of days: e.g., "30.0"
  2. ISO date: e.g., "2025-01-01"
  3. Date with time and timezone: e.g., "2025-01-01 12:00:00 UTC"
  4. RFC3339-like with time: e.g., "2025-05-23T15:29" or "2025-05-23T15:29:00Z"
  5. Numeric date (YYYYMMDD): e.g., "20250101"
  6. Common date formats: e.g., "01-01-2025", "30 jan 2025", "30-jan-2025", "30 january 2025", "30-january-2025"

Month names are matched case-insensitively.

§Errors

Returns ExpirationDateError::ParseError if format matches no known pattern, or ExpirationDateError::InvalidDateTime if the derived time components are out of range.

§Examples
use expiration_date::ExpirationDate;
use positive::pos_or_panic;

let exp_days = ExpirationDate::from_string("365").unwrap();
assert_eq!(exp_days, ExpirationDate::Days(pos_or_panic!(365.0)));

let exp_date = ExpirationDate::from_string("2025-12-31").unwrap();
Source

pub fn from_string_to_days( s: &str, ) -> Result<ExpirationDate, ExpirationDateError>

Parses string and converts result to Days variant.

§Errors

Returns error if parsing or conversion fails.

Source§

impl ExpirationDate

Source

pub fn set_reference_datetime(dt: Option<DateTime<Utc>>)

Sets the reference datetime for Days variant calculations.

Source

pub fn get_reference_datetime() -> Option<DateTime<Utc>>

Gets the current reference datetime.

Trait Implementations§

Source§

impl Clone for ExpirationDate

Source§

fn clone(&self) -> ExpirationDate

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 ExpirationDate

Source§

impl Debug for ExpirationDate

Source§

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

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

impl Default for ExpirationDate

Source§

fn default() -> ExpirationDate

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

impl<'de> Deserialize<'de> for ExpirationDate

Source§

fn deserialize<D>( deserializer: D, ) -> Result<ExpirationDate, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

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

impl Display for ExpirationDate

Source§

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

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

impl Eq for ExpirationDate

Source§

impl Hash for ExpirationDate

Source§

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

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 Ord for ExpirationDate

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

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

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for ExpirationDate

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl PartialOrd for ExpirationDate

Source§

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

This method returns an ordering between self and other values if one exists. Read more
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
Source§

impl Serialize for ExpirationDate

Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

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

impl ToSchema for ExpirationDate

Source§

fn name() -> Cow<'static, str>

Return name of the schema. Read more
Source§

fn schemas(schemas: &mut Vec<(String, RefOr<Schema>)>)

Implement reference utoipa::openapi::schema::Schemas for this type. 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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PartialSchema for T
where T: ComposeSchema + ?Sized,

Source§

fn schema() -> RefOr<Schema>

Return ref or schema of implementing type that can then be used to construct combined schemas.
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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more