hour.js (4419B)
1import { makeGetSet } from '../moment/get-set'; 2import { addFormatToken } from '../format/format'; 3import { 4 addRegexToken, 5 match1to2, 6 match2, 7 match3to4, 8 match5to6, 9 match1to2NoLeadingZero, 10 match1to2HasZero, 11} from '../parse/regex'; 12import { addParseToken } from '../parse/token'; 13import { HOUR, MINUTE, SECOND } from './constants'; 14import toInt from '../utils/to-int'; 15import zeroFill from '../utils/zero-fill'; 16import getParsingFlags from '../create/parsing-flags'; 17 18// FORMATTING 19 20function hFormat() { 21 return this.hours() % 12 || 12; 22} 23 24function kFormat() { 25 return this.hours() || 24; 26} 27 28addFormatToken('H', ['HH', 2], 0, 'hour'); 29addFormatToken('h', ['hh', 2], 0, hFormat); 30addFormatToken('k', ['kk', 2], 0, kFormat); 31 32addFormatToken('hmm', 0, 0, function () { 33 return '' + hFormat.apply(this) + zeroFill(this.minutes(), 2); 34}); 35 36addFormatToken('hmmss', 0, 0, function () { 37 return ( 38 '' + 39 hFormat.apply(this) + 40 zeroFill(this.minutes(), 2) + 41 zeroFill(this.seconds(), 2) 42 ); 43}); 44 45addFormatToken('Hmm', 0, 0, function () { 46 return '' + this.hours() + zeroFill(this.minutes(), 2); 47}); 48 49addFormatToken('Hmmss', 0, 0, function () { 50 return ( 51 '' + 52 this.hours() + 53 zeroFill(this.minutes(), 2) + 54 zeroFill(this.seconds(), 2) 55 ); 56}); 57 58function 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 68meridiem('a', true); 69meridiem('A', false); 70 71// PARSING 72 73function matchMeridiem(isStrict, locale) { 74 return locale._meridiemParse; 75} 76 77addRegexToken('a', matchMeridiem); 78addRegexToken('A', matchMeridiem); 79addRegexToken('H', match1to2, match1to2HasZero); 80addRegexToken('h', match1to2, match1to2NoLeadingZero); 81addRegexToken('k', match1to2, match1to2NoLeadingZero); 82addRegexToken('HH', match1to2, match2); 83addRegexToken('hh', match1to2, match2); 84addRegexToken('kk', match1to2, match2); 85 86addRegexToken('hmm', match3to4); 87addRegexToken('hmmss', match5to6); 88addRegexToken('Hmm', match3to4); 89addRegexToken('Hmmss', match5to6); 90 91addParseToken(['H', 'HH'], HOUR); 92addParseToken(['k', 'kk'], function (input, array, config) { 93 var kInput = toInt(input); 94 array[HOUR] = kInput === 24 ? 0 : kInput; 95}); 96addParseToken(['a', 'A'], function (input, array, config) { 97 config._isPm = config._locale.isPM(input); 98 config._meridiem = input; 99}); 100addParseToken(['h', 'hh'], function (input, array, config) { 101 array[HOUR] = toInt(input); 102 getParsingFlags(config).bigHour = true; 103}); 104addParseToken('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}); 110addParseToken('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}); 118addParseToken('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}); 123addParseToken('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 133export 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 139export 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 146export function localeMeridiem(hours, minutes, isLower) { 147 if (hours > 11) { 148 return isLower ? 'pm' : 'PM'; 149 } else { 150 return isLower ? 'am' : 'AM'; 151 } 152}