luxon icon indicating copy to clipboard operation
luxon copied to clipboard

Add `Duration.min()` and `Duration.max()`

Open jirutka opened this issue 1 year ago • 4 comments

Can you please add static methods min and max to the Duration, analogous to the methods in DateTime?

jirutka avatar Oct 10 '23 23:10 jirutka

Hey @icambron,

If you are ok with adding these methods to the Duration class, can I be assigned this issue? I have made some changes regarding this and can raise a PR for it.

shashankbhat10 avatar Oct 14 '23 20:10 shashankbhat10

This definitely is not trivial. The problem is that durations by default use "casual" conversion rules, which can make it so this comparison would not be reflexive or transitive, depending on which unit you use for comparison. For example: 29 days > 4 weeks, using days 4 weeks == 1 month, using weeks 29 days < 1 month, using days

So, 29 days is both larger and smaller than 1 month, considering the above logic.

Also: 4 weeks == 1 month, using weeks; but 4 weeks < 1 month, using days (because 28 days < 30 days)

This gets even more complicated if you have multiple (even different) units on both sides. So really, the user needs to tell which unit to use for the comparison. And even then they might not get the result they want.

diesieben07 avatar Oct 14 '23 20:10 diesieben07

I get what you are saying. I think could be done after any resolution of #1514, right?

shashankbhat10 avatar Oct 14 '23 21:10 shashankbhat10

No, this is an inherent property of the casual conversion matrix that Durations use by default. You can read more about it in the documentation.

diesieben07 avatar Oct 14 '23 21:10 diesieben07