Function parse

Source
pub fn parse(
    source: &str,
    default_unit: Option<TimeUnit>,
    max: Option<Duration>,
) -> Result<Duration, ParseError>
Expand description

Parse the source string into a Duration

This method does not include the time units for nano seconds unlike the TimeSpanParser::parse_nanos method. The parser saturates at the maximum Duration of u64::MAX micro seconds if not specified otherwise. Optionally, it’s possible to specify a different default time unit than TimeUnit::Second

§Panics

This method panics if max is a a negative Duration.

§Errors

Returns a ParseError if an error during the parsing process occurred

§Examples

use fundu::{Duration, TimeUnit};
use fundu_systemd::{parse, SYSTEMD_MAX_MICRO_DURATION};

assert_eq!(
    parse("2hours", None, None),
    Ok(Duration::positive(2 * 60 * 60, 0))
);
assert_eq!(
    parse("1y 12month", None, None),
    Ok(Duration::positive(63_115_200, 0))
);
assert_eq!(
    parse("12.3", Some(TimeUnit::MilliSecond), None),
    Ok(Duration::positive(0, 12_300_000))
);
assert_eq!(
    parse("100000000000000000000000000000years", None, None),
    Ok(SYSTEMD_MAX_MICRO_DURATION)
);
assert_eq!(
    parse(
        "100000000000000000000000000000years",
        None,
        Some(Duration::MAX)
    ),
    Ok(Duration::MAX)
);
assert_eq!(
    parse("infinity", None, None),
    Ok(SYSTEMD_MAX_MICRO_DURATION)
);