2279dbb07c79c75085bb243958abffa01dc706424c3891d9a8e795502d9cfcb800d85b10d82baedb853180cc15030756543624829dbd677dae39a08b0bb630 4.0 KB

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