Skip to main content

parse_duration

Function parse_duration 

Source
pub fn parse_duration(
    text: &str,
    i18n: &I18n,
) -> Result<Duration, DurationError>
Expand description

Reads a length of time written by hand, in any language i18n knows.

Three forms are read:

  • Numbers with units, in any order, each unit once: 1 h 30 min, 1sa30dk, 90 dk, 2 hours, 1,5 sa. The unit words come from quvyta.duration.hour-words, minute-words and second-words of every language, the active one first; case does not matter, and Turkish dotted and dotless i read alike. A number after a unit with no unit of its own takes the next smaller one: 1 h 30 is an hour and a half. A decimal point or comma splits a unit (1.5 h is 90 minutes), rounded to the second. Minutes and seconds may run past 60 and carry over: 1 h 90 min is two hours and a half.
  • A clock face, h:mm or h:mm:ss: 2:15 is two hours and a quarter, never two minutes. Minutes and seconds are one or two digits under 60.
  • A bare number, read in minutes: 25 is 25 minutes, the length of most timers.

ยงErrors

A DurationError naming the first part that could not be read, or DurationError::TooLarge past 99 hours, 59 minutes and 59 seconds.