Skip to content
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

Validate input for improper leap dates and short months #72

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import {
utcYear
} from "d3-time";

function getMaxDay(m, y) {
if (m == 3 || m == 5 || m == 8 || m == 10) { // April, June, September, November
return 30;
} else if (m == 0 || m == 2 || m == 4 || m == 6 || m == 7 || m == 9 || m == 11) { // January, March, May, July, August, October, December
return 31;
} else if (m == 1) { // February
return y % 4 == 0 ? 29 : 28; // Leap years are divisible by 4
} else {
return null;
}
}

function localDate(d) {
if (0 <= d.y && d.y < 100) {
var date = new Date(-1, d.m, d.d, d.H, d.M, d.S, d.L);
Expand Down Expand Up @@ -209,6 +221,11 @@ export default function formatLocale(locale) {
// If the month was not specified, inherit from the quarter.
if (d.m === undefined) d.m = "q" in d ? d.q : 0;

// Check for invalid days (leap days/short months)
if ((d.d > getMaxDay(d.m, d.y) || d.d < 1)){
return null;
}

// Convert day-of-week and week-of-year to day-of-year.
if ("V" in d) {
if (d.V < 1 || d.V > 53) return null;
Expand Down
7 changes: 7 additions & 0 deletions test/utcParse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,10 @@ tape("utcParse(\"%s.%f\")(date) parses UNIX timetamps in seconds and microsecond
test.deepEqual(p("631197296.789000"), date.utc(1990, 0, 1, 12, 34, 56, 789));
test.end();
});

tape("utcParse throws error when given improper leap years", function(test) {
var p = timeFormat.utcParse("%d %m %y");
test.deepEqual(p("29 02 01"), null);
test.deepEqual(p("31 11 01"), null);
test.end();
});