-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
uucore: add datetime parser #4917
Conversation
Add a relaxed datetime parser to uucore for use in `date` utility (and potentially others). This datetime parser functions by using `chrono`s own parsing utilities and a try/succeed approach to parsing. This commit adds `date -d` functionality to the `date` utility by enabling parsing of a number of date formats. This implementation of the datetime parser has some drawbacks and some positives. On the positive side: - it was easy to implement - it is easy to add more datetime formats to In order to add additionally supported formats, a developer can add the required format string to the `format` mod in `parse_datetime.rs`, and then add it as a potential format to the relevant `fmts` vec. On the negative: - It is not easily customiseable beyond the supported `chrono` parsing formats. E.g., `chrono` does not currently support parsing offsets without trailing zeros. `date -d "UTC+1"` should return a valid response but `chrono` fails to parse this. - Because it is an attempt driven parser, it is likely not that performant. I have not done any performance testing as part of this change, but I would expect a custom parser to perform much better.
Nice, what would you think about making a crate like https://github.com/uutils/humantime_to_duration/ ? |
That would be great! I can give that a shot |
@sylvestre I actually think this should be part of This little test shows that all the complex modifiers should also work when reading from a file: echo "yesterday" > dates.txt
date -f dates.txt |
I think that a new |
Oh yeah I misspelled it. I meant to say that they should be the same. Duration is indeed a bit weird because I think the main use case is to create datetimes |
happy to rename them (we don't have any other users of humantime_to_duration) However, humantime_to_duration uses the time crate, this PR uses chrono. We should use only one :) |
My preference is for chrono, because it handles timezones better and is actively maintained again, but whatever works is good I suppose :) |
ok, let me do the port, should be easy :) |
Ace, I see that's been merged! Should I move this PR to the humantime crate? Seems there's a few clippy spelling warnings I need to fix too |
It would be great, thanks Please try to follow the same naming and structure :) |
Closing this PR as it has been moved to uutils/parse_datetime#12 |
@Benjscho @tertsdiepraam ok to rename humantime_to_duration to parse_datetime? |
Sounds good to me! I think when renaming the crate it will need some reorganization to make it make more sense. I think there should be a main lib function I'm happy to do some of the work for that reorg in a follow up PR after renaming if that plan sounds like it makes sense |
This commit is a trivial followup for: uutils#4917 and uutils/parse_datetime#12 The functionality to parse the datetime was moved into the parse_datetime crate and the only (tiny) piece left is to call it from `date`. It also adds the two tests from the original PR#4917. Closes: uutils#4657 Thanks to Ben Schofield
This commit is a trivial followup for: uutils#4917 and uutils/parse_datetime#12 The functionality to parse the datetime was moved into the parse_datetime crate and the only (tiny) piece left is to call it from `date`. It also adds the test-case from the original issue. I did not include the two tests from PR#4917 because they appear to work even without this change. I am happy to include them of course if prefered. Closes: uutils#4657 Thanks to Ben Schofield
* date: fix `date -f dates.txt is failing` This commit is a trivial followup for: #4917 and uutils/parse_datetime#12 The functionality to parse the datetime was moved into the parse_datetime crate and the only (tiny) piece left is to call it from `date`. It also adds the test-case from the original issue. I did not include the two tests from PR#4917 because they appear to work even without this change. I am happy to include them of course if prefered. Closes: #4657 Thanks to Ben Schofield * tests: tweak changes to test_date.rs to be more idiomatic Co-authored-by: Sylvestre Ledru <sylvestre@debian.org> --------- Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
Add a relaxed datetime parser to uucore for use in
date
utility (and potentially others). This datetime parser functions by usingchrono
s own parsing utilities and a try/succeed approach to parsing. This commit addsdate -d
functionality to thedate
utility by enabling parsing of a number of date formats.This implementation of the datetime parser has some drawbacks and some positives. On the positive side:
In order to add additionally supported formats, a developer can add the required format string to the
format
mod inparse_datetime.rs
, and then add it as a potential format to the relevantfmts
vec.On the negative:
chrono
parsing formats. E.g.,chrono
does not currently support parsing offsets without trailing zeros.date -d "UTC+1"
should return a valid response butchrono
fails to parse this.