Days In Month - parsicore/parsidate GitHub Wiki

Static Method days_in_month

Calculates the number of days in a specific month of a given Persian year.

Description

This static method determines the correct number of days for a given Persian month and year, adhering to the rules of the Solar Hijri (Persian) calendar. It specifically accounts for the variable length of the final month, Esfand, based on whether the provided year is a leap year.

The rules are:

  • Months 1 through 6 (Farvardin to Shahrivar) always contain 31 days.
  • Months 7 through 11 (Mehr to Bahman) always contain 30 days.
  • Month 12 (Esfand) contains 30 days if the specified year is a Persian leap year, and 29 days if it is a common (non-leap) year.

If the provided month number is outside the valid range of 1 to 12, the method returns 0.

Arguments

  • year: The Persian year (e.g., 1403) as an integer type (e.g., i32). This is used solely to determine if the year is a leap year when checking the length of Esfand (month 12).
  • month: The Persian month number, as an integer type (e.g., u8, u32), where 1 represents Farvardin and 12 represents Esfand. Must be between 1 and 12 (inclusive) for a valid result.

Returns

  • A u8 (or similar unsigned integer type) representing the number of days (29, 30, or 31) in the specified month and year.
  • Returns 0 if the provided month argument is less than 1 or greater than 12.

Note: This method performs a direct calculation based on calendar rules and does not return a Result or Err type.

Examples (Rust)

use parsidate::ParsiDate; // Assuming ParsiDate provides this static method

// --- Months 1-6 (Always 31 days) ---
assert_eq!(ParsiDate::days_in_month(1403, 1), 31); // Farvardin
assert_eq!(ParsiDate::days_in_month(1400, 6), 31); // Shahrivar (Year doesn't matter here)

// --- Months 7-11 (Always 30 days) ---
assert_eq!(ParsiDate::days_in_month(1403, 7), 30); // Mehr
assert_eq!(ParsiDate::days_in_month(1404, 11), 30); // Bahman (Year doesn't matter here)

// --- Month 12 (Esfand - Depends on Leap Year) ---
// 1403 is a Persian leap year
assert_eq!(ParsiDate::days_in_month(1403, 12), 30);

// 1404 is a common year
assert_eq!(ParsiDate::days_in_month(1404, 12), 29);

// 1399 is a Persian leap year
assert_eq!(ParsiDate::days_in_month(1399, 12), 30);

// 1400 is a common year
assert_eq!(ParsiDate::days_in_month(1400, 12), 29);

// --- Invalid Month Input ---
assert_eq!(ParsiDate::days_in_month(1403, 0), 0);  // Month 0 is invalid
assert_eq!(ParsiDate::days_in_month(1403, 13), 0); // Month 13 is invalid
assert_eq!(ParsiDate::days_in_month(1403, 15), 0); // Month 15 is invalid