Start Of Season (ParsiDateTime) - parsicore/parsidate GitHub Wiki
Method start_of_season (on ParsiDateTime)
Returns a new ParsiDateTime representing the start of the season for the current instance's date, preserving the original time component.
Description
This method calculates the first day of the Persian season (Bahar, Tabestan, Paeez, or Zemestan) in which the date part of the current ParsiDateTime (self) falls. It then creates and returns a new ParsiDateTime instance representing that start date, while keeping the original time component (hour, minute, second) unchanged.
The calculation of the start-of-season date (Year, Month=1/4/7/10, Day=1) is delegated to the ParsiDate::start_of_season method applied to the date part of self. Please refer to its documentation for the specific date logic.
Returns
Ok(ParsiDateTime): If the date part of the originalParsiDateTime(self) is valid, returns a newParsiDateTimeinstance representing the first day of the corresponding season with the original time, wrapped inOk.Err(DateError::InvalidDate): If the date part of the originalParsiDateTimeinstance is invalid (e.g., invalid month), preventing the determination of the season. This error is propagated from the underlyingParsiDate::start_of_seasoncall.
Examples (Rust)
use parsidate::{ParsiDateTime, ParsiDate, Season, DateError}; // Assuming these types exist
// Example 1: Date in Paeez (Autumn) - Month 8 (Aban)
let dt_paeez = ParsiDateTime::new(1403, 8, 20, 15, 30, 0).unwrap(); // Aban 20th, 15:30:00
let start_dt_paeez = dt_paeez.start_of_season();
assert!(start_dt_paeez.is_ok());
let start_dt = start_dt_paeez.unwrap();
// Verify the date part: Paeez starts on Month 7 (Mehr), Day 1
assert_eq!(start_dt.date(), ParsiDate::new(1403, 7, 1).unwrap());
// Verify the time part: Should be preserved from the original dt
assert_eq!(start_dt.hour(), 15);
assert_eq!(start_dt.minute(), 30);
assert_eq!(start_dt.second(), 0);
// Example 2: Date in Bahar (Spring) - Month 1 (Farvardin)
let dt_bahar = ParsiDateTime::new(1404, 1, 5, 9, 0, 59).unwrap(); // Farvardin 5th, 09:00:59
let start_dt_bahar = dt_bahar.start_of_season().unwrap();
// Verify the date part: Bahar starts on Month 1 (Farvardin), Day 1
assert_eq!(start_dt_bahar.date(), ParsiDate::new(1404, 1, 1).unwrap());
// Verify the time part: Should be preserved
assert_eq!(start_dt_bahar.time(), (9, 0, 59));
// Example 3: Handling an invalid date created unsafely
// let invalid_dt = unsafe { ParsiDateTime::new_unchecked(1403, 13, 1, 10, 0, 0) }; // Invalid month 13
// let result = invalid_dt.start_of_season();
// Expect an error because the season cannot be determined from the invalid date part
// assert!(result.is_err());
// assert_eq!(result, Err(DateError::InvalidDate));