-
Notifications
You must be signed in to change notification settings - Fork 741
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
Fix Duration#normalize, shiftTo and shiftToAll #1498
Fix Duration#normalize, shiftTo and shiftToAll #1498
Conversation
Moving fractional parts of bigger units into smaller units is totally something normalize() should do |
A couple of questions about this:
|
Yes, fractions will be moved down as much as possible. Or, worded differently, only the smallest unit should have fractions after normalization.
Not as far as I can tell, because the 2nd call would not find any fractions to move to smaller units. Do you have a situation in mind where this could happen?
The matrix can be configured on the Duration itself and is used for more than just normalization. You can find out more in the documentation. |
Thanks very much @diesieben07 - that's reassuring, and resolves all my questions. Also my apologies - I didn't find that section of the documentation. It's an enjoyable read! The concern I had about a non-idempotent case was not a realistic one as long as the normalization is a strictly a single-pass greatest-to-smallest: I had wondered about, for example, fractional years being shifted down to |
Hold on, maybe my concern returns due to this in the API docs:
As usual I should read and test more before commenting. I will take more time to look into this soon. So conversion 'up' could occur? |
Yes, conversion up will occur, but only for parts of lower order units that wholly fit into higher order units. This cannot be in conflict with the first part, because we only convert fractional parts of the higher order units downwards. This means that those parts will never be considered for upward conversion, because they would not fit into an integer part of the higher unit. Example: |
@diesieben07 Ok; experimenting with this has reassured me but I did find one more caveat:
I understand that the |
This fixes #1496 by introducing an additional conversion step in
Duration#normalize
/normalizeValues
.This step converts any fractions in larger units into lower units, if possible.
For example:
{ years: 2.5, days: 0, hours: 0 }
becomes{ years: 2, days: 182, hours: 12 }
(assuming casual conversions).This also affects
shiftTo
andshiftToAll
. I have added tests for all three methods to test this behavior.Note: This alters the behavior of
normalize
, because previouslynormalize
would not do this.{years: 2.5, days: 0}
would stay as-is. I would argue that that is a bug however, which is fixed by this pull request.shiftTo
(and by extensionshiftToAll
) did do this, prior to callingnormalize
, however I would argue that this is something that should be handled bynormalize
.