]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Support meridiemHour in locales
authorIskren Chernev <iskren.chernev@gmail.com>
Fri, 26 Dec 2014 10:28:54 +0000 (12:28 +0200)
committerIskren Chernev <iskren.chernev@gmail.com>
Fri, 26 Dec 2014 10:29:30 +0000 (12:29 +0200)
Some meridiem tokens span across noon, so there is no clear am/pm equivalent.
To properly parse such locales one needs the token AND the hour together to
produce a new hour (that might be same as hour or hour + 12). Also hour 12 has
special meaning

moment.js

index e93c2e54bf9855fd524c4c3a2029fcaade6bc18f..227bf0aad56eb2bc6a0f003cd0c4d0f0f093d537 100644 (file)
--- a/moment.js
+++ b/moment.js
     formatTokenFunctions.DDDD = padToken(formatTokenFunctions.DDD, 3);
 
 
+    function meridiemFixWrap(locale, hour, meridiem) {
+        var isPm;
+
+        if (meridiem == null) {
+            // nothing to do
+            return hour;
+        }
+        if (locale.meridiemHour != null) {
+            return locale.meridiemHour(hour, meridiem);
+        } else if (locale.isPM != null) {
+            // Fallback
+            isPm = locale.isPM(meridiem);
+            if (isPm && hour < 12) {
+                hour += 12;
+            }
+            if (!isPm && hour === 12) {
+                hour = 0;
+            }
+            return hour;
+        } else {
+            // thie is not supposed to happen
+            return hour;
+        }
+    }
+
     /************************************
         Constructors
     ************************************/
             }
         },
 
+
         _calendar : {
             sameDay : '[Today at] LT',
             nextDay : '[Tomorrow at] LT',
         // AM / PM
         case 'a' : // fall through to A
         case 'A' :
-            config._isPm = config._locale.isPM(input);
+            config._meridiem = input;
+            // config._isPm = config._locale.isPM(input);
             break;
         // HOUR
         case 'h' : // fall through to hh
         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 is 12 am, change hours to 0
-        if (config._isPm === false && config._a[HOUR] === 12) {
-            config._a[HOUR] = 0;
-        }
+        // handle meridiem
+        config._a[HOUR] = meridiemFixWrap(config._locale, config._a[HOUR],
+                config._meridiem);
         dateFromConfig(config);
         checkOverflow(config);
     }