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

Years greater than 9999 require a + at the beginning in NaiveDateTime::parse_from_str #947

Closed
ethowitz opened this issue Jan 25, 2023 · 5 comments

Comments

@ethowitz
Copy link

Description
When calling NaiveDateTime::parse_from_str, years greater than 9999 require a + sign at the beginning. I didn't see this in the documentation for that function or in the documentation for chrono::format::strftime, though I do see that it's documented for Numeric::Year, which strftime appears to use internally. I wasn't sure if this was expected behavior, but I wanted to bring it to your attention. Thanks!

Minimal example
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ecc55c3483a582e866c5bf5ae51d8325

use chrono::NaiveDateTime;

const FORMAT_STRING: &str = "%Y-%m-%d %H:%M:%S%.f";

fn main() {
    assert!(NaiveDateTime::parse_from_str("9999-01-01 12:34:56", FORMAT_STRING).is_ok());
    assert!(NaiveDateTime::parse_from_str("10000-01-01 12:34:56", FORMAT_STRING).is_err());
    assert!(NaiveDateTime::parse_from_str("+10000-01-01 12:34:56", FORMAT_STRING).is_ok());
}

Versions

  • rustc 1.64.0-nightly
  • chrono 0.4.21
@esheppa
Copy link
Collaborator

esheppa commented Jan 28, 2023

Thanks for the question @ethowitz - this is intended behaviour to conform with ISO8601 - essentially the standard primarily supports years 1583 through 9999, but wider ranges can be used, however five-digit and above years require either a - or + prefix. There is further detail at https://en.wikipedia.org/wiki/ISO_8601#Years

@c-git
Copy link
Contributor

c-git commented Jan 28, 2023

Thanks for the question @ethowitz - this is intended behaviour to conform with ISO8601 - essentially the standard primarily supports years 1583 through 9999, but wider ranges can be used, however five-digit and above years require either a - or + prefix. There is further detail at https://en.wikipedia.org/wiki/ISO_8601#Years

Would it be ok to just add a note to the docs for this function as well to help future users?

I can try to write a PR if this is something desirable.

@djc
Copy link
Member

djc commented Jan 30, 2023

Yeah, documentation improvements for this would be great, thanks!

@c-git
Copy link
Contributor

c-git commented Jan 30, 2023

Ok submitted a PR for the docs, happy to add or remove as you see fit. Added the (+/-) for consistency with what I added to the format information. Can remove both if it is not desirable.

@c-git
Copy link
Contributor

c-git commented Jan 30, 2023

Closed the old pull request and create a new one because I needed to target 0.4.x and had originally started from main.

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

No branches or pull requests

5 participants