Skip to content

Support providing default values during parsing #632

Open
@msrd0

Description

@msrd0

I'm trying to parse a date in the format YYMMDD, e.g. 231022. While this crate can parse 20231022, it fails to parse 231022 (Playground):

use time::macros::{date, format_description};
use time::Date;

fn main() {
    let long_format = format_description!("[year][month][day]");
    assert_eq!(
        Date::parse("20231022", long_format).unwrap(),
        date!(2023 - 10 - 22)
    );

    let short_format = format_description!("[year repr:last_two][month][day]");
    assert_eq!(
        Date::parse("231022", short_format).unwrap(),
        date!(2023 - 10 - 22)
    );
}
thread 'main' panicked at src/main.rs:13:45:
called `Result::unwrap()` on an `Err` value: TryFromParsed(InsufficientInformation)

This is probably because there is no century information included in the later. It would be nice if it was possible to give default values during parsing, or add an option to assume missing values to be 0 (so I can add 2000 years after parsing) or similar. This would probably also make sense for formats with optional fields.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parsingArea: parsingC-feature-requestCategory: a new feature (not already implemented)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions