Function uucore::parse_time::from_str
source · [−]Expand description
Parse a duration from a string.
The string may contain only a number, like “123” or “4.5”, or it may contain a number with a unit specifier, like “123s” meaning one hundred twenty three seconds or “4.5d” meaning four and a half days. If no unit is specified, the unit is assumed to be seconds.
The only allowed suffixes are
- “s” for seconds,
- “m” for minutes,
- “h” for hours,
- “d” for days.
This function uses Duration::saturating_mul
to compute the
number of seconds, so it does not overflow. If overflow would have
occurred, Duration::MAX
is returned instead.
Errors
This function returns an error if the input string is empty, the input is not a valid number, or the unit specifier is invalid or unknown.
Examples
use std::time::Duration;
use uucore::parse_time::from_str;
assert_eq!(from_str("123"), Ok(Duration::from_secs(123)));
assert_eq!(from_str("2d"), Ok(Duration::from_secs(60 * 60 * 24 * 2)));