Expand description
A simple library for parsing human-readable duration strings into std::time::Duration.
§Usage
This library provides a parse function for quick and easy parsing, and a Parser
struct for more control over parsing behavior.
§The parse function
The parse function is a convenience wrapper around a default Parser.
use durstr::parse;
use std::time::Duration;
let dur = parse("12 minutes, 21 seconds");
assert_eq!(dur, Ok(Duration::from_secs(741)));
let dur = parse("1hr 2min 3sec");
assert_eq!(dur, Ok(Duration::from_secs(3723)));§The Parser struct
For more control, you can use the Parser struct directly. For example, to parse with case-insensitivity:
use durstr::{Parser, ParserOptions};
use std::time::Duration;
let parser = Parser::new(ParserOptions { ignore_case: true });
let dur = parser.parse("1 MINUTE, 2 SECONDS");
assert_eq!(dur, Ok(Duration::from_secs(62)));§Supported Units
| Unit | Aliases |
|---|---|
| Millisecond | ms, msec(s), millisecond(s) |
| Second | s, sec(s), second(s) |
| Minute | m, min(s), minute(s) |
| Hour | h, hr(s), hour(s) |
Structs§
- Parser
- A configurable parser for duration strings.
- Parser
Options - Options to customize the behavior of a
Parser.
Enums§
- Error
- An error that can occur when parsing a duration string.
Functions§
- parse
- Parses a duration string into a
std::time::Duration.