Season - parsicore/parsidate GitHub Wiki
season
Method Returns the Persian season (بهار, تابستان, پاییز, زمستان) corresponding to this ParsiDate
instance.
Description
This method determines which of the four standard Persian seasons the date represented by the ParsiDate
instance falls into, based solely on its month component.
The seasons are defined according to the following month ranges:
- Bahar (Spring): Months 1, 2, 3 (Farvardin, Ordibehesht, Khordad)
- Tabestan (Summer): Months 4, 5, 6 (Tir, Mordad, Shahrivar)
- Paeez (Autumn/Fall): Months 7, 8, 9 (Mehr, Aban, Azar)
- Zemestan (Winter): Months 10, 11, 12 (Dey, Bahman, Esfand)
Assumption: This method generally expects the ParsiDate
instance to hold a valid date (month between 1 and 12). If the instance could contain invalid data (e.g., from unsafe
creation), the error condition applies.
Returns
Ok(Season)
: If the month component is valid (1-12), returns the correspondingSeason
enum variant (e.g.,Season::Bahar
,Season::Tabestan
,Season::Paeez
,Season::Zemestan
) wrapped inOk
.Err(DateError::InvalidDate)
: If theParsiDate
instance holds an invalid month (e.g., 0 or a value greater than 12), typically resulting from construction usingunsafe ParsiDate::new_unchecked
.
Examples (Rust)
use parsidate::{ParsiDate, Season, DateError}; // Assuming these types exist
// Example 1: Month 2 (Ordibehesht) is in Spring (Bahar)
let date_spring = ParsiDate::new(1403, 2, 15).unwrap();
assert_eq!(date_spring.season(), Ok(Season::Bahar));
// Example 2: Month 6 (Shahrivar) is in Summer (Tabestan)
let date_summer = ParsiDate::new(1403, 6, 31).unwrap();
assert_eq!(date_summer.season(), Ok(Season::Tabestan));
// Example 3: Month 9 (Azar) is in Autumn (Paeez)
let date_autumn = ParsiDate::new(1403, 9, 1).unwrap();
assert_eq!(date_autumn.season(), Ok(Season::Paeez));
// Example 4: Month 12 (Esfand) is in Winter (Zemestan) - works for leap/common years
let date_winter = ParsiDate::new(1403, 12, 30).unwrap(); // End of a leap year
assert_eq!(date_winter.season(), Ok(Season::Zemestan));
let date_winter_common = ParsiDate::new(1404, 12, 1).unwrap(); // Start of Esfand in common year
assert_eq!(date_winter_common.season(), Ok(Season::Zemestan));
// Example 5: Handling an invalid date created unsafely
let invalid_date = unsafe { ParsiDate::new_unchecked(1403, 13, 1) }; // Invalid month 13
let season_result = invalid_date.season();
// We expect an error, likely InvalidDate
assert!(season_result.is_err());
assert_eq!(season_result, Err(DateError::InvalidDate));
// Example 6: Handling another invalid date (month 0)
let invalid_date_zero = unsafe { ParsiDate::new_unchecked(1403, 0, 1) }; // Invalid month 0
assert_eq!(invalid_date_zero.season(), Err(DateError::InvalidDate));