#duration-string #parse-duration #duration #string #parse #date #parser

parse_duration0

Parses a duration from a string. Fork with security fix.

1 stable release

3.0.0 Jul 5, 2022

#468 in Date and time

Download history 52/week @ 2023-12-10 55/week @ 2023-12-17 46/week @ 2023-12-24 7/week @ 2023-12-31 7/week @ 2024-02-18 34/week @ 2024-02-25 16/week @ 2024-03-03 10/week @ 2024-03-10 8/week @ 2024-03-17

68 downloads per month
Used in 2 crates

MIT license

30KB
415 lines

parse_duration0

Crates.io Documentation Test & lint Check dependencies License: MIT

This is a fork of zeta12ti's parse_duration.

Main differences:

  • It fixes a DOS issue.
  • It drops support for numbers bigger than ±9.22e18.
  • It drops support for exponential notation.

This crate provides a function parse for parsing strings into durations. The parser is based on the standard set by systemd.time, but extends it significantly. For example, negative numbers, decimals and exponents are allowed.

extern crate parse_duration;

use ::parse_duration0::parse;
use ::std::time::Duration;

// One hour less than a day
assert_eq!(parse("1 day -1 hour"), Ok(Duration::new(82_800, 0)));
// Extra things will be ignored
assert_eq!(
    parse("Duration: 1 hour, 15 minutes and 29 seconds"),
    Ok(Duration::new(4529, 0))
);

Dependencies

~2.9–4MB
~67K SLoC