-
Notifications
You must be signed in to change notification settings - Fork 174
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
Make applyDuration
reversible
#298
base: main
Are you sure you want to change the base?
Conversation
0eda15b
to
d37caed
Compare
if (duration.sign < 0) { | ||
for (const action of durationApplicationActionsBackward) action(r, duration) | ||
} else { | ||
for (const action of durationApplicationActionsForward) action(r, duration) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd prefer to see this without the for loops. I'm a little concerned that they might cause performance regressions (though I have no empirical evidence for that), but more importantly is that I'm not sure they aid readability, and I don't think the unrolled loops would result in a bigger file overall;
export function applyDuration(date: Date | number, duration: Duration): Date {
const r = new Date(date)
if (duration.sign < 0) {
r.setUTCFullYear(r.getUTCFullYear() + duration.years)
r.setUTCMonth(r.getUTCMonth() + duration.months)
r.setUTCDate(r.getUTCDate() + duration.weeks * 7 + duration.days)
r.setUTCHours(r.getUTCHours() + duration.hours)
r.setUTCMinutes(r.getUTCMinutes() + duration.minutes)
r.setUTCSeconds(r.getUTCSeconds() + duration.seconds)
} else {
r.setUTCSeconds(r.getUTCSeconds() + duration.seconds)
r.setUTCMinutes(r.getUTCMinutes() + duration.minutes)
r.setUTCHours(r.getUTCHours() + duration.hours)
r.setUTCDate(r.getUTCDate() + duration.weeks * 7 + duration.days)
r.setUTCMonth(r.getUTCMonth() + duration.months)
r.setUTCFullYear(r.getUTCFullYear() + duration.years)
}
}
Unless there's a good reason I am missing about using the loop? If that is the case and they need to be kept, I'd quite like to see some benchmarks to see the performance impact of keeping them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's mostly just for DRY, if you say so then I will do it that way.
It looks as though there's a test failure - that perhaps should be changed with this change? |
The failing test is not because of or affected by this change as stated in my original comment (it also appears that quite a few tests use In the test of the check you run:
it is |
(This is part of a set of multiple pull requests looking to overhaul the calculation functions.)
When the duration to be applied is negative, this pull request makes the application order of the components reversed compared to when the duration is positive, so that adding a duration to a date then subtracting the same amount yields the same date.
Note: One test is failing on my end from the current upstream code:
This is caused by the current implementation of
roundToNearestUnit
computing the future date to be in 2027, resulting in a 3-year difference as it only considers the year alone in this case. This will be fixed with a reimplementation of the function in one of my other pull requests.