End Of Season - jalalvandi/ParsiDate GitHub Wiki

Method end_of_season

Calculates the date of the last day of the Persian season that this ParsiDate instance (self) falls into.

Description

This method determines the ending date of the Persian season (Bahar, Tabestan, Paeez, Zemestan) corresponding to the date represented by self.

The Persian seasons are defined as follows:

  • Bahar (Spring): Months 1 (Farvardin), 2 (Ordibehesht), 3 (Khordad)
  • Tabestan (Summer): Months 4 (Tir), 5 (Mordad), 6 (Shahrivar)
  • Paeez (Autumn): Months 7 (Mehr), 8 (Aban), 9 (Azar)
  • Zemestan (Winter): Months 10 (Dey), 11 (Bahman), 12 (Esfand)

The method calculates the end date by:

  1. Identifying the season based on the month of self.
  2. Determining the final month of that season (3, 6, 9, or 12).
  3. Calculating the number of days in that final month, using the year of self. This correctly accounts for the length of Esfand (29 or 30 days) based on whether the year is a leap year.
  4. Constructing a new ParsiDate with the same year as self, the final month of the season, and the last day of that month.

Arguments

This method takes no arguments other than the self reference to the ParsiDate instance it is called on.

Returns

  • Ok(ParsiDate): If the self instance represents a valid date, returns the ParsiDate corresponding to the last day of the season self falls into, wrapped in Ok.
  • Err(DateError::InvalidDate): If the self instance itself represents an invalid date according to Persian calendar rules. (Note: Creation of the resulting end-of-season date is generally expected to succeed if self is valid).

Examples (Rust)

use parsidate::{ParsiDate, DateError}; // Assuming these types exist

// --- Bahar (Spring) ---
// Ends on Khordad 31st (Month 3, Day 31)
let date_spring = ParsiDate::new(1403, 2, 15).unwrap(); // Ordibehesht 15th
let end_spring = date_spring.end_of_season().unwrap();
assert_eq!(end_spring, ParsiDate::new(1403, 3, 31).unwrap());

// --- Tabestan (Summer) ---
// Ends on Shahrivar 31st (Month 6, Day 31)
let date_summer = ParsiDate::new(1403, 6, 1).unwrap(); // Shahrivar 1st
let end_summer = date_summer.end_of_season().unwrap();
assert_eq!(end_summer, ParsiDate::new(1403, 6, 31).unwrap());

// --- Paeez (Autumn) ---
// Ends on Azar 30th (Month 9, Day 30)
let date_autumn = ParsiDate::new(1403, 8, 20).unwrap(); // Aban 20th
let end_autumn = date_autumn.end_of_season().unwrap();
assert_eq!(end_autumn, ParsiDate::new(1403, 9, 30).unwrap());

// --- Zemestan (Winter) - Leap Year ---
// 1403 is a leap year. Winter ends on Esfand 30th.
let date_winter_leap = ParsiDate::new(1403, 11, 5).unwrap(); // Bahman 5th, 1403
let end_winter_leap = date_winter_leap.end_of_season().unwrap();
assert_eq!(end_winter_leap, ParsiDate::new(1403, 12, 30).unwrap());

// Also test with a date in the last month of winter (leap year)
let date_winter_leap_last_month = ParsiDate::new(1403, 12, 10).unwrap(); // Esfand 10th, 1403
let end_winter_leap2 = date_winter_leap_last_month.end_of_season().unwrap();
assert_eq!(end_winter_leap2, ParsiDate::new(1403, 12, 30).unwrap());


// --- Zemestan (Winter) - Common Year ---
// 1404 is a common year. Winter ends on Esfand 29th.
let date_winter_common = ParsiDate::new(1404, 10, 1).unwrap(); // Dey 1st, 1404
let end_winter_common = date_winter_common.end_of_season().unwrap();
assert_eq!(end_winter_common, ParsiDate::new(1404, 12, 29).unwrap());

// Also test with a date in the last month of winter (common year)
let date_winter_common_last_month = ParsiDate::new(1404, 12, 29).unwrap(); // Esfand 29th, 1404
let end_winter_common2 = date_winter_common_last_month.end_of_season().unwrap();
assert_eq!(end_winter_common2, ParsiDate::new(1404, 12, 29).unwrap());