-
Notifications
You must be signed in to change notification settings - Fork 542
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
Add ParseErrorKind::TooMany if format string contains too many directives for given type #1079
Conversation
7e5f906
to
9c893e4
Compare
I have not looked at your code yet, apologies. But you are modifying parts of the public API. Is there a way to do this that is backwards compatible? Or do you want to target 0.5? I am slowly getting more familiar with the ideas behind the current parser myself... At a high level we have:
The right place to raise an error for what you want is when converting |
Not to discourage, but I don't know if an error is what I would like for your case in #1075. use chrono;
fn main() {
println!("{:?}", chrono::NaiveDate::parse_from_str("2020-01-01 17", "%Y-%m-%d %H"));
} Can it not be a valid use case to be only interested in the date part? While you have to provide a format string that can parse all fields of input, you may not be interested in all of them. |
I see your linked issue pola-rs/polars#8849. Would it help if chrono was able to respect the padding modifiers when parsing (they are currently ignored)? |
Sure, but then couldn't you just as easily call use chrono::{NaiveDateTime};
fn main() {
let naive_datetime = NaiveDateTime::parse_from_str("2023-05-23 10:30:00", "%Y-%m-%d %H:%M:%S").unwrap();
let naive_date = naive_datetime.date();
println!("{:?}", naive_date);
} |
Thanks for taking a look at the linked issue - I don't think that respecting the padding modifiers would solve that case, because then |
Sorry this wouldn't work if Might just have to work around this in polars then if this is the desired behaviour - saves breaking backwards compatibility in chrono too. Thanks for your responses, closing then! |
You also seem to be at a pc at the moment, so I'll take advantage of that to ask something more 😄. I have seen another issue that it would be nice if we had some way to validate a format string. And in a personal project I wanted a way to query if a format string has enough fields to describe a date, a time, datetime, etc. Would such a validating function be helpful here? (And don't mind my opinions too much) |
if a format string doesn't have enough fields to describe a date/time/datetime, then it already errors - what we'd really need is a way to validate if a format string has more fields than will end up in the result |
That seems like useful information that should somehow be possible to return. |
I do think we should probably error out if the format string is parsing data that it has no way of returning. |
thanks @djc - do we want to continue the conversation in the issue, and see where to take it from there? |
Yeah, can you make a proposal there on how we could accomodate this use case? |
The example in the linked issue is interesting. What if the input only has a date and hour? We are currently not able to parse that into a use chrono::{NaiveDateTime};
fn main() {
let naive_datetime = NaiveDateTime::parse_from_str("2023-05-23 10", "%Y-%m-%d %H").unwrap();
let naive_date = naive_datetime.date();
println!("{:?} {:?}", naive_datetime, naive_date);
} Fails with:
It seems like parsing where the fields do not fully match to output type has three choices:
Chrono currently works like 1. Option 2 seems more useful to me. |
same, to me too |
Agreed in the sense that I think date + hour should be accepted, but I think we probably shouldn't parse |
For Issue #660, I changed behavior of function However, 2. is useful. If 2. is accepted then it must be clearly documented everywhere it happens (which I think includes function /// Function `parse_from_str` will substitute default values for missing date parts.
/// A missing second specifier will substitute a `second` value of `0`.
/// A missing minute specifier will substitute a `minute` value of `0`.
/// A missing hour specifier will substitute a `hour` value of `0`.
/// A missing day specifier will substitute a `day` value of `1`.
/// A missing month specifier value will substitute a `month` value of `1`.
/// A missing year specifier value will substitute the current year.
(the substitute behavior for missing day, month, and year could be debated further) |
Thanks for contributing to chrono!
If your feature is semver-compatible, please target the 0.4.x branch;
the main branch will be used for 0.5.0 development going forward.
Please consider adding a test to ensure your bug fix/feature will not break in the future.
Here's my attempt at adding ParseErrorKind::TooMany
closes #1075