Accept a space separator in NaiveDateTime::from_str - #1815
Closed
hdimer wants to merge 1 commit into
Closed
Conversation
`NaiveDateTime`'s `Display` writes a space between the date and the time,
but `FromStr` hard-coded `Item::Literal("T")`, so no `NaiveDateTime` could
round-trip through `to_string()`/`parse()`.
Split the format items at the separator and accept whitespace as well as a
`T`, the same fix chronotope#378 made for `DateTime<FixedOffset>`.
Fixes chronotope#1792
hdimer
marked this pull request as ready for review
August 22, 2026 19:57
Member
|
chrono is soft-deprecated and will not be accepting most kinds of changes: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Thanks for contributing to chrono!
Targeting
main: this only widens the set of accepted inputs, so it is semver-compatible.Fixes #1792.
NaiveDateTime'sDisplaywrites a space between the date and the time, butFromStrhard-codedItem::Literal("T"), so nothing round-trips:#378 fixed exactly this for
DateTime<FixedOffset>back in 0.4.11 ("Support a space orTinFromStrforDateTime<Tz>, meaning that e.g.dt.to_string().parse::<DateTime<Utc>>()now correctly works on round-trip").NaiveDateTimekept the old items array, XXX comment and all, so it was left behind.This splits the items at the separator and accepts whitespace there as well as a
T, following the structureNaiveTime::from_stralready uses. Addstest_to_string_round_trip, mirroring the one insrc/datetime/tests.rs.Three things worth flagging, since they are judgement calls rather than mechanical consequences:
"2012-12-12 12:12:12"moves from the invalid list to the valid list intest_datetime_from_str, and the matchingis_err()in the serde tests becomes a positive assertion. I added"2012-12-1212:12:12"(no separator at all) to the invalid list to keep that case covered, sinceNumeric(Day)is width-2 and would otherwise happily split1212:12:12.Deserializegoes throughFromStr, soNaiveDateTimenow accepts the space form from JSON. That is whatDateTime<Utc>andDateTime<FixedOffset>already do today as a consequence of support round tripping display <-> datetime #378, so it makes the naive type consistent rather than novel, but it is a wire-format change and you may feel differently about it.trim_start()means a tab or newline works too, so"2016-07-08\n09:10:48"now parses. Every other slot in this same format already trims Unicode whitespace viaItem::Space(""), so this is that existing policy applied to one more position, but it is looser thanparse_rfc3339_relaxed, which matches a single byte. Happy to tighten it to an ASCII space if you would rather keep the two parsers aligned.I deliberately did not make the
Tcase-insensitive, so"2012-12-12t12:12:12"is still rejected here whileDateTime<FixedOffset>accepts it. That is outside what #1792 asks for and the existing test pins it, so it seemed like your call to make. I left the// XXX shouldn't this be case-insensitive?comment in place.cargo test(full CI feature set, plus the--no-default-featuresvariants),cargo clippy --all-targets -D warningsandcargo fmt --checkare all clean.I used AI tooling while working on this, and I have reviewed and tested the change myself.
Used AI assistance on this; I reviewed and tested the change myself.