-
Notifications
You must be signed in to change notification settings - Fork 18
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
Chrono's API is inherently inconsistent with GNU's #56
Comments
Yes! Thank you! Issue 1 is exactly what I've been advocating for that we should fix. But it's been a bit messy because Now Issue 2 is something I didn't realize yet, so nice find! I think the first question to answer is what GNU does with adding 1 month. In other words, how does it define addition to be commutative? A pretty good solution might be to adapt something like that |
I agree. I'm going to try and do more sleuthing to see if I can figure out how to predict the date returned by GNU. But one concern I have is that even if we did figure out how GNU is handling addition of durations such as months, it still may be hard to do with the
I read #1282 and I agree it sounds like this might be what we need. I'm going to keep this issue open and follow up with my findings. It is still an open question about what to do with In the meantime, if anyone has more to add, let's hear it! I really enjoy working on #3505 and want to get this over the finish line but addressing these issues are a bit above one person. |
Sorry for the delay! I wanted to follow up on this issue because I have been looking into how to predict how GNU implements When adding a month (or months) to a date, GNU will attempt to land you on the same date in the requested month ( But the real key that exposes how they do things under the hood is when dealing with February. If you imagine starting from When incrementing months, GNU will first land you naively in the requested month, (January will map to February for an increment of 1 month), then GNU will attempt to land you on the requested day (February 31st), if that is not possible, it land you on the closest date, in the same month then increment forward the difference. Example:
Consider:
From what I can tell this "algorithm" predicts GNU output for any arbitrary months:
I haven't done any work yet to implement this algorithm because I wanted to follow up first, but ostensibly this feels very doable. We can use existing chrono APIs to implement the algorithm above and bring us one step close to full GNU interchangeability. |
Whoa, great work! I think this makes sense! This is probably exactly what we should do. |
Thanks for the feedback. I will get a PR together and we can go from there. |
|
It's sort of done. There is a PR for it but I needed some merge conflicts to be resolved to be merged. I never got to resolving them because I honestly could not figure out how to resolve them. They were too different and incompatible from what I could see. If you want to take another stab at it go ahead. |
I am creating this issue to start a discussion about how to overcome some roadblocks I discovered while trying to address #3505 over at
coreutils
.Issue 1
To get right to it, the implementation currently used by this library is not consistent with GNU's.
Currently that way that
parse_datetime
parses relative strings is that in runs the string through a regex and createsDuration
objects and adds them together. The inconstancy arises when adding months: it simply adds 30 days; GNU does not do this. Under the existing (parse_datetime
) API2022-05-15 +1 month
will land you on2022-06-14
(After you do some refactoring to get adate
back not aDuration
) while running GNU'sdate -d'2022-05-15 +1 month
lands you on2022-06-15
. Now, we could have our API increment months usingMonths::new
which returns aDuration
object but that would change the behavior of the existing API and cause a bunch of tests to fail. Why can't we change the tests? Let me explain.Issue 2
Chrono's API is inconsistent with GNU
While working to solve #3505 I ran into a subtle bug. Addition and subtraction of
Months
in Chrono's API is non-commutative. The TL;DR is thatchrono
does clamping when adding months to their Date APIs. To who understand why this is an issue consider:Output:
While nothing is being commuted here, it shows that addition of months cannot be commutative under an implementation with this behavior. GNU does not do this. Consider:
While GNU is consistent, Chrono is not. In fact it is 3 whole days off (If "off" is even the right word. What is "correct" is highly debatable). The addition of months is complicated because what it means to add months is somewhat arbitrary. Even how GNU adds months seems inconsistent sometimes. Adding 30 days vs. 1 month, 60 days vs. 2 months results in different dates that ostensibly don't have any obvious connective logic. This behavior further serves to frustrate the issue under discussion!
Whats the Point?
I appears that with these issues, bringing
coreutils
'stouch
anddate
APIs consistent with GNU's under any arbitrary string will be extremely difficult due to how inherently differentchrono
and GNU handle the addition and subtraction of months (maybe even other units of time).Options
As touched on earlier, we could tweak
parse_relative_time.rs
to usechrono
'sMonths::new
instead ofDurations::days
and accept that it will cause a slight change in behavior and just rewrite the tests (What I was tempted to ask for permission to do).The other option I considered was having a separate implementation for when you want a
date
back vs. aDuration
but that seems silly. Users would rightfully assume that the separate functions are two sides of the same coin and would be consistent with each other.The core issue of this discussion is that either option the result would be behavior that is inconsistent with GNU is some cases. It doesn't appear possible to get away from it. Given any arbitrary string what is the correct order of operations to get the correct date (as defined by GNU) and is it possible to do it with
chrono
under their existing implementation???For what it is worth my intitial pull request to address #3505 did appear to have the requisite functionality but it is unclear if that is the case in for any arbitrary string. And using that would, again, result in a API behavior change.
These issues don't necessarily have obvious solutions which is why I wanted to bring it to the
uutils
community.The text was updated successfully, but these errors were encountered: