From: Iskren Chernev Date: Fri, 26 Dec 2014 10:28:54 +0000 (+0200) Subject: Support meridiemHour in locales X-Git-Tag: 2.9.0~13^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c70661abc8e98a9ce872abfe1185d5bff101d6d3;p=thirdparty%2Fmoment.git Support meridiemHour in locales 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 --- diff --git a/moment.js b/moment.js index e93c2e54b..227bf0aad 100644 --- a/moment.js +++ b/moment.js @@ -365,6 +365,31 @@ 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 ************************************/ @@ -932,6 +957,7 @@ } }, + _calendar : { sameDay : '[Today at] LT', nextDay : '[Tomorrow at] LT', @@ -1233,7 +1259,8 @@ // 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 @@ -1510,14 +1537,9 @@ 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); }