From: Tim Wood Date: Mon, 17 Jun 2013 20:48:01 +0000 (-0700) Subject: iso week days should be in the 1-7 range, not 0-6. #699 http://en.wikipedia.org/wiki... X-Git-Tag: 2.1.0~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4f7bc8bb8bcf51c182d7ad3948cbec079a330135;p=thirdparty%2Fmoment.git iso week days should be in the 1-7 range, not 0-6. #699 http://en.wikipedia.org/wiki/ISO_week_date --- diff --git a/moment.js b/moment.js index f6176b6de..3a2868f3e 100644 --- a/moment.js +++ b/moment.js @@ -1462,10 +1462,10 @@ }, isoWeekday : function (input) { - // iso weeks start on monday, which is 1, so we subtract 1 (and add - // 7 for negative mod to work). - var weekday = (this._d.getDay() + 6) % 7; - return input == null ? weekday : this.add("d", input - weekday); + // behaves the same as moment#day except + // as a getter, returns 7 instead of 0 (1-7 range instead of 0-6) + // as a setter, sunday should belong to the previous week. + return input == null ? this.day() || 7 : this.day(this.day() % 7 ? input : input - 7); }, // If passed a language key, it will set the language for this diff --git a/test/moment/format.js b/test/moment/format.js index ddfe34ff1..69702123e 100644 --- a/test/moment/format.js +++ b/test/moment/format.js @@ -260,13 +260,13 @@ exports.format = { "iso weekday formats" : function(test) { test.expect(7); - test.equal(moment([1985, 1, 4]).format('E'), '0', "Feb 4 1985 is Monday -- 0th day"); - test.equal(moment([2029, 8, 18]).format('E'), '1', "Sep 18 2029 is Tuesday -- 1st day"); - test.equal(moment([2013, 3, 24]).format('E'), '2', "Apr 24 2013 is Wednesday -- 2nd day"); - test.equal(moment([2015, 2, 5]).format('E'), '3', "Mar 5 2015 is Thursday -- 3nd day"); - test.equal(moment([1970, 0, 2]).format('E'), '4', "Jan 2 1970 is Friday -- 4th day"); - test.equal(moment([2001, 4, 12]).format('E'), '5', "May 12 2001 is Saturday -- 5th day"); - test.equal(moment([2000, 0, 2]).format('E'), '6', "Jan 2 2000 is Sunday -- 6th day"); + test.equal(moment([1985, 1, 4]).format('E'), '1', "Feb 4 1985 is Monday -- 1st day"); + test.equal(moment([2029, 8, 18]).format('E'), '2', "Sep 18 2029 is Tuesday -- 2nd day"); + test.equal(moment([2013, 3, 24]).format('E'), '3', "Apr 24 2013 is Wednesday -- 3rd day"); + test.equal(moment([2015, 2, 5]).format('E'), '4', "Mar 5 2015 is Thursday -- 4th day"); + test.equal(moment([1970, 0, 2]).format('E'), '5', "Jan 2 1970 is Friday -- 5th day"); + test.equal(moment([2001, 4, 12]).format('E'), '6', "May 12 2001 is Saturday -- 6th day"); + test.equal(moment([2000, 0, 2]).format('E'), '7', "Jan 2 2000 is Sunday -- 7th day"); test.done(); }, @@ -283,7 +283,7 @@ exports.format = { test.equal(moment([2001, 4, 14]).format('e'), '5', "May 14 2001 is Monday -- 5th day"); test.equal(moment([2000, 0, 4]).format('e'), '6', "Jan 4 2000 is Tuesday -- 6th day"); - test.done() + test.done(); }, "toString is just human readable format" : function(test) { diff --git a/test/moment/weekday.js b/test/moment/weekday.js index acf1b0219..ef1571e34 100644 --- a/test/moment/weekday.js +++ b/test/moment/weekday.js @@ -7,17 +7,56 @@ exports.week_year = { for (i = 0; i < 7; ++i) { moment.lang('dow:' + i + ',doy:6', {week: {dow: i, doy: 6}}); - test.equal(moment([1985, 1, 4]).isoWeekday(), 0, "Feb 4 1985 is Monday -- 0th day"); - test.equal(moment([2029, 8, 18]).isoWeekday(), 1, "Sep 18 2029 is Tuesday -- 1st day"); - test.equal(moment([2013, 3, 24]).isoWeekday(), 2, "Apr 24 2013 is Wednesday -- 2nd day"); - test.equal(moment([2015, 2, 5]).isoWeekday(), 3, "Mar 5 2015 is Thursday -- 3nd day"); - test.equal(moment([1970, 0, 2]).isoWeekday(), 4, "Jan 2 1970 is Friday -- 4th day"); - test.equal(moment([2001, 4, 12]).isoWeekday(), 5, "May 12 2001 is Saturday -- 5th day"); - test.equal(moment([2000, 0, 2]).isoWeekday(), 6, "Jan 2 2000 is Sunday -- 6th day"); + test.equal(moment([1985, 1, 4]).isoWeekday(), 1, "Feb 4 1985 is Monday -- 1st day"); + test.equal(moment([2029, 8, 18]).isoWeekday(), 2, "Sep 18 2029 is Tuesday -- 2nd day"); + test.equal(moment([2013, 3, 24]).isoWeekday(), 3, "Apr 24 2013 is Wednesday -- 3rd day"); + test.equal(moment([2015, 2, 5]).isoWeekday(), 4, "Mar 5 2015 is Thursday -- 4th day"); + test.equal(moment([1970, 0, 2]).isoWeekday(), 5, "Jan 2 1970 is Friday -- 5th day"); + test.equal(moment([2001, 4, 12]).isoWeekday(), 6, "May 12 2001 is Saturday -- 6th day"); + test.equal(moment([2000, 0, 2]).isoWeekday(), 7, "Jan 2 2000 is Sunday -- 7th day"); } test.done(); }, + "iso weekday setter" : function(test) { + test.expect(27); + + var a = moment([2011, 0, 10]); + test.equal(moment(a).isoWeekday(1).date(), 10, 'set from mon to mon'); + test.equal(moment(a).isoWeekday(4).date(), 13, 'set from mon to thu'); + test.equal(moment(a).isoWeekday(7).date(), 16, 'set from mon to sun'); + test.equal(moment(a).isoWeekday(-6).date(), 3, 'set from mon to last mon'); + test.equal(moment(a).isoWeekday(-3).date(), 6, 'set from mon to last thu'); + test.equal(moment(a).isoWeekday(0).date(), 9, 'set from mon to last sun'); + test.equal(moment(a).isoWeekday(8).date(), 17, 'set from mon to next mon'); + test.equal(moment(a).isoWeekday(11).date(), 20, 'set from mon to next thu'); + test.equal(moment(a).isoWeekday(14).date(), 23, 'set from mon to next sun'); + + a = moment([2011, 0, 13]); + test.equal(moment(a).isoWeekday(1).date(), 10, 'set from thu to mon'); + test.equal(moment(a).isoWeekday(4).date(), 13, 'set from thu to thu'); + test.equal(moment(a).isoWeekday(7).date(), 16, 'set from thu to sun'); + test.equal(moment(a).isoWeekday(-6).date(), 3, 'set from thu to last mon'); + test.equal(moment(a).isoWeekday(-3).date(), 6, 'set from thu to last thu'); + test.equal(moment(a).isoWeekday(0).date(), 9, 'set from thu to last sun'); + test.equal(moment(a).isoWeekday(8).date(), 17, 'set from thu to next mon'); + test.equal(moment(a).isoWeekday(11).date(), 20, 'set from thu to next thu'); + test.equal(moment(a).isoWeekday(14).date(), 23, 'set from thu to next sun'); + + a = moment([2011, 0, 16]); + test.equal(moment(a).isoWeekday(1).date(), 10, 'set from sun to mon'); + test.equal(moment(a).isoWeekday(4).date(), 13, 'set from sun to thu'); + test.equal(moment(a).isoWeekday(7).date(), 16, 'set from sun to sun'); + test.equal(moment(a).isoWeekday(-6).date(), 3, 'set from sun to last mon'); + test.equal(moment(a).isoWeekday(-3).date(), 6, 'set from sun to last thu'); + test.equal(moment(a).isoWeekday(0).date(), 9, 'set from sun to last sun'); + test.equal(moment(a).isoWeekday(8).date(), 17, 'set from sun to next mon'); + test.equal(moment(a).isoWeekday(11).date(), 20, 'set from sun to next thu'); + test.equal(moment(a).isoWeekday(14).date(), 23, 'set from sun to next sun'); + + test.done(); + }, + "weekday first day of week Sunday (dow 0)": function(test) { test.expect(7);