5b5a70531ec47fe221d604cdc546495ff2ac8db27cca16ee84b7ccee52f3f3b744e9b856a99f8b1e62ca1984bf12212798071bbe97ddb1aa3bce9188654b75 4.0 KB

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