Skip to content

Fix #30 - locale.parse returns null on weekday mismatch. - #132

Open
cpruijsen wants to merge 1 commit into
d3:mainfrom
cpruijsen:fix/issue-30
Open

cpruijsen wants to merge 1 commit into
d3:mainfrom
cpruijsen:fix/issue-30

Conversation

@cpruijsen

Copy link
Copy Markdown

locale.parse accepts a weekday that contradicts the date it is parsed alongside. %a %m/%d/%Y on
"Tue 01/01/1990" returns 1 January 1990, a Monday, rather than null. Per #29 a specified weekday
that does not match the implied date should fail the parse.

src/locale.js only consulted the weekday when a week number was present, because that is the case
where it contributes to the date. When it is redundant it was ignored rather than checked. This adds
the else branch: with a weekday but no week number, compute the day the parsed date falls on and
return null when the two disagree. %u is compared modulo 7, since ISO numbers Sunday as 7 where
getDay uses 0.

Existing tests had to change, and it is worth being explicit about why rather than leaving it to the
diff: "Sun 01/01/1990" and "Wed 02/03/1991" were both asserting successful parses of dates whose
weekday is wrong. 1 January 1990 was a Monday and 3 February 1991 a Sunday. They now use the correct
weekday, and a mismatching case asserts null.

Fixes #30

If a weekday is specified and a week number is not specified, verify
that the parsed weekday matches the weekday implied by the date, and
return null if it does not.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

locale.parse should return null on weekday mismatch.

1 participant