jiff::signed_duration

Function parse_iso_or_friendly

Source
fn parse_iso_or_friendly(bytes: &[u8]) -> Result<SignedDuration, Error>
Expand description

A common parsing function that works in bytes.

Specifically, this parses either an ISO 8601 duration into a SignedDuration or a “friendly” duration into a SignedDuration. It also tries to give decent error messages.

This works because the friendly and ISO 8601 formats have non-overlapping prefixes. Both can start with a + or -, but aside from that, an ISO 8601 duration always has to start with a P or p. We can utilize this property to very quickly determine how to parse the input. We just need to handle the possibly ambiguous case with a leading sign a little carefully in order to ensure good error messages.

(We do the same thing for Span.)