]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[bugfix] Fix weekday verification for UTC and offset days (fixes #4227) (#4332)
authorIsaac Cambron <isaac@isaaccambron.com>
Mon, 11 Dec 2017 00:27:27 +0000 (19:27 -0500)
committerKunal Marwaha <marwahaha@berkeley.edu>
Mon, 11 Dec 2017 00:27:27 +0000 (19:27 -0500)
* Verify utc date weekdays using local weekday

* add tests and change check

src/lib/create/from-array.js
src/test/moment/create.js
src/test/moment/zones.js

index 7626c455d56b1c325b4d637d0a7955b5cb852410..b5a0911de1103a7448bf2bf1bf4df4e4a13245f2 100644 (file)
@@ -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;
     }
 }
index 8a50eefa15dccd57b9c26f94fbf9018cb971f7b6..91fb3a9f3ac1d3f18795d6d61c00d21fc89ceebb 100644 (file)
@@ -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');
-});
-
index dbe9e2902c11d706315500c3ad4c014471ea0ca9..a7e4f6ce9444a6b7b722164fbb1ef9251e57e2b3 100644 (file)
@@ -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');
+});