Skip to main content

parse_value

Function parse_value 

Source
pub fn parse_value(
    pattern: &ParsedPattern,
    input: &str,
    target: ParseTarget,
) -> Option<ParsedValue>
Expand description

Reverse-parse a string against a pattern.

Strict about literal separators but lenient about trailing segments: input that runs out partway through the pattern fills the remaining segments with sensible defaults (month → 1, day → 1, hour/minute/second → 0, AM/PM → AM). The first segment of each target is required:

  • DateOnly / DateTime: the year must be present.
  • TimeOnly: the hour must be present.

Examples for pattern %Y-%m-%d:

InputResult
"2026-05-02"Date(2026, 5, 2)
"2026-5-2"Date(2026, 5, 2) (1-digit segments)
"2026-5"Date(2026, 5, 1) (day defaulted)
"2026-"Date(2026, 1, 1) (month + day defaulted)
"2026"Date(2026, 1, 1)
""None (year required)
"2026/05/02"None (literal separator mismatch)
"2026-13-02"None (out of range)

Out-of-range values and literal-separator mismatches always reject — leniency only applies to missing trailing input.