Function fundu::parse_duration

source ·
pub fn parse_duration(string: &str) -> Result<Duration, ParseError>
Available on crate feature standard only.
Expand description

Parse a string into a std::time::Duration by accepting a string similar to floating point with the default set of time units.

This method is basically the same like DurationParser::new providing a simple to setup onetime parser. It is generally a better idea to use the DurationParser builder if multiple inputs with the same set of time units need to be parsed or a customization of the time format is wished.

See also the module level documentation for more details and more information about the format.

Errors

This function returns a ParseError when parsing of the input string failed.

Examples

use std::time::Duration;

use fundu::{parse_duration, ParseError};

let duration = parse_duration("+1.09e1").unwrap();
assert_eq!(duration, Duration::new(10, 900_000_000));

assert_eq!(
    parse_duration("Not a number"),
    Err(ParseError::Syntax(
        0,
        "Invalid input: 'Not a number'".to_string()
    ))
);