From: Andrei Cocorean Date: Sun, 6 Apr 2014 17:59:33 +0000 (+0200) Subject: Parse quarter from string X-Git-Tag: 2.6.0~7^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ca4184ae21f239dc90a1f0be398469b3b07eeecd;p=thirdparty%2Fmoment.git Parse quarter from string --- diff --git a/moment.js b/moment.js index 256d96c1d..2307b3700 100644 --- a/moment.js +++ b/moment.js @@ -53,7 +53,7 @@ isoDurationRegex = /^(-)?P(?:(?:([0-9,.]*)Y)?(?:([0-9,.]*)M)?(?:([0-9,.]*)D)?(?:T(?:([0-9,.]*)H)?(?:([0-9,.]*)M)?(?:([0-9,.]*)S)?)?|([0-9,.]*)W)$/, // format tokens - formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|S{1,4}|X|zz?|ZZ?|.)/g, + formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Q|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|S{1,4}|X|zz?|ZZ?|.)/g, localFormattingTokens = /(\[[^\[]*\])|(\\)?(LT|LL?L?L?|l{1,4})/g, // parsing token regexes @@ -949,6 +949,8 @@ function getParseRegexForToken(token, config) { var a, strict = config._strict; switch (token) { + case 'Q': + return parseTokenOneDigit; case 'DDDD': return parseTokenThreeDigits; case 'YYYY': @@ -1040,6 +1042,12 @@ var a, datePartArray = config._a; switch (token) { + // QUARTER + case 'Q': + if (input != null) { + datePartArray[MONTH] = (toInt(input) - 1) * 3; + } + break; // MONTH case 'M' : // fall through to MM case 'MM' : diff --git a/test/moment/create.js b/test/moment/create.js index afeb79af4..3f6beaf62 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -227,6 +227,7 @@ exports.create = { "string with format" : function (test) { moment.lang('en'); var a = [ + ['YYYY-Q', '2014-4'], ['MM-DD-YYYY', '12-02-1999'], ['DD-MM-YYYY', '12-02-1999'], ['DD/MM/YYYY', '12/02/1999'], @@ -661,6 +662,8 @@ exports.create = { }, "strict parsing" : function (test) { + test.equal(moment("2014-", "YYYY-Q", true).isValid(), false, "fail missing quarter"); + test.equal(moment("2012-05", "YYYY-MM", true).format("YYYY-MM"), "2012-05", "parse correct string"); test.equal(moment(" 2012-05", "YYYY-MM", true).isValid(), false, "fail on extra whitespace"); test.equal(moment("foo 2012-05", "[foo] YYYY-MM", true).format('YYYY-MM'), "2012-05", "handle fixed text");