Crate parsidate

Source
Expand description

§ParsiDate: Persian (Jalali) Calendar Implementation in Rust

This crate provides comprehensive functionality for working with the Persian (Jalali) calendar system. It allows for:

  • Conversion: Seamlessly convert dates between the Gregorian and Persian calendars.
  • Validation: Check if a given year, month, and day combination forms a valid Persian date.
  • Formatting: Display Persian dates in various common formats (e.g., “YYYY/MM/DD”, “D MMMM YYYY”, “YYYY-MM-DD”).
  • Date Arithmetic: Calculate the number of days between two Persian dates.
  • Leap Year Calculation: Determine if a Persian or Gregorian year is a leap year.
  • Weekday Calculation: Find the Persian name for the day of the week.

It relies on the chrono crate for underlying Gregorian date representations and calculations.

§Examples

use chrono::NaiveDate;
use parsidate::{ParsiDate, DateError};

// Convert Gregorian to Persian
let gregorian_date = NaiveDate::from_ymd_opt(2024, 7, 23).unwrap();
let persian_date = ParsiDate::from_gregorian(gregorian_date).unwrap();
assert_eq!(persian_date.year, 1403);
assert_eq!(persian_date.month, 5); // Mordad
assert_eq!(persian_date.day, 2);

// Convert Persian to Gregorian
let p_date = ParsiDate { year: 1403, month: 1, day: 1 }; // Farvardin 1st, 1403
let g_date = p_date.to_gregorian().unwrap();
assert_eq!(g_date, NaiveDate::from_ymd_opt(2024, 3, 20).unwrap()); // March 20th, 2024 (Persian New Year)

// Formatting
assert_eq!(persian_date.format("short"), "1403/05/02");
assert_eq!(persian_date.format("long"), "2 مرداد 1403");
assert_eq!(persian_date.format("iso"), "1403-05-02");

// Validation
assert!(ParsiDate { year: 1403, month: 12, day: 30 }.is_valid()); // 1403 is a leap year
assert!(!ParsiDate { year: 1404, month: 12, day: 30 }.is_valid());// 1404 is not a leap year
assert!(!ParsiDate { year: 1403, month: 13, day: 1 }.is_valid()); // Invalid month

// Weekday
assert_eq!(persian_date.weekday(), "سه‌شنبه"); // Tuesday

// Days Between
let date1 = ParsiDate { year: 1403, month: 5, day: 2 };
let date2 = ParsiDate { year: 1403, month: 5, day: 12 };
assert_eq!(date1.days_between(&date2), 10);

Structs§

ParsiDate
Represents a date in the Persian (Jalali or Shamsi) calendar system.

Enums§

DateError
Enumerates potential errors during date operations within the parsidate crate.