| 1 |
import { makeGetSet } from '../moment/get-set'; |
| 2 |
import { addFormatToken } from '../format/format'; |
| 3 |
import { |
| 4 |
addRegexToken, |
| 5 |
match1to2, |
| 6 |
match2, |
| 7 |
match3to4, |
| 8 |
match5to6, |
| 9 |
match1to2NoLeadingZero, |
| 10 |
match1to2HasZero, |
| 11 |
} from '../parse/regex'; |
| 12 |
import { addParseToken } from '../parse/token'; |
| 13 |
import { HOUR, MINUTE, SECOND } from './constants'; |
| 14 |
import toInt from '../utils/to-int'; |
| 15 |
import zeroFill from '../utils/zero-fill'; |
| 16 |
import getParsingFlags from '../create/parsing-flags'; |
| 17 |
|
| 18 |
// FORMATTING |
| 19 |
|
| 20 |
function hFormat() { |
| 21 |
return this.hours() % 12 || 12; |
| 22 |
} |
| 23 |
|
| 24 |
function kFormat() { |
| 25 |
return this.hours() || 24; |
| 26 |
} |
| 27 |
|
| 28 |
addFormatToken('H', ['HH', 2], 0, 'hour'); |
| 29 |
addFormatToken('h', ['hh', 2], 0, hFormat); |
| 30 |
addFormatToken('k', ['kk', 2], 0, kFormat); |
| 31 |
|
| 32 |
addFormatToken('hmm', 0, 0, function () { |
| 33 |
return '' + hFormat.apply(this) + zeroFill(this.minutes(), 2); |
| 34 |
}); |
| 35 |
|
| 36 |
addFormatToken('hmmss', 0, 0, function () { |
| 37 |
return ( |
| 38 |
'' + |
| 39 |
hFormat.apply(this) + |
| 40 |
zeroFill(this.minutes(), 2) + |
| 41 |
zeroFill(this.seconds(), 2) |
| 42 |
); |
| 43 |
}); |
| 44 |
|
| 45 |
addFormatToken('Hmm', 0, 0, function () { |
| 46 |
return '' + this.hours() + zeroFill(this.minutes(), 2); |
| 47 |
}); |
| 48 |
|
| 49 |
addFormatToken('Hmmss', 0, 0, function () { |
| 50 |
return ( |
| 51 |
'' + |
| 52 |
this.hours() + |
| 53 |
zeroFill(this.minutes(), 2) + |
| 54 |
zeroFill(this.seconds(), 2) |
| 55 |
); |
| 56 |
}); |
| 57 |
|
| 58 |
function meridiem(token, lowercase) { |
| 59 |
addFormatToken(token, 0, 0, function () { |
| 60 |
return this.localeData().meridiem( |
| 61 |
this.hours(), |
| 62 |
this.minutes(), |
| 63 |
lowercase |
| 64 |
); |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
meridiem('a', true); |
| 69 |
meridiem('A', false); |
| 70 |
|
| 71 |
// PARSING |
| 72 |
|
| 73 |
function matchMeridiem(isStrict, locale) { |
| 74 |
return locale._meridiemParse; |
| 75 |
} |
| 76 |
|
| 77 |
addRegexToken('a', matchMeridiem); |
| 78 |
addRegexToken('A', matchMeridiem); |
| 79 |
addRegexToken('H', match1to2, match1to2HasZero); |
| 80 |
addRegexToken('h', match1to2, match1to2NoLeadingZero); |
| 81 |
addRegexToken('k', match1to2, match1to2NoLeadingZero); |
| 82 |
addRegexToken('HH', match1to2, match2); |
| 83 |
addRegexToken('hh', match1to2, match2); |
| 84 |
addRegexToken('kk', match1to2, match2); |
| 85 |
|
| 86 |
addRegexToken('hmm', match3to4); |
| 87 |
addRegexToken('hmmss', match5to6); |
| 88 |
addRegexToken('Hmm', match3to4); |
| 89 |
addRegexToken('Hmmss', match5to6); |
| 90 |
|
| 91 |
addParseToken(['H', 'HH'], HOUR); |
| 92 |
addParseToken(['k', 'kk'], function (input, array, config) { |
| 93 |
var kInput = toInt(input); |
| 94 |
array[HOUR] = kInput === 24 ? 0 : kInput; |
| 95 |
}); |
| 96 |
addParseToken(['a', 'A'], function (input, array, config) { |
| 97 |
config._isPm = config._locale.isPM(input); |
| 98 |
config._meridiem = input; |
| 99 |
}); |
| 100 |
addParseToken(['h', 'hh'], function (input, array, config) { |
| 101 |
array[HOUR] = toInt(input); |
| 102 |
getParsingFlags(config).bigHour = true; |
| 103 |
}); |
| 104 |
addParseToken('hmm', function (input, array, config) { |
| 105 |
var pos = input.length - 2; |
| 106 |
array[HOUR] = toInt(input.substr(0, pos)); |
| 107 |
array[MINUTE] = toInt(input.substr(pos)); |
| 108 |
getParsingFlags(config).bigHour = true; |
| 109 |
}); |
| 110 |
addParseToken('hmmss', function (input, array, config) { |
| 111 |
var pos1 = input.length - 4, |
| 112 |
pos2 = input.length - 2; |
| 113 |
array[HOUR] = toInt(input.substr(0, pos1)); |
| 114 |
array[MINUTE] = toInt(input.substr(pos1, 2)); |
| 115 |
array[SECOND] = toInt(input.substr(pos2)); |
| 116 |
getParsingFlags(config).bigHour = true; |
| 117 |
}); |
| 118 |
addParseToken('Hmm', function (input, array, config) { |
| 119 |
var pos = input.length - 2; |
| 120 |
array[HOUR] = toInt(input.substr(0, pos)); |
| 121 |
array[MINUTE] = toInt(input.substr(pos)); |
| 122 |
}); |
| 123 |
addParseToken('Hmmss', function (input, array, config) { |
| 124 |
var pos1 = input.length - 4, |
| 125 |
pos2 = input.length - 2; |
| 126 |
array[HOUR] = toInt(input.substr(0, pos1)); |
| 127 |
array[MINUTE] = toInt(input.substr(pos1, 2)); |
| 128 |
array[SECOND] = toInt(input.substr(pos2)); |
| 129 |
}); |
| 130 |
|
| 131 |
// LOCALES |
| 132 |
|
| 133 |
export function localeIsPM(input) { |
| 134 |
// IE8 Quirks Mode & IE7 Standards Mode do not allow accessing strings like arrays |
| 135 |
// Using charAt should be more compatible. |
| 136 |
return (input + '').toLowerCase().charAt(0) === 'p'; |
| 137 |
} |
| 138 |
|
| 139 |
export var defaultLocaleMeridiemParse = /[ap]\.?m?\.?/i, |
| 140 |
// Setting the hour should keep the time, because the user explicitly |
| 141 |
// specified which hour they want. So trying to maintain the same hour (in |
| 142 |
// a new timezone) makes sense. Adding/subtracting hours does not follow |
| 143 |
// this rule. |
| 144 |
getSetHour = makeGetSet('Hours', true); |
| 145 |
|
| 146 |
export function localeMeridiem(hours, minutes, isLower) { |
| 147 |
if (hours > 11) { |
| 148 |
return isLower ? 'pm' : 'PM'; |
| 149 |
} else { |
| 150 |
return isLower ? 'am' : 'AM'; |
| 151 |
} |
| 152 |
} |