From 208d48edfac3db8afad6da038b7906444bd8bc28 Mon Sep 17 00:00:00 2001 From: guilloux-m Date: Mon, 7 Dec 2015 16:23:43 +0100 Subject: [PATCH 1/2] test(date): a date should be valid even a daylight saving time In France, difference between UTC time and local time is about 2 or 1 hour depending on summer or winter time (see daylight saving time : http://www.timeanddate.com/time/dst/2002.html). Date and date-time validation failed if : -the test date is the last day of a month; -AND the daylight saving time occurs the last day of this month. For example: "2002-03-31T22:00:00.00Z" in France time zone. --- tests/our-tests/format.json | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/our-tests/format.json b/tests/our-tests/format.json index 6e5d296..eab4724 100644 --- a/tests/our-tests/format.json +++ b/tests/our-tests/format.json @@ -21,6 +21,11 @@ "data": "1998-12-31T23:59:60Z", "valid": true }, + { + "description": "matches schema (even on daylight saving time)", + "data": "2002-03-31T22:00:00.000Z", + "valid": true + }, { "description": "does not match schema (invalid mday)", "data": "2013-02-29T03:40:15Z", @@ -51,6 +56,11 @@ "data": "2013-02-08", "valid": true }, + { + "description": "matches schema (even on daylight saving time)", + "data": "2002-03-31", + "valid": true + }, { "description": "does not match schema (invalid mday)", "data": "2013-02-29", From b6a4fe7700e3b3fbab278862d40ad5e2f0932810 Mon Sep 17 00:00:00 2001 From: guilloux-m Date: Mon, 7 Dec 2015 16:38:27 +0100 Subject: [PATCH 2/2] fix(date): daylight saving time issue To avoid daylight saving time issue, date should be instanciate regardless local time zone (i.e. in UTC time). --- lib/suites/draft-04/keywords/formats/date-time.js | 9 ++++++--- lib/suites/draft-04/keywords/formats/date.js | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/suites/draft-04/keywords/formats/date-time.js b/lib/suites/draft-04/keywords/formats/date-time.js index e9b8c39..ce23ae2 100644 --- a/lib/suites/draft-04/keywords/formats/date-time.js +++ b/lib/suites/draft-04/keywords/formats/date-time.js @@ -28,9 +28,12 @@ module.exports = function(config) { sec >= 0 && sec <= 60 // it’s 60 during a leap second ) { - var d = new Date(year, (month - 1) + 1); // the next month - var lastDay = new Date(d - 86400000); - if (mday <= lastDay.getDate()) { + var d = new Date("0000-01-01T00:00:00.000Z"); // Initialize a date in UTC time + d.setUTCFullYear(year); + d.setUTCMonth((month - 1) + 1); // the next month + var lastDay = new Date(d - 86400000); // the day before + + if (mday <= lastDay.getUTCDate()) { // [day-of-month is valid] diff --git a/lib/suites/draft-04/keywords/formats/date.js b/lib/suites/draft-04/keywords/formats/date.js index 071973b..7657a40 100644 --- a/lib/suites/draft-04/keywords/formats/date.js +++ b/lib/suites/draft-04/keywords/formats/date.js @@ -22,9 +22,12 @@ module.exports = function(config) { mday >= 1 && mday <= 31 ) { - var d = new Date(year, (month - 1) + 1); // the next month - var lastDay = new Date(d - 86400000); - if (mday <= lastDay.getDate()) { + var d = new Date("0000-01-01T00:00:00.000Z"); // Initialize a date in UTC time + d.setUTCFullYear(year); + d.setUTCMonth((month - 1) + 1); // the next month + var lastDay = new Date(d - 86400000); // the day before + + if (mday <= lastDay.getUTCDate()) { // [day-of-month is valid] valid = true; }