]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Detect hours above 12 with h/hh tokens
authorIskren Chernev <iskren.chernev@gmail.com>
Wed, 22 Oct 2014 18:00:30 +0000 (11:00 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 17 Nov 2014 06:54:30 +0000 (22:54 -0800)
Update the docs about parsingFlags to include bigHour key.
Fixes #1734

moment.js
test/moment/is_valid.js

index 46340bb98a5e11e09c5c9599fa03647d15eefd55..c83d5dd220c6d931492fb75058a5bbd736ee33e8 100644 (file)
--- a/moment.js
+++ b/moment.js
             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;
         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
             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;
         if (config._isPm === false && config._a[HOUR] === 12) {
             config._a[HOUR] = 0;
         }
-
         dateFromConfig(config);
         checkOverflow(config);
     }
index c991eaa149a511f395f6fb916c3147c7d57b9787..8a5bb9b613908f1ad54448ca7de068b35cd9b24a 100644 (file)
@@ -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);