fix(plugin): parse timezone offset in customParseFormat strict mode - #3182
Open
stevechen256-source wants to merge 1 commit into
Open
fix(plugin): parse timezone offset in customParseFormat strict mode#3182stevechen256-source wants to merge 1 commit into
stevechen256-source wants to merge 1 commit into
Conversation
When parsing with the customParseFormat plugin in strict mode, the round-trip comparison reformats the parsed date using the local timezone offset, so a valid input whose offset differs from the local one (e.g. an ISO 8601 'Z' string parsed in a non-UTC timezone) was always rejected as invalid. Compare the input against the reformatted output in the input's own offset (equivalent to rendering it in UTC) whenever a Z/ZZ token was parsed, so offsets round-trip correctly while out-of-range dates are still rejected.
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.
What & Why
customParseFormatstrict mode rejects valid date strings that contain a timezone offset (Z/ZZtoken) whenever the input offset differs from the local timezone. For example, on a non-UTC machine:The root cause is the strict-mode round-trip check (
date != this.format(format)):format()renders aZ/ZZtoken using the local timezone offset, so the reformatted string legitimately differs from the input (both the offset lexeme and the wall-clock fields), and the input is wrongly discarded.This has been reported several times: #2712, #2730, #2741, #2607.
Changes
parseFormattedInputnow also returns the parsed timezone offset.Z,+09,+09:00,+0900, …) to the corresponding UTC form before comparing. This keeps the existing round-trip semantics (padding, literal characters, out-of-range dates such as2024-02-31are still rejected) while making offsets round-trip correctly.Z, offsets like+09/+00:00,Zagainst aZZtoken, and the invalid2024-02-31case.Test plan
npm testpasses: 4 timezone passes oftimezone.test(6/6 each), 4 timezone-plugin passes (54/54 each), full suite799 passed, 799 totalwith the repo's100%line-coverage threshold.TZvalues (UTC, Asia/Shanghai, America/New_York, Europe/Paris, Pacific/Auckland): parsedvalueOf()matchesmomentexactly for all new cases, and out-of-range dates stay invalid.eslintclean on the changed files.