Weekday - jalalvandi/ParsiDate GitHub Wiki
weekday
Method Returns the full Persian name of the weekday corresponding to this ParsiDate
.
Description
This method calculates and returns the name of the day of the week for the date represented by the ParsiDate
instance. The returned names are the standard Persian weekday names:
- "شنبه" (Shanbeh - Saturday)
- "یکشنبه" (Yekshanbeh - Sunday)
- "دوشنبه" (Doshanbeh - Monday)
- "سهشنبه" (Seshanbeh - Tuesday)
- "چهارشنبه" (Chaharshanbeh - Wednesday)
- "پنجشنبه" (Panjshanbeh - Thursday)
- "جمعه" (Jomeh - Friday)
How it Works
The calculation typically follows these steps:
- Validation: Checks if the
ParsiDate
instance (self
) is valid. - Conversion to Gregorian: Converts the valid
ParsiDate
to its equivalentchrono::NaiveDate
using logic similar toto_gregorian
. - Weekday Calculation: Uses the functionality of the
chrono
library (specificallychrono::Weekday
) to determine the day of the week for the resulting Gregorian date. - Mapping to Persian Name: Maps the
chrono::Weekday
result (which typically follows an ISO standard, e.g., Monday=0 or Sunday=0) to the correct Persian name, carefully considering that in many Persian contexts, Saturday ("شنبه") is treated as the start of the week (often index 0 in mappings).
Returns
Ok(String)
: If theParsiDate
is valid and the conversion/calculation succeeds, returns aString
containing the full Persian weekday name (e.g., "شنبه", "یکشنبه").Err(DateError::InvalidDate)
: If theParsiDate
instance (self
) fails its internal validity check (e.g., it was created usingunsafe ParsiDate::new_unchecked
with invalid month or day values).Err(DateError::GregorianConversionError)
: If the necessary intermediate step of converting theParsiDate
to a Gregorian date fails (e.g., due to range issues or internal errors in the conversion logic).
Examples (Rust)
use parsidate::{ParsiDate, DateError}; // Assuming these types exist
// Example 1: 1403-05-02 is Gregorian 2024-07-23, which is a Tuesday ("سهشنبه")
let date_tue = ParsiDate::new(1403, 5, 2).unwrap();
assert_eq!(date_tue.weekday(), Ok("سهشنبه".to_string()));
// Example 2: 1403-01-04 is Gregorian 2024-03-23, which is a Saturday ("شنبه")
let date_sat = ParsiDate::new(1403, 1, 4).unwrap();
assert_eq!(date_sat.weekday(), Ok("شنبه".to_string()));
// Example 3: 1403-01-10 is Gregorian 2024-03-29, which is a Friday ("جمعه")
let date_fri = ParsiDate::new(1403, 1, 10).unwrap();
assert_eq!(date_fri.weekday(), Ok("جمعه".to_string()));
// Example 4: Handling an invalid date created unsafely
let invalid_date = unsafe { ParsiDate::new_unchecked(1404, 12, 30) }; // Invalid day for common year
let result = invalid_date.weekday();
// Expect an error, likely InvalidDate because the check happens first
assert!(result.is_err());
// Optionally check the specific error kind
// assert!(matches!(result, Err(DateError::InvalidDate) | Err(DateError::GregorianConversionError)));
assert_eq!(result.err().unwrap(), DateError::InvalidDate); // Assuming validation runs first