ParsiDate: Comprehensive Persian Calendar Date & Time for Rust
parsidate provides comprehensive functionality for working with the Persian (Jalali/Shamsi) calendar system in Rust. It allows for seamless representation, conversion, validation, formatting, parsing, and arithmetic for naive dates (ParsiDate), naive date-times (ParsiDateTime), and timezone-aware date-times (ZonedParsiDateTime). It leverages the chrono crate for Gregorian representations and duration calculations.
✨ Features
- Three Core Types:
ParsiDate: A naive date (year, month, day).ParsiDateTime: A naive date and time (hour, minute, second).ZonedParsiDateTime: A timezone-aware date and time, handling DST and offsets correctly (requires thetimezonefeature).
- Conversion: Easily convert between
chronotypes (NaiveDate,NaiveDateTime) andparsidatetypes. - Validation: Robust validation to ensure all created dates and times are logically correct.
- Formatting: Display dates and times using
strftime-like patterns with Persian names (%B,%A), seasons (%K), and time components (%H,%M,%S,%T). - Parsing: Parse strings into
ParsiDateorParsiDateTimefrom various formats. - Arithmetic:
- Add or subtract days, months, and years, with correct handling of month lengths and leap years.
- Add or subtract
chrono::Durationfor precise time calculations.
- Date/Time Information: Get the Persian weekday, ordinal day, season, week number, and access individual date/time components.
- Helpers: Easily get the first/last day of the month, year, or season, or create modified dates/datetimes (
with_year,with_hour, etc.). - Current Time: Get the current system date (
ParsiDate::today()), naive datetime (ParsiDateTime::now()), or zoned datetime (ZonedParsiDateTime::now(tz)). - Serde Support: Optional serialization/deserialization for all date/time types via the
serdefeature. - Range: Supports Persian years from 1 to 9999.
⚙️ Installation & Features
Add parsidate to your Cargo.toml. You can enable features based on your needs.
[]
= "1.7.0"
# Add other dependencies as needed
= "0.4"
Available features:
serde(default): Enables serialization and deserialization support via theserdecrate.timezone: Enables theZonedParsiDateTimestruct and timezone functionality. Requires thechrono-tzcrate.
To enable specific features:
[]
# Example: Enable both serde and timezone support
= { = "1.7.0", = ["serde", "timezone"] }
# For timezone support, you also need chrono-tz
= "0.8"
The full feature enables all available features: parsidate = { version = "1.7.0", features = ["full"] }.
🚀 Usage Examples
Naive Dates (ParsiDate)
use ;
use NaiveDate;
// Creation and Validation
let pd = new.unwrap; // 2 Mordad 1403
assert!; // Invalid leap day
// Conversion
let g_date = from_ymd_opt.unwrap;
assert_eq!;
assert_eq!;
// Formatting & Parsing
assert_eq!;
assert_eq!;
// Arithmetic
let next_day = pd.add_days.unwrap;
assert_eq!;
Naive DateTimes (ParsiDateTime)
use ;
use Duration;
// Creation
let pdt = new.unwrap;
assert_eq!;
// Formatting & Parsing
assert_eq!;
assert_eq!;
// Arithmetic
let next_hour = pdt.add_duration.unwrap;
assert_eq!;
let next_day_dt = pdt.add_days.unwrap; // Preserves time
assert_eq!;
Timezone-Aware DateTimes (ZonedParsiDateTime)
This functionality requires the timezone feature.
// This example needs the `timezone` feature enabled for `parsidate`
// and the `chrono-tz` crate added to Cargo.toml.
Seasons and Other Helpers
use ;
let winter_date = new.unwrap; // Bahman 10th
assert_eq!;
assert_eq!;
// Get season boundaries
let end_of_winter = winter_date.end_of_season.unwrap; // 1403 is a leap year
assert_eq!;
Formatting and Parsing Specifiers
The library supports strftime-like specifiers for formatting and parsing.
| Specifier | Description | Example (1403-05-02, 15:30:45) |
Notes |
|---|---|---|---|
%Y |
Year with century | 1403 |
|
%m |
Month as zero-padded number | 05 |
|
%d |
Day of month as zero-padded number | 02 |
|
%B |
Full Persian month name | مرداد |
|
%A |
Full Persian weekday name | سهشنبه |
|
%w |
Weekday as number (Saturday=0) | 3 |
|
%j |
Day of year as zero-padded number | 126 |
|
%K |
Full Persian season name | تابستان |
|
%H |
Hour (24-hour clock), zero-padded | 15 |
ParsiDateTime |
%M |
Minute, zero-padded | 30 |
ParsiDateTime |
%S |
Second, zero-padded | 45 |
ParsiDateTime |
%T |
Equivalent to %H:%M:%S |
15:30:45 |
ParsiDateTime |
%W |
Week number of the year | 19 |
|
%% |
A literal % character |
% |
Note: Parsing requires an exact match to the format string. Specifiers like %A, %w, %j, %K, and %W are not supported for parsing.
⚠️ Error Handling
Most methods that can fail return a Result<T, DateError>. The DateError enum provides detailed information about the cause of failure, including InvalidDate, InvalidTime, ParseError(ParseErrorKind), GregorianConversionError, and ArithmeticOverflow.
Contributing
Contributions (bug reports, feature requests, pull requests) are welcome! Please open an issue to discuss significant changes first.
📄 License
Licensed under the Apache License, Version 2.0.
Version:1.7.0
Sign: parsidate-20250607-fea13e856dcd-459c6e73c83e49e10162ee28b26ac7cd