ExpirationDate
A standalone crate for handling financial instrument expiration dates in Rust.
Overview
ExpirationDate is a Rust library that provides a flexible enum for representing
expiration dates of financial instruments such as options, futures, and other derivatives.
It supports two representations:
- Days: A positive number of days from the current date using
Positive - DateTime: An absolute point in time using
chrono::DateTime<Utc>
This is particularly useful in quantitative finance applications where expiration dates need to be expressed either as days-to-expiration (DTE) or as specific calendar dates.
Minimum Supported Rust Version
expiration_date requires Rust 1.85 or later (Edition 2024).
Features
- Dual Representation: Express expirations as days remaining or absolute datetimes
- Time-to-Expiration: Calculate expiration in fractional years for pricing models
- Multiple Parse Formats: Parse dates from RFC3339, YYYYMMDD, DD-MM-YYYY, DD-Mon-YYYY, and more
- Prelude Module: Simple imports with
use expiration_date::prelude::*; - Serde Support: Full serialization/deserialization support for JSON and other formats
- Custom Ordering: Compare and sort expirations regardless of variant type
- Reference DateTime: Thread-local reference time support for deterministic testing
- Optional utoipa Integration: OpenAPI schema generation support via feature flag
Installation
Add this to your Cargo.toml:
[]
= "0.1"
To enable OpenAPI schema support:
[]
= { = "0.1", = ["utoipa"] }
Quick Start
use ExpirationDate;
use pos_or_panic;
// Create from days
let exp = Days;
let years = exp.get_years.unwrap;
// Create from a specific datetime
use ;
let dt = Utc.with_ymd_and_hms.unwrap;
let exp = DateTime;
// Parse from string (supports multiple formats)
let exp = from_string.unwrap;
let exp = from_string.unwrap;
let exp = from_string.unwrap;
// Get expiration as fractional years (for Black-Scholes, etc.)
let t = exp.get_years.unwrap;
API Overview
Creation
use ExpirationDate;
use pos_or_panic;
use ;
// From days to expiration
let exp = Days;
// From absolute datetime
let dt = Utc.with_ymd_and_hms.unwrap;
let exp = DateTime;
// From string (auto-detects format)
let exp = from_string.unwrap;
let exp = from_string.unwrap; // Parsed as days
// Default (365 days)
let exp = default;
Time Calculations
use ExpirationDate;
use pos_or_panic;
let exp = Days;
// Get time to expiration in years (days / 365)
let years = exp.get_years.unwrap; // ~0.5
// Get number of days as Positive
let days = exp.get_days.unwrap;
// Get the resolved DateTime<Utc>
let date = exp.get_date.unwrap;
// Get date as formatted string (YYYY-MM-DD)
let date_str = exp.get_date_string.unwrap;
String Parsing
ExpirationDate::from_string supports multiple date formats:
| Format | Example |
|---|---|
| Numeric (days) | "30.0" |
| ISO 8601 / RFC3339 | "2025-12-31T00:00:00Z" |
| ISO 8601 no seconds | "2025-12-31T15:29" |
| Date only | "2025-12-31" |
| YYYYMMDD | "20251231" |
| DD-MM-YYYY | "31-12-2025" |
| DD Mon YYYY | "31 dec 2025" |
| DD-Mon-YYYY | "31-dec-2025" |
Comparison and Ordering
Expiration dates can be compared across variant types. Mixed comparisons normalize both sides to days for consistent ordering:
use ExpirationDate;
use pos_or_panic;
use ;
let thirty_days = Days;
let sixty_days = Days;
assert!;
// Sort a collection of mixed variants
let mut expirations = vec!;
expirations.sort;
Serialization
use ExpirationDate;
use pos_or_panic;
let exp = Days;
let json = to_string.unwrap; // {"days":30.0}
let parsed: ExpirationDate = from_str.unwrap;
Error Handling
The library provides ExpirationDateError for comprehensive error handling:
use ExpirationDate;
use ExpirationDateError;
let result = from_string;
assert!;
Error variants include:
ParseError- Failed to parse a date stringConversionError- Failed to convert between representationsInvalidDateTime- Invalid datetime value encounteredPositiveError- Error from the underlyingPositivetypeChronoParseError- Error from chrono date parsingParseIntError- Error parsing integer components
Use Cases
- Options Pricing: Express time-to-expiration for Black-Scholes and other models
- Risk Management: Track and compare expiration dates across portfolios
- Trading Systems: Parse expiration dates from various exchange formats
- Financial Data: Serialize/deserialize expiration dates in APIs and databases
License
This project is licensed under the MIT License.
Contribution and Contact
We welcome contributions to this project! If you would like to contribute, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and ensure that the project still builds and all tests pass.
- Commit your changes and push your branch to your forked repository.
- Submit a pull request to the main repository.
Development
- Run tests:
make test - Lint:
make lint - Format:
make fmt - Run benchmarks:
make bench(wrapscargo bench --all-features; Criterion benches live underbenches/)
If you have any questions, issues, or would like to provide feedback, please feel free to contact the project maintainer:
Contact Information
- Author: Joaquín Béjar García
- Email: jb@taunais.com
- Telegram: @joaquin_bejar
- Repository: https://github.com/joaquinbejar/ExpirationDate
- Documentation: https://docs.rs/expiration_date
We appreciate your interest and look forward to your contributions!
✍️ License
Licensed under MIT license