3263a331f8cfafe2b12a9552fb18532f4d8b9d2dafc3f11e6a480e9376af501763698bdb7ca28257239930a706ad6c8ab580c52c259d7803827071a849557d 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { configFromISO, configFromRFC2822 } from './from-string';
  2. import { configFromArray } from './from-array';
  3. import { getParseRegexForToken } from '../parse/regex';
  4. import { addTimeToArrayFromToken } from '../parse/token';
  5. import {
  6. expandFormat,
  7. formatTokenFunctions,
  8. formattingTokens,
  9. } from '../format/format';
  10. import checkOverflow from './check-overflow';
  11. import { YEAR, HOUR } from '../units/constants';
  12. import { hooks } from '../utils/hooks';
  13. import getParsingFlags from './parsing-flags';
  14. // constant that refers to the ISO standard
  15. hooks.ISO_8601 = function () {};
  16. // constant that refers to the RFC 2822 form
  17. hooks.RFC_2822 = function () {};
  18. // date from string and format string
  19. export function configFromStringAndFormat(config) {
  20. // TODO: Move this to another part of the creation flow to prevent circular deps
  21. if (config._f === hooks.ISO_8601) {
  22. configFromISO(config);
  23. return;
  24. }
  25. if (config._f === hooks.RFC_2822) {
  26. configFromRFC2822(config);
  27. return;
  28. }
  29. config._a = [];
  30. getParsingFlags(config).empty = true;
  31. // This array is used to make a Date, either with `new Date` or `Date.UTC`
  32. var string = '' + config._i,
  33. i,
  34. parsedInput,
  35. tokens,
  36. token,
  37. skipped,
  38. stringLength = string.length,
  39. totalParsedInputLength = 0,
  40. era,
  41. tokenLen;
  42. tokens =
  43. expandFormat(config._f, config._locale).match(formattingTokens) || [];
  44. tokenLen = tokens.length;
  45. for (i = 0; i < tokenLen; i++) {
  46. token = tokens[i];
  47. parsedInput = (string.match(getParseRegexForToken(token, config)) ||
  48. [])[0];
  49. if (parsedInput) {
  50. skipped = string.substr(0, string.indexOf(parsedInput));
  51. if (skipped.length > 0) {
  52. getParsingFlags(config).unusedInput.push(skipped);
  53. }
  54. string = string.slice(
  55. string.indexOf(parsedInput) + parsedInput.length
  56. );
  57. totalParsedInputLength += parsedInput.length;
  58. }
  59. // don't parse if it's not a known token
  60. if (formatTokenFunctions[token]) {
  61. if (parsedInput) {
  62. getParsingFlags(config).empty = false;
  63. } else {
  64. getParsingFlags(config).unusedTokens.push(token);
  65. }
  66. addTimeToArrayFromToken(token, parsedInput, config);
  67. } else if (config._strict && !parsedInput) {
  68. getParsingFlags(config).unusedTokens.push(token);
  69. }
  70. }
  71. // add remaining unparsed input length to the string
  72. getParsingFlags(config).charsLeftOver =
  73. stringLength - totalParsedInputLength;
  74. if (string.length > 0) {
  75. getParsingFlags(config).unusedInput.push(string);
  76. }
  77. // clear _12h flag if hour is <= 12
  78. if (
  79. config._a[HOUR] <= 12 &&
  80. getParsingFlags(config).bigHour === true &&
  81. config._a[HOUR] > 0
  82. ) {
  83. getParsingFlags(config).bigHour = undefined;
  84. }
  85. getParsingFlags(config).parsedDateParts = config._a.slice(0);
  86. getParsingFlags(config).meridiem = config._meridiem;
  87. // handle meridiem
  88. config._a[HOUR] = meridiemFixWrap(
  89. config._locale,
  90. config._a[HOUR],
  91. config._meridiem
  92. );
  93. // handle era
  94. era = getParsingFlags(config).era;
  95. if (era !== null) {
  96. config._a[YEAR] = config._locale.erasConvertYear(era, config._a[YEAR]);
  97. }
  98. configFromArray(config);
  99. checkOverflow(config);
  100. }
  101. function meridiemFixWrap(locale, hour, meridiem) {
  102. var isPm;
  103. if (meridiem == null) {
  104. // nothing to do
  105. return hour;
  106. }
  107. if (locale.meridiemHour != null) {
  108. return locale.meridiemHour(hour, meridiem);
  109. } else if (locale.isPM != null) {
  110. // Fallback
  111. isPm = locale.isPM(meridiem);
  112. if (isPm && hour < 12) {
  113. hour += 12;
  114. }
  115. if (!isPm && hour === 12) {
  116. hour = 0;
  117. }
  118. return hour;
  119. } else {
  120. // this is not supposed to happen
  121. return hour;
  122. }
  123. }