fix: parseJSONDate incorrectly adding 1900 to years 0-99 - #741
Merged
Conversation
JavaScript's Date constructor (and Date.UTC) interpret years 0-99 as 1900+year. We now shift the result back by exactly 1900 years, but only for years in that range. The shift is relative (not an absolute setYear) so that timezone offsets which push the instant across a year boundary are preserved. Adds coverage via the existing convertToModel/mapToModel date mapping tests, including leap-day and year-crossing-offset cases. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Why
parseJSONDatewas returning the wrong year for dates with a year in the range 0-99. For example,parseJSONDate("0001-01-01", "date")producedTue Jan 01 1901instead of year 1. This is because JavaScript'sDateconstructor (andDate.UTC) interpret a year of 0-99 as1900 + year.Approach
After constructing the
Date, we now shift the result back by exactly 1900 years, but only when the parsed year falls in the 0-99 range. The shift is relative (getFullYear() - 1900) rather than an absolutesetFullYear(parsedYear). This matters because the constructor may normalize the date across a year boundary (e.g. a timezone offset that pushes the UTC instant into the previous year); a relative shift preserves that normalization, whereas an absolute set would clobber it and produce an off-by-one-year result.The fix is applied consistently across all three relevant branches: date-only, datetime without offset, and datetime with a
Z/explicit offset. The time-only branch is unaffected (it already uses the current year by design).Testing
Coverage was added through the existing
convertToModel/mapToModeldate-mapping table inmodel.toModel.spec.ts(runs against both mapping variants), covering:0001-01-01,0099-06-15,0001-01-01T14:00:00,0001-06-15T12:00:00Z)0004-02-29)2020-01-01T00:30:00+01:00)0001-01-01T00:30:00+01:00)year < 100boundary (0100-01-01), which must be left untouchedAll 668 coalesce-vue tests pass and
tscis clean.