-
Notifications
You must be signed in to change notification settings - Fork 508
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
Operations on NaiveDate are not commutative. #1345
Comments
This is expected, and documented:
Note the use of clamped. |
Thank you for your reply. However, I'm not sure I understand. I saw this but it didn't appear to be relevant to my issue. Why would clamping of days result in operations being non-commutative? |
Because |
Why not? I suppose you are saying one results in clamping while the other doesn't? I'm sorry, I am still not sure I follow. In your example nothing is being commuted. |
You can try the statements I posted in my last comment and see why not, right? I understand in my example there is no commutation but I think it shows that any expression containing |
I appreciate you trying to help me. I won't be able to run your example until later this evening. For now, I can be satisfied with the problem being tied to how addition of months is implemented. |
It is not really a question of how things are implemented in chrono. Working with dates is just tricky with all sorts of failure cases because months and years don't have a fixed number of days. (Let's forget the stuff when you add the time-of-day.) We have Is there a specific problem you are trying to solve that we can help with? |
Yes, I am building a parser that can take an arbitrary string such as "2022-05-15 + 1 Month -10 years + 10 hours" and give the resultant date back. The problem I ran into is that the date returned is in some cases dependant on the order of operations. I was tempted to ask what the "correct" order of operations was but I felt that it may be too arbitrary. |
Ah, cool. You may want to read a bit about the plans in #1282. |
Thank you! Grouping operations is very helpful advice. Having looked at #1282 it sounds like a great feature and may help with our issues over at coreutils. |
When taking an arbitrary date,
2022-05-15
for example, and performing operations on that date, the order of operations does appear to matter.For example, taking the date above and subtracting 20 years, then adding 111 months, then 1000 days is not the same as taking the same date, adding 1000 days, subtracting 20 years and adding 111 months. I have pasted some code below that shows this:
The 2nd assertion fails with the
date
variable showing2014-05-08
, which is 3 days off from the expected2014-05-11
.The text was updated successfully, but these errors were encountered: