23bb5861641d69872fae197b54a283497d4c5dfdb34c056ae5abfd513fa0b224a0f36519eb5c8c4bd2af1d7442d2edf241916b3a0d76a45ce7a34c614324f8 3.6 KB

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