Skip to main content

parse_duration

Function parse_duration 

pub fn parse_duration(s: &str) -> Result<Duration, ParseDurationError>
Expand description

Parses a string in the exact format accepted by Go’s time.ParseDuration, restricted to non-negative values (since std::time::Duration can’t represent negatives).

Accepts a decimal number with a required unit suffix, optionally repeated (for example, "300ms", "1h30m", "2h45m30.5s"), with an optional leading sign. Valid units are ns, us (or µs/μs), ms, s, m, and h. A bare 0 (optionally signed) is accepted as zero.

§Errors

Returns ParseDurationError if the string is empty, has a missing or unknown unit, contains no digits, parses to a negative duration, or overflows the supported nanosecond range.