From: Iskren Chernev Date: Wed, 22 Oct 2014 18:00:30 +0000 (-0700) Subject: Detect hours above 12 with h/hh tokens X-Git-Tag: 2.8.4~11^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=526fc6a9ac20515fff00b1cbaa3a652e5666da69;p=thirdparty%2Fmoment.git Detect hours above 12 with h/hh tokens Update the docs about parsingFlags to include bigHour key. Fixes #1734 --- diff --git a/moment.js b/moment.js index 46340bb98..c83d5dd22 100644 --- a/moment.js +++ b/moment.js @@ -731,7 +731,8 @@ if (m._strict) { m._isValid = m._isValid && m._pf.charsLeftOver === 0 && - m._pf.unusedTokens.length === 0; + m._pf.unusedTokens.length === 0 && + m._pf.bigHour === undefined; } } return m._isValid; @@ -1233,11 +1234,13 @@ case 'A' : config._isPm = config._locale.isPM(input); break; - // 24 HOUR - case 'H' : // fall through to hh - case 'HH' : // fall through to hh + // HOUR case 'h' : // fall through to hh case 'hh' : + config._pf.bigHour = true; + /* falls through */ + case 'H' : // fall through to HH + case 'HH' : datePartArray[HOUR] = toInt(input); break; // MINUTE @@ -1502,6 +1505,10 @@ config._pf.unusedInput.push(string); } + // clear _12h flag if hour is <= 12 + if (config._pf.bigHour === true && config._a[HOUR] <= 12) { + config._pf.bigHour = undefined; + } // handle am pm if (config._isPm && config._a[HOUR] < 12) { config._a[HOUR] += 12; @@ -1510,7 +1517,6 @@ if (config._isPm === false && config._a[HOUR] === 12) { config._a[HOUR] = 0; } - dateFromConfig(config); checkOverflow(config); } diff --git a/test/moment/is_valid.js b/test/moment/is_valid.js index c991eaa14..8a5bb9b61 100644 --- a/test/moment/is_valid.js +++ b/test/moment/is_valid.js @@ -46,6 +46,16 @@ exports.isValid = { test.done(); }, + 'h/hh with hour > 12' : function (test) { + test.ok(moment('06/20/2014 11:51 PM', 'MM/DD/YYYY hh:mm A', true).isValid(), '11 for hh'); + test.ok(moment('06/20/2014 11:51 AM', 'MM/DD/YYYY hh:mm A', true).isValid(), '11 for hh'); + test.ok(moment('06/20/2014 23:51 PM', 'MM/DD/YYYY hh:mm A').isValid(), 'non-strict validity 23 for hh'); + test.ok(moment('06/20/2014 23:51 PM', 'MM/DD/YYYY hh:mm A').parsingFlags().bigHour, 'non-strict bigHour 23 for hh'); + test.ok(!moment('06/20/2014 23:51 PM', 'MM/DD/YYYY hh:mm A', true).isValid(), 'validity 23 for hh'); + test.ok(moment('06/20/2014 23:51 PM', 'MM/DD/YYYY hh:mm A', true).parsingFlags().bigHour, 'bigHour 23 for hh'); + test.done(); + }, + 'array bad date leap year' : function (test) { test.expect(8);