Sub Duration (ZonedParsiDateTime) - parsicore/parsidate GitHub Wiki
sub_duration
(on ZonedParsiDateTime)
Method Subtracts a specified chrono::Duration
from this ZonedParsiDateTime
, returning a new ZonedParsiDateTime
instance.
Description
This convenience method calculates a new ZonedParsiDateTime
by subtracting the provided duration from the current datetime. All components of the datetime (date, time, and timezone offset) are taken into account.
For more idiomatic Rust code, you can alternatively use the -
operator with chrono::Duration
directly.
Arguments
duration
: Achrono::Duration
to subtract.- Positive values move the datetime backward.
- Negative values move the datetime forward.
Returns
ZonedParsiDateTime
: A new instance with the datetime adjusted by the specified duration.
The original instance remains unchanged.
Examples (Rust)
use parsidate::{ZonedParsiDateTime}; // Assuming this type exists
use chrono::Duration;
// Create a ZonedParsiDateTime for 1403-01-01 01:00:00 in Tehran timezone
let dt = ZonedParsiDateTime::new(1403, 1, 1, 1, 0, 0, Tehran).unwrap();
// Subtract 2 hours. Same as using `- Duration::hours(2)`
let earlier = dt.sub_duration(Duration::hours(2));
// --- Check results ---
// Subtracting 2 hours crosses into the previous day.
assert_eq!(earlier.date(), dt.date().with_year(1402).unwrap().with_month(12).unwrap().with_day(29).unwrap()); // Expected date: 1402-12-29
assert_eq!(earlier.hour(), 23); // Hour correctly adjusted