Skip to main content

parse_duration

Function parse_duration 

Source
pub fn parse_duration(
    input: impl AsRef<str>,
) -> Result<Value, ParseDurationError>
Available on crate feature jiff only.
Expand description

Parses an input string containing an xsd:duration literal.

Requires jiff (enabled by datetime). Duration designators must be uppercase ASCII; friendly and clock-style backend spellings are rejected. Only a leading minus sign is allowed; a leading plus sign is rejected. Only seconds may have a fraction, with ASCII digits on both sides of a period separator; commas and fractional hours or minutes are rejected. Fractional seconds support at most nine digits (nanosecond precision). Longer fractions are rejected, including excess zeros, never rounded.

§Errors

Returns an error for invalid lexical forms or values outside the backend’s signed, fixed-length duration representation. Calendar years and months are not supported. Fractions longer than nine digits return an explicit precision error. Overflow returns an error rather than panicking.

let value = xsd::parse_duration("-PT1.000000001S").unwrap();
assert_eq!(value.to_string(), "-PT1.000000001S");
assert!(xsd::parse_duration("PT0.0000000001S").is_err());