From: Jason Davies Date: Wed, 18 Jul 2012 10:13:31 +0000 (+0100) Subject: Fix parsing/formatting of first century dates. X-Git-Tag: 1.7.0~5^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f38f021200d1237ff65d0be9671ea384b36f5a5c;p=thirdparty%2Fmoment.git Fix parsing/formatting of first century dates. According to ECMA-262, `new Date(y, …)` will auto-convert `y` to `1900 + y` if `0 ≤ y ≤ 99`, hence setFullYear or setUTCFullYear is used instead. This also zero-pads the year for the "YYYY" format, since the year may not necessarily have four digits. Lastly, the pt-br test is adjusted to test for the first day of the month instead of the zeroth (which was causing it to fail). --- diff --git a/moment.js b/moment.js index c959ab31d..868044d97 100644 --- a/moment.js +++ b/moment.js @@ -103,7 +103,7 @@ dddd : 'v("weekdays",t.day())', w : '(a=new Date(t.year(),t.month(),t.date()-t.day()+5),b=new Date(a.getFullYear(),0,4),a=~~((a-b)/864e5/7+1.5))', YY : 'p(t.year()%100,2)', - YYYY : 't.year()', + YYYY : 'p(t.year(),4)', a : 'm(t.hours(),t.minutes(),!0)', A : 'm(t.hours(),t.minutes(),!1)', H : 't.hours()', @@ -274,8 +274,14 @@ } // we store whether we used utc or not in the input array input[7] = asUTC; - date = asUTC ? new Date(Date.UTC.apply({}, input)) : - new Date(input[0], input[1], input[2], input[3], input[4], input[5], input[6]); + date = new Date(0); + if (asUTC) { + date.setUTCFullYear(input[0], input[1], input[2]); + date.setUTCHours(input[3], input[4], input[5], input[6]); + } else { + date.setFullYear(input[0], input[1], input[2]); + date.setHours(input[3], input[4], input[5], input[6]); + } date._a = input; return date; } diff --git a/test/lang/pt-br.js b/test/lang/pt-br.js index 07113d3af..9f6b93582 100644 --- a/test/lang/pt-br.js +++ b/test/lang/pt-br.js @@ -105,7 +105,7 @@ exports["lang:pt-br"] = { var expected = 'Janeiro Jan_Fevereiro Fev_Março Mar_Abril Abr_Maio Mai_Junho Jun_Julho Jul_Agosto Ago_Setembro Set_Outubro Out_Novembro Nov_Dezembro Dez'.split("_"); var i; for (i = 0; i < expected.length; i++) { - test.equal(moment([2011, i, 0]).format('MMMM MMM'), expected[i], expected[i]); + test.equal(moment([2011, i, 1]).format('MMMM MMM'), expected[i], expected[i]); } test.done(); }, @@ -238,4 +238,4 @@ exports["lang:pt-br"] = { test.equal(weeksFromNow.calendar(), weeksFromNow.format('L'), "in 2 weeks"); test.done(); } -}; \ No newline at end of file +}; diff --git a/test/moment/create.js b/test/moment/create.js index a6686ae9b..13486bd43 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -92,9 +92,9 @@ exports.create = { "empty string with formats" : function(test) { test.expect(3); - test.equal(moment(' ', 'MM').format('YYYY-MM-DD HH:mm:ss'), '1900-01-01 00:00:00', 'should not break if input is an empty string'); - test.equal(moment(' ', 'DD').format('YYYY-MM-DD HH:mm:ss'), '1900-01-01 00:00:00', 'should not break if input is an empty string'); - test.equal(moment(' ', ['MM', "DD"]).format('YYYY-MM-DD HH:mm:ss'), '1900-01-01 00:00:00', 'should not break if input is an empty string'); + test.equal(moment(' ', 'MM').format('YYYY-MM-DD HH:mm:ss'), '0000-01-01 00:00:00', 'should not break if input is an empty string'); + test.equal(moment(' ', 'DD').format('YYYY-MM-DD HH:mm:ss'), '0000-01-01 00:00:00', 'should not break if input is an empty string'); + test.equal(moment(' ', ['MM', "DD"]).format('YYYY-MM-DD HH:mm:ss'), '0000-01-01 00:00:00', 'should not break if input is an empty string'); test.done(); },