Function jackdauer::duration[][src]

pub fn duration(input: &str) -> Result<Duration, Error>
Expand description

Parses a std::time::Duration from a human readable string of characters.

The supported syntax for durations consists in a comma separated list of time values, followed by their respective time unit. Alternatively, the “and” keyword can be used in place of a comma. While time values are only accepted in their numerical form, time units are accepted in both their singular and plural forms.

If the provided string cannot’ be parsed, a ParseError will be returned as part of the Result.

Reference

The accepted time units formats are as follows:

  • Years: years, year and y are accepted
  • Months: months, month, and mo are accepted
  • Weeks: weeks, week, and w are accepted
  • Days: days, day, and d are accepted
  • Hours: hours, hour, and h are accepted
  • Minutes: minutes mins, and min are accepted
  • Seconds: seconds, second, secs, sec and s are accepted
  • Milliseconds: milliseconds, millisecond, ms are accepted
  • Nanoseconds: nanoseconds, nanosecond, ns are accepted

Examples

use jackdauer::duration;

// Singular
let nanoseconds = duration("1 nanosecond");

// Plural
let milliseconds = duration("2 milliseconds");

// Full syntax example
let small = duration("3 seconds, 2 milliseconds and 1 nanosecond");