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

%:::z format incorrectly expects minutes when parsing #1228

Open
max-kamps opened this issue Aug 27, 2023 · 1 comment
Open

%:::z format incorrectly expects minutes when parsing #1228

max-kamps opened this issue Aug 27, 2023 · 1 comment
Labels

Comments

@max-kamps
Copy link

max-kamps commented Aug 27, 2023

If I understand the docs correctly, %:::z corresponds to the timezone offset, without minutes.
When formatting, this works correctly.
However, when parsing it does expect minutes, and rejects inputs that don't include them.

Tested on 0.4.26 and main, both affected

Playground-compatible sample:

use chrono::{FixedOffset, NaiveDate, TimeZone, Timelike, Utc, DateTime};

fn main() {
    let dt = FixedOffset::west_opt(5 * 3600)
        .unwrap()
        .from_local_datetime(
            &NaiveDate::from_ymd_opt(2001, 7, 8)
                .unwrap()
                .and_hms_nano_opt(0, 34, 59, 1_026_490_708)
                .unwrap(),
        )
        .unwrap();

    const FORMAT: &str = "%Y-%m-%d %H:%M:%S%:::z";
    
    // Formatting works as expected. This produces "-05" for the timezone
    println!("{}", dt.format(FORMAT));
    // Output: 2001-07-08 00:34:60-05
    
    // However, parsing doesn't! This will result in a ParseError(TooShort) / "premature end of input error"
    println!("{:?}", DateTime::parse_from_str("2001-07-08 00:34:60-05", FORMAT));
    // Output: Err(ParseError(TooShort))

    // Adding minutes to the timezone fixes it. But %:::z should not expect minutes
    println!("{:?}", DateTime::parse_from_str("2001-07-08 00:34:60-0500", FORMAT));
    // Output: Ok(2001-07-08T00:34:60-05:00)
}
@pitdicker
Copy link
Collaborator

Should get fixed with #1083.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants