-
Notifications
You must be signed in to change notification settings - Fork 533
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
Why does chrono::NaiveDate::parse_from_str("2020-01-01 17", "%Y-%m-%d %H")
pass?
#1075
Comments
@MarcoGorelli would you be willing to submit a PR for the |
totally! I'll give this a go, thanks, this would be useful in polars |
closing per discussion in #1079 |
reopening as per #1079 (comment) |
I don't know what to propose as a solution, but what would be really useful in polars would be if there was a way to tell if the format string contains more elements than will end up in the result, so that |
This is a safer approach to creating dates/datetimes so I like it. Off-hand, I can think of three solutions:
|
We're definitely not going to depend on the default_args crate in chrono. I think we can have |
That would be good, but in #1079 (comment) it was brought up that it might be a valid use case to only parse the date part |
Related issue: #528 |
In case it helps, here's a minimal reproducer of the larger issue in polars: use chrono;
fn parser(ts: &str, fmt: &str) -> Option<i64> {
let res = chrono::NaiveDateTime::parse_from_str(ts, fmt)
.ok()
.map(|ndt| ndt.timestamp_millis());
let res = res.or_else(|| {
chrono::NaiveDate::parse_from_str(ts, fmt)
.ok()
.map(|nd| nd.and_hms_opt(0, 0, 0).unwrap().timestamp_millis())
});
res
}
fn main() {
println!("res: {:?}", parser("2020-01-01 10", "%Y-%m-%d %H"));
println!("res: {:?}", parser("2020-01-01", "%Y-%m-%d"));
} This outputs res: Some(1577836800000)
res: Some(1577836800000) but ideally, in polars, the first one would need to be On the polars side, would erroring if the given Thanks all for your engagement here, much appreciated 🙏 |
Ok. I have started playing with this, and most of it is easy to adjust.
Next I hit a guarantee in the documentation of
I would say input like this should just be parsed as |
One step further... When parsing So better make it configurable. This can be done with an extra field on |
While I conceptually agree, I feel like semver does not allow us to break this documented guarantee in 0.4.x. The rest of your proposed logic sounds good to me. Should clearly document that... |
In addition to the above, would you consider parsing into This comes up sometimes in polars, e.g. pola-rs/polars#10178 |
@MarcoGorelli see #1094. |
Here's a reproducible example:
This outputs
My question is - would it be possible to introduce a new
ParseError
, and have the first one throwParseError(TooMany)
, instead of silently dropping'%H'
?As an aside, thanks for your work on this awesome project
The text was updated successfully, but these errors were encountered: