pub fn parse_with_date(
source: &str,
date: Option<DateTime>,
) -> Result<Duration, ParseError>Expand description
Parse the source string into a Duration relative to the optionally given date
If the date is None, then the system time of now is assumed. Time units of year and
month are parsed fuzzy since years and months are not all of equal length. Any leading and
trailing whitespace is ignored. The parser saturates at the maximum of Duration::MAX.
This method is equivalent to RelativeTimeParser::parse_with_date. See also the documentation
of RelativeTimeParser::parse_with_date.
§Errors
Returns a ParseError if an error during the parsing process occurred or the calculation
of the calculation of the given date plus the duration of the source string overflows.
§Examples
use fundu_gnu::{parse_with_date, DateTime, Duration};
assert_eq!(
parse_with_date("2hours", None),
Ok(Duration::positive(2 * 60 * 60, 0))
);
let date_time = DateTime::from_gregorian_date_time(1970, 2, 1, 0, 0, 0, 0);
assert_eq!(
parse_with_date("+1month", Some(date_time)),
Ok(Duration::positive(28 * 86400, 0))
);
assert_eq!(
parse_with_date("+1year", Some(date_time)),
Ok(Duration::positive(365 * 86400, 0))
);
// 1972 is a leap year
let date_time = DateTime::from_gregorian_date_time(1972, 2, 1, 0, 0, 0, 0);
assert_eq!(
parse_with_date("+1month", Some(date_time)),
Ok(Duration::positive(29 * 86400, 0))
);
assert_eq!(
parse_with_date("+1year", Some(date_time)),
Ok(Duration::positive(366 * 86400, 0))
);