From: Jason Davies Date: Thu, 19 Jul 2012 14:33:28 +0000 (+0100) Subject: Add tests for C1 and support for six-digit years. X-Git-Tag: 2.0.0~83^2~1^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=273b9ef58e32d21a1a151fec2ef7902f42553c73;p=thirdparty%2Fmoment.git Add tests for C1 and support for six-digit years. --- diff --git a/moment.js b/moment.js index 868044d97..f430bc781 100644 --- a/moment.js +++ b/moment.js @@ -30,7 +30,7 @@ aspNetJsonRegex = /^\/?Date\((\-?\d+)/i, // format tokens - formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|zz?|ZZ?)/g, + formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|YYYYY|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|zz?|ZZ?|-)/g, localFormattingTokens = /(LT|LL?L?L?)/g, formattingRemoveEscapes = /(^\[)|(\\)|\]$/g, @@ -42,6 +42,7 @@ parseTokenOneToThreeDigits = /\d{1,3}/, // 0 - 999 parseTokenThreeDigits = /\d{3}/, // 000 - 999 parseTokenFourDigits = /\d{4}/, // 0000 - 9999 + parseTokenFiveToSixDigits = /[+\-]?\d{5,6}/, // -999,999 - 999,999 parseTokenWord = /[0-9a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+/i, // any word characters or numbers parseTokenTimezone = /Z|[\+\-]\d\d:?\d\d/i, // +00:00 -00:00 +0000 -0000 or Z parseTokenT = /T/i, // T (ISO seperator) @@ -104,6 +105,7 @@ 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 : 'p(t.year(),4)', + YYYYY: 'p(t.year(),5)', a : 'm(t.hours(),t.minutes(),!0)', A : 'm(t.hours(),t.minutes(),!1)', H : 't.hours()', @@ -402,6 +404,8 @@ return parseTokenThreeDigits; case 'YYYY': return parseTokenFourDigits; + case 'YYYYY': + return parseTokenFiveToSixDigits; case 'S': case 'SS': case 'SSS': @@ -474,7 +478,8 @@ datePartArray[0] = input + (input > 70 ? 1900 : 2000); break; case 'YYYY' : - datePartArray[0] = ~~Math.abs(input); + case 'YYYYY' : + datePartArray[0] = ~~input; break; // AM / PM case 'a' : // fall through to A diff --git a/test/moment/create.js b/test/moment/create.js index 13486bd43..52befca23 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -294,5 +294,22 @@ exports.create = { test.equal(moment(null), null, "Calling moment(null)"); test.equal(moment('', 'YYYY-MM-DD'), null, "Calling moment('', 'YYYY-MM-DD')"); test.done(); + }, + + "first century" : function(test) { + test.expect(2); + test.equal(moment([0, 0, 1]).format("YYYY-MM-DD"), "0000-01-01", "Year AD 0"); + test.equal(moment([99, 0, 1]).format("YYYY-MM-DD"), "0099-01-01", "Year AD 99"); + test.done(); + }, + + "six digit years" : function(test) { + test.expect(5); + test.equal(moment([-270000, 0, 1]).format("YYYYY-MM-DD"), "-270000-01-01", "format BC 270,000"); + test.equal(moment([ 270000, 0, 1]).format("YYYYY-MM-DD"), "270000-01-01", "format AD 270,000"); + test.equal(moment("-270000-01-01", "YYYYY-MM-DD").toDate().getUTCFullYear(), -270000, "parse BC 270,000"); + test.equal(moment("270000-01-01", "YYYYY-MM-DD").toDate().getUTCFullYear(), 270000, "parse AD 270,000"); + test.equal(moment("+270000-01-01", "YYYYY-MM-DD").toDate().getUTCFullYear(), 270000, "parse AD +270,000"); + test.done(); } };