Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support providing default values during parsing #632

Open
msrd0 opened this issue Oct 22, 2023 · 1 comment
Open

Support providing default values during parsing #632

msrd0 opened this issue Oct 22, 2023 · 1 comment
Labels
A-parsing Area: parsing C-feature-request Category: a new feature (not already implemented) C-needs-details Category: more details are needed to assess the situation

Comments

@msrd0
Copy link

msrd0 commented Oct 22, 2023

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.

@jhpratt
Copy link
Member

jhpratt commented Oct 23, 2023

This is currently possible by going through Parsed struct. You would need to parse the format into Parsed, alter the year as necessary, and finally convert it to Date.

@jhpratt jhpratt added C-feature-request Category: a new feature (not already implemented) C-needs-details Category: more details are needed to assess the situation A-parsing Area: parsing labels Oct 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parsing Area: parsing C-feature-request Category: a new feature (not already implemented) C-needs-details Category: more details are needed to assess the situation
Projects
None yet
Development

No branches or pull requests

2 participants