Skip to content

Commit

Permalink
Support meridiemHour in locales
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ichernev committed Dec 26, 2014
1 parent bd328d5 commit c70661a
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
************************************/
Expand Down Expand Up @@ -932,6 +957,7 @@
}
},


_calendar : {
sameDay : '[Today at] LT',
nextDay : '[Tomorrow at] LT',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit c70661a

Please sign in to comment.