From: Isaac Cambron Date: Mon, 11 Dec 2017 00:27:27 +0000 (-0500) Subject: [bugfix] Fix weekday verification for UTC and offset days (fixes #4227) (#4332) X-Git-Tag: 2.19.4~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1c978ba782b76ead2f67ddabbb53fc32bc87f7fa;p=thirdparty%2Fmoment.git [bugfix] Fix weekday verification for UTC and offset days (fixes #4227) (#4332) * Verify utc date weekdays using local weekday * add tests and change check --- diff --git a/src/lib/create/from-array.js b/src/lib/create/from-array.js index 7626c455d..b5a0911de 100644 --- a/src/lib/create/from-array.js +++ b/src/lib/create/from-array.js @@ -21,7 +21,7 @@ function currentDateArray(config) { // note: all values past the year are optional and will default to the lowest possible value. // [year, month, day , hour, minute, second, millisecond] export function configFromArray (config) { - var i, date, input = [], currentDate, yearToUse; + var i, date, input = [], currentDate, expectedWeekday, yearToUse; if (config._d) { return; @@ -71,6 +71,8 @@ export function configFromArray (config) { } config._d = (config._useUTC ? createUTCDate : createDate).apply(null, input); + expectedWeekday = config._useUTC ? config._d.getUTCDay() : config._d.getDay(); + // Apply timezone offset from input. The actual utcOffset can be changed // with parseZone. if (config._tzm != null) { @@ -82,7 +84,7 @@ export function configFromArray (config) { } // check for mismatching day of week - if (config._w && typeof config._w.d !== 'undefined' && config._w.d !== config._d.getDay()) { + if (config._w && typeof config._w.d !== 'undefined' && config._w.d !== expectedWeekday) { getParsingFlags(config).weekdayMismatch = true; } } diff --git a/src/test/moment/create.js b/src/test/moment/create.js index 8a50eefa1..91fb3a9f3 100644 --- a/src/test/moment/create.js +++ b/src/test/moment/create.js @@ -750,6 +750,30 @@ test('parsing iso week year/week/weekday', function (assert) { assert.equal(moment.utc('2012-W01').format(), '2012-01-02T00:00:00Z', '2012 week 1 (1st Jan Sun)'); }); +test('parsing weekdays verifies the day', function (assert) { + // string with format + assert.ok(!moment('Wed 08-10-2017', 'ddd MM-DD-YYYY').isValid(), 'because day of week is incorrect for the date'); + assert.ok(moment('Thu 08-10-2017', 'ddd MM-DD-YYYY').isValid(), 'because day of week is correct for the date'); +}); + +test('parsing weekday on utc dates verifies day acccording to utc time', function (assert) { + assert.ok(moment.utc('Mon 03:59', 'ddd HH:mm').isValid(), 'Monday 03:59'); +}); + +test('parsing weekday on local dates verifies day acccording to local time', function (assert) { + // this doesn't do much useful if you're not in the US or at least close to it + assert.ok(moment('Mon 03:59', 'ddd HH:mm').isValid(), 'Monday 03:59'); +}); + +test('parsing weekday on utc dates with specified offsets verifies day acccording to that offset', function (assert) { + assert.ok(moment.utc('Mon 03:59 +12:00', 'ddd HH:mm Z', true).isValid(), 'Monday 03:59'); +}); + +test('parsing weekday on local dates with specified offsets verifies day acccording to that offset', function (assert) { + // if you're in the US, these times will all be sometime Sunday, but they shoud parse as Monday + assert.ok(moment('Mon 03:59 +12:00', 'ddd HH:mm Z', true).isValid(), 'Monday 03:59'); +}); + test('parsing week year/week/weekday (dow 1, doy 4)', function (assert) { moment.locale('dow:1,doy:4', {week: {dow: 1, doy: 4}}); @@ -1224,9 +1248,3 @@ test('k, kk', function (assert) { } }); -test('mismatching day-of-week and date', function (assert) { - // string with format - assert.ok(!moment('Wed 08-10-2017', 'ddd MM-DD-YYYY').isValid(), 'because day of week is incorrect for the date'); - assert.ok(moment('Thu 08-10-2017', 'ddd MM-DD-YYYY').isValid(), 'because day of week is correct for the date'); -}); - diff --git a/src/test/moment/zones.js b/src/test/moment/zones.js index dbe9e2902..a7e4f6ce9 100644 --- a/src/test/moment/zones.js +++ b/src/test/moment/zones.js @@ -499,3 +499,8 @@ test('parse zone with a minutes unit abs less than 16 should retain minutes', fu assert.equal(o.zone(), -15); assert.equal(o.hour(), 0); }); + +test('parse zone with weekday on verifies day acccording to the offset', function (assert) { + test.expectedDeprecations(); + assert.ok(moment.parseZone('Mon 03:59 +12:00', 'ddd HH:mm Z', true).isValid(), 'Monday 03:59'); +});