fix(timepicker): fix date overflow on short months #6480
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Change year, month, date setting on time change to be atomic, preventing overflows for short months.
When the time is decremented before midnight such that the date would go back a day there is code to reset the year, month and day, in that order. this causes an issue with shorter months, for example February or April, when on the 1st of these months and the date goes back a day, calling
date.setMonth(monthIndex)
causes an overflow to the next month.For example, February 1st 00:00:00 -> January 31st 23:00:00
Upon calling
date.setMonth(1)
we are essentially setting the date to February 31st which rolls over a few into March.date.setDate(1)
is then called which results in the Date object representing March 1st, instead of February 1st.The fix is a simple one,
date.setFullYear
accepts arguments for year, month and day, allowing these to be set atomically and prevents this overflow issue.Closes #5766