Skip to content

Conversation

@jcarstairs-scottlogic
Copy link

#3429 demonstrated that roundToNearestMinutes() did not produce expected behaviour. This PR makes a few changes so that roundToNearestMinutes() fixes that misbehaviour...

  • Previously, there was an inexplicable addedMinutes variable which added minutes on to the result. There was no obvious reason for this, and it could only ever cause the result to go wrong: indeed, this seemed to be the chief culprit in the case of roundToNearestMinutes producing unexpected results #3429. I have removed this variable
  • Fixing this behaviour caused the tests to fail. This was because the tests had been engineered to expect the wrong results. I fixed this, and while I was there, removed duplicate tests and replaced test inputs with boundary cases
  • Fixing the tests to expect what they ought to expect and putting in boundary cases caused further test failures. This in turn was partly because there was no account made of the milliseconds field of the input. Now, milliseconds are accounted for, which allowed some more of the tests to pass.
  • Finally, the remaining issue was that the rounding method was expected to default to round, but in fact it was defaulting to trunc. I have fixed this as well.

Now, all tests pass, and I have manually checked that this should fix #3429

@mp035
Copy link

mp035 commented Sep 21, 2023

I just came across this bug, and was going to submit my own PR, but upon reveiw, @jcarstairs-scottlogic , your PR is perfect and goes even deeper to completely solve the issue. Great work. I really hope the maintainers pull it in soon 😉

I note however that the date-fns documentation clearly states that the default rounding method is 'trunc'. You may need to add the documentation change to your PR. Although that is a breaking change, it looks like the function hasn't performed correct truncation anyway, so anything you break would have been relying on broken code. 😧

@kossnocorp
Copy link
Member

Thank you for the PR, I'm working to rebase and finish it.

Finally, the remaining issue was that the rounding method was expected to default to round, but in fact it was defaulting to trunc. I have fixed this as well.

Trunc is the default behavior across the library, so it should be the default indeed.

@kossnocorp
Copy link
Member

Thank you so much for your work. I'll make sure to mention you in the release notes. I used this PR as it adds more edge case tests and also was proposed a bit earlier: #3132

@kossnocorp kossnocorp closed this Jan 9, 2024
@kossnocorp
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

No open projects
Status: Shipped

Development

Successfully merging this pull request may close these issues.

roundToNearestMinutes producing unexpected results

3 participants