With Minute (ParsiDateTime) - parsicore/parsidate GitHub Wiki
Method with_minute (on ParsiDateTime)
Creates a new ParsiDateTime instance with only the minute component changed, preserving the date and other time components.
Description
This method creates a new ParsiDateTime based on the current instance (self), replacing only the minute component of the time part. The date part (year, month, day) and the other time components (hour, second) are copied directly from the original self instance.
This method performs validation on the provided minute value to ensure it is within the standard range (0-59). It also implicitly checks if the original ParsiDateTime's date part was valid.
Arguments
minute: The desired new minute (u8or similar integer type) for the newParsiDateTime. Must be between 0 and 59 (inclusive).
Returns
Ok(ParsiDateTime): If the originalParsiDateTimehas a valid date part and the providedminuteis valid (0-59), returns the newParsiDateTimeinstance (with updated minute and original date/hour/second) wrapped inOk.Err(DateError::InvalidTime): If the providedminutevalue is outside the valid range [0, 59].Err(DateError::InvalidDate): If the date part of the originalParsiDateTimeinstance (self) was invalid (e.g., created unsafely with invalid date components).
Examples (Rust)
use parsidate::{ParsiDateTime, DateError, ParsiDate}; // Assuming these types exist
let dt = ParsiDateTime::new(1403, 5, 2, 10, 30, 45).unwrap(); // 10:30:45 AM
// --- Success Case: Change Minute Within Valid Range ---
let dt_new_min_result = dt.with_minute(55); // Change minute to 55
assert!(dt_new_min_result.is_ok());
let dt_new_min = dt_new_min_result.unwrap();
// Verify the new minute and preserved components
assert_eq!(dt_new_min.minute(), 55);
assert_eq!(dt_new_min.date(), ParsiDate::new(1403, 5, 2).unwrap()); // Date unchanged
assert_eq!(dt_new_min.hour(), 10); // Hour unchanged
assert_eq!(dt_new_min.second(), 45); // Second unchanged
// Set minute to 0
let dt_zero_min_result = dt.with_minute(0);
assert!(dt_zero_min_result.is_ok());
assert_eq!(dt_zero_min_result.unwrap().minute(), 0);
// --- Error Case: Attempting to Set an Invalid Minute ---
// Try setting minute to 60 (valid range is 0-59).
let result_invalid_minute = dt.with_minute(60);
assert!(result_invalid_minute.is_err());
assert_eq!(result_invalid_minute, Err(DateError::InvalidTime)); // Check error type
// Try setting minute to a large invalid number
let result_invalid_minute_large = dt.with_minute(100);
assert!(result_invalid_minute_large.is_err());
assert_eq!(result_invalid_minute_large, Err(DateError::InvalidTime));
// --- Error Case: Starting date is invalid ---
// Create an invalid DateTime unsafely (invalid date part)
// let invalid_start_dt = unsafe { ParsiDateTime::new_unchecked(1404, 12, 30, 10, 30, 0) }; // Invalid date
// assert!(!invalid_start_dt.is_valid());
// Attempting to change the minute should fail because the original date is invalid
// assert_eq!(invalid_start_dt.with_minute(31), Err(DateError::InvalidDate));