Start Of Season - jalalvandi/ParsiDate GitHub Wiki
start_of_season
Method Returns a new ParsiDate
instance representing the first day of the season in which the current date falls.
Description
This method calculates the starting date of the Persian season corresponding to the ParsiDate
instance (self
).
It first determines the season (Bahar, Tabestan, Paeez, Zemestan) of the self
instance based on its month. Then, it constructs and returns a new ParsiDate
with the following components:
- Year: The same year as the original date (
self.year
). - Month: The first month of the determined season:
- 1 (Farvardin) for Bahar (Spring)
- 4 (Tir) for Tabestan (Summer)
- 7 (Mehr) for Paeez (Autumn)
- 10 (Dey) for Zemestan (Winter)
- Day: Always set to 1.
Assumption: This method relies on the validity of the month component of the self
instance to correctly determine the season. If self
contains an invalid month, an error is returned.
Returns
Ok(ParsiDate)
: If the original date (self
) is valid, returns a newParsiDate
instance representing the first day of the corresponding season (Month 1, 4, 7, or 10, Day 1), wrapped inOk
.Err(DateError::InvalidDate)
: If the originalParsiDate
instance (self
) holds invalid data (specifically an invalid month), preventing the determination of the season. This error is likely propagated from an internal check or call to determine the season.
Examples (Rust)
use parsidate::{ParsiDate, Season, DateError}; // Assuming these types exist
// Example 1: Date in Paeez (Autumn) - Month 8 (Aban)
let date_paeez = ParsiDate::new(1403, 8, 20).unwrap();
let start_paeez = date_paeez.start_of_season().unwrap();
// Paeez starts on Month 7 (Mehr), Day 1
assert_eq!(start_paeez, ParsiDate::new(1403, 7, 1).unwrap());
// Example 2: Date in Bahar (Spring) - Month 1 (Farvardin)
let date_bahar = ParsiDate::new(1403, 1, 5).unwrap();
let start_bahar = date_bahar.start_of_season().unwrap();
// Bahar starts on Month 1 (Farvardin), Day 1
assert_eq!(start_bahar, ParsiDate::new(1403, 1, 1).unwrap());
// Example 3: Date in Tabestan (Summer) - Month 6 (Shahrivar)
let date_tabestan = ParsiDate::new(1404, 6, 31).unwrap();
let start_tabestan = date_tabestan.start_of_season().unwrap();
// Tabestan starts on Month 4 (Tir), Day 1
assert_eq!(start_tabestan, ParsiDate::new(1404, 4, 1).unwrap());
// Example 4: Date in Zemestan (Winter) - Month 11 (Bahman)
let date_zemestan = ParsiDate::new(1399, 11, 22).unwrap();
let start_zemestan = date_zemestan.start_of_season().unwrap();
// Zemestan starts on Month 10 (Dey), Day 1
assert_eq!(start_zemestan, ParsiDate::new(1399, 10, 1).unwrap());
// Example 5: Handling an invalid date created unsafely
let invalid_date = unsafe { ParsiDate::new_unchecked(1403, 0, 15) }; // Invalid month 0
let result = invalid_date.start_of_season();
// Expect an error because the season cannot be determined
assert!(result.is_err());
assert_eq!(result, Err(DateError::InvalidDate));