1 import { makeGetSet } from '../moment/get-set';
2 import { addFormatToken } from '../format/format';
3 import { addUnitAlias } from './aliases';
4 import { addRegexToken, match1to2, match2, match3to4, match5to6 } from '../parse/regex';
5 import { addParseToken } from '../parse/token';
6 import { HOUR, MINUTE, SECOND } from './constants';
7 import toInt from '../utils/to-int';
8 import zeroFill from '../utils/zero-fill';
9 import getParsingFlags from '../create/parsing-flags';
14 return this.hours() % 12 || 12;
18 return this.hours() || 24;
21 addFormatToken('H', ['HH', 2], 0, 'hour');
22 addFormatToken('h', ['hh', 2], 0, hFormat);
23 addFormatToken('k', ['kk', 2], 0, kFormat);
25 addFormatToken('hmm', 0, 0, function () {
26 return '' + hFormat.apply(this) + zeroFill(this.minutes(), 2);
29 addFormatToken('hmmss', 0, 0, function () {
30 return '' + hFormat.apply(this) + zeroFill(this.minutes(), 2) +
31 zeroFill(this.seconds(), 2);
34 addFormatToken('Hmm', 0, 0, function () {
35 return '' + this.hours() + zeroFill(this.minutes(), 2);
38 addFormatToken('Hmmss', 0, 0, function () {
39 return '' + this.hours() + zeroFill(this.minutes(), 2) +
40 zeroFill(this.seconds(), 2);
43 function meridiem (token, lowercase) {
44 addFormatToken(token, 0, 0, function () {
45 return this.localeData().meridiem(this.hours(), this.minutes(), lowercase);
54 addUnitAlias('hour', 'h');
58 function matchMeridiem (isStrict, locale) {
59 return locale._meridiemParse;
62 addRegexToken('a', matchMeridiem);
63 addRegexToken('A', matchMeridiem);
64 addRegexToken('H', match1to2);
65 addRegexToken('h', match1to2);
66 addRegexToken('HH', match1to2, match2);
67 addRegexToken('hh', match1to2, match2);
69 addRegexToken('hmm', match3to4);
70 addRegexToken('hmmss', match5to6);
71 addRegexToken('Hmm', match3to4);
72 addRegexToken('Hmmss', match5to6);
74 addParseToken(['H', 'HH'], HOUR);
75 addParseToken(['a', 'A'], function (input, array, config) {
76 config._isPm = config._locale.isPM(input);
77 config._meridiem = input;
79 addParseToken(['h', 'hh'], function (input, array, config) {
80 array[HOUR] = toInt(input);
81 getParsingFlags(config).bigHour = true;
83 addParseToken('hmm', function (input, array, config) {
84 var pos = input.length - 2;
85 array[HOUR] = toInt(input.substr(0, pos));
86 array[MINUTE] = toInt(input.substr(pos));
87 getParsingFlags(config).bigHour = true;
89 addParseToken('hmmss', function (input, array, config) {
90 var pos1 = input.length - 4;
91 var pos2 = input.length - 2;
92 array[HOUR] = toInt(input.substr(0, pos1));
93 array[MINUTE] = toInt(input.substr(pos1, 2));
94 array[SECOND] = toInt(input.substr(pos2));
95 getParsingFlags(config).bigHour = true;
97 addParseToken('Hmm', function (input, array, config) {
98 var pos = input.length - 2;
99 array[HOUR] = toInt(input.substr(0, pos));
100 array[MINUTE] = toInt(input.substr(pos));
102 addParseToken('Hmmss', function (input, array, config) {
103 var pos1 = input.length - 4;
104 var pos2 = input.length - 2;
105 array[HOUR] = toInt(input.substr(0, pos1));
106 array[MINUTE] = toInt(input.substr(pos1, 2));
107 array[SECOND] = toInt(input.substr(pos2));
112 export function localeIsPM (input) {
113 // IE8 Quirks Mode & IE7 Standards Mode do not allow accessing strings like arrays
114 // Using charAt should be more compatible.
115 return ((input + '').toLowerCase().charAt(0) === 'p');
118 export var defaultLocaleMeridiemParse = /[ap]\.?m?\.?/i;
119 export function localeMeridiem (hours, minutes, isLower) {
121 return isLower ? 'pm' : 'PM';
123 return isLower ? 'am' : 'AM';
130 // Setting the hour should keep the time, because the user explicitly
131 // specified which hour he wants. So trying to maintain the same hour (in
132 // a new timezone) makes sense. Adding/subtracting hours does not follow
134 export var getSetHour = makeGetSet('Hours', true);