Is Valid (ParsiDateTime) - jalalvandi/ParsiDate GitHub Wiki
is_valid
(on ParsiDateTime)
Method Checks if the current ParsiDateTime
instance represents a valid combination of a Persian date and a time of day.
Description
This method performs a comprehensive validity check on the ParsiDateTime
instance, verifying both its date and time components according to standard rules:
- Date Validity: It checks whether the internal
ParsiDate
component is valid by callingParsiDate::is_valid()
. This ensures the year, month, and day combination is legitimate within the Persian calendar (e.g., year is within [1, 9999], month is [1, 12], day is within the correct range for the month considering leap years). - Time Validity: It checks whether the time components (hour, minute, second) fall within their standard allowed ranges:
- Hour (
H
): Must be between 0 and 23 (inclusive). - Minute (
M
): Must be between 0 and 59 (inclusive). - Second (
S
): Must be between 0 and 59 (inclusive).
- Hour (
Both the date part AND the time part must be valid for this method to return true
.
Usage
This method is particularly useful for verifying instances that might have been created using potentially unsafe methods like unsafe ParsiDateTime::new_unchecked()
, or in any situation where the validity of a ParsiDateTime
instance is uncertain.
Instances created through safe methods like ParsiDateTime::new()
, ParsiDateTime::from_date_and_time()
, ParsiDateTime::from_gregorian()
, or ParsiDateTime::now()
are generally expected to be valid, provided the creation process itself did not return an error.
Returns
true
: If both the date part (year, month, day) is valid according toParsiDate::is_valid()
and the time part (hour, minute, second) is within its valid range.false
: If either the date part is invalid or any of the time components (hour, minute, second) is outside its valid range.
Examples (Rust)
use parsidate::{ParsiDateTime, ParsiDate}; // Assuming these types exist
// --- Case 1: Fully Valid Instance ---
// Created via a safe constructor, expected to be valid
let valid_dt = ParsiDateTime::new(1403, 12, 30, 23, 59, 59).unwrap(); // End of a leap year
assert!(valid_dt.is_valid());
// --- Case 2: Invalid Time (created unsafely) ---
// Date part is valid (1403-01-01), but hour 24 is invalid.
let invalid_time_dt = unsafe { ParsiDateTime::new_unchecked(1403, 1, 1, 24, 0, 0) };
assert!(!invalid_time_dt.is_valid());
// --- Case 3: Invalid Date (created unsafely) ---
// Time part is valid (10:00:00), but date 1404-12-30 is invalid (1404 not leap).
let invalid_date_dt = unsafe { ParsiDateTime::new_unchecked(1404, 12, 30, 10, 0, 0) };
assert!(!invalid_date_dt.is_valid());
// --- Case 4: Both Date and Time Invalid (created unsafely) ---
// Month 13, Day 32, Hour 25, Minute 60, Second 60 are all invalid.
let invalid_both_dt = unsafe { ParsiDateTime::new_unchecked(1404, 13, 32, 25, 60, 60) };
assert!(!invalid_both_dt.is_valid());
// --- Case 5: Valid date and time (edge case midnight) ---
let midnight = ParsiDateTime::new(1404, 1, 1, 0, 0, 0).unwrap();
assert!(midnight.is_valid());