Function duration_str::parse_chrono

source ·
pub fn parse_chrono(input: impl AsRef<str>) -> Result<Duration, String>
Expand description

convert Into<String> to chrono::Duration

§Example

use duration_str::parse_chrono;
use chrono::Duration;

// supports units
let duration = parse_chrono("1d").unwrap();
assert_eq!(duration,Duration::seconds(24*60*60));

// supports addition
let duration = parse_chrono("3m+31").unwrap();
assert_eq!(duration,Duration::seconds(211));

// spaces are optional
let duration = parse_chrono("3m + 31").unwrap();
assert_eq!(duration,Duration::seconds(211));

// plus sign is optional
let duration = parse_chrono("3m  31").unwrap();
assert_eq!(duration,Duration::seconds(211));

// both plus and spaces are optional
let duration = parse_chrono("3m31").unwrap();
assert_eq!(duration,Duration::seconds(211));

// supports multiplication
let duration = parse_chrono("1m*10").unwrap();
assert_eq!(duration,Duration::seconds(600));

// spaces are optional
let duration = parse_chrono("1m * 10").unwrap();
assert_eq!(duration,Duration::seconds(600));