5aaad5cc3fa02f39522921b97556088db7865c5830331a966406fec14317ae1cf8fe742f7ef157c50debf480c2604451f4aa0d11bfc6432d4c17af9c812cb8 4.3 KB

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