]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix parsing/formatting of first century dates.
authorJason Davies <jason@jasondavies.com>
Wed, 18 Jul 2012 10:13:31 +0000 (11:13 +0100)
committerJason Davies <jason@jasondavies.com>
Wed, 18 Jul 2012 10:13:31 +0000 (11:13 +0100)
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).

moment.js
test/lang/pt-br.js
test/moment/create.js

index c959ab31debf2f52053d38f3f31e6ef8bde6d7a8..868044d97fec65a404ab315196fe4a8577077276 100644 (file)
--- a/moment.js
+++ b/moment.js
             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()',
         }
         // 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;
     }
index 07113d3afa120b4216daadabaa0abd2607fed4b8..9f6b93582ddd5b64c97e0aad8eed962e904dc176 100644 (file)
@@ -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
+};
index a6686ae9bc1ce1624ac88e33bd36c623ddf58d1c..13486bd438cfcaf3732af9700ffb5bfc937a7506 100644 (file)
@@ -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();
     },