1547c319bf8204821eff4e454857d724a2c66969629ce3faddcae04d1cb84c04bbc6e4d873a3d8548430895cc2957d2a1db9f15c617f74b6c1e25cb26c20c8 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { configFromStringAndFormat } from './from-string-and-format';
  2. import { hooks } from '../utils/hooks';
  3. import { deprecate } from '../utils/deprecate';
  4. import getParsingFlags from './parsing-flags';
  5. // iso 8601 regex
  6. // 0000-00-00 0000-W00 or 0000-W00-0 + T + 00 or 00:00 or 00:00:00 or 00:00:00.000 + +00:00 or +0000 or +00)
  7. var extendedIsoRegex = /^\s*((?:[+-]\d{6}|\d{4})-(?:\d\d-\d\d|W\d\d-\d|W\d\d|\d\d\d|\d\d))(?:(T| )(\d\d(?::\d\d(?::\d\d(?:[.,]\d+)?)?)?)([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?/;
  8. var basicIsoRegex = /^\s*((?:[+-]\d{6}|\d{4})(?:\d\d\d\d|W\d\d\d|W\d\d|\d\d\d|\d\d))(?:(T| )(\d\d(?:\d\d(?:\d\d(?:[.,]\d+)?)?)?)([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?/;
  9. var tzRegex = /Z|[+-]\d\d(?::?\d\d)?/;
  10. var isoDates = [
  11. ['YYYYYY-MM-DD', /[+-]\d{6}-\d\d-\d\d/],
  12. ['YYYY-MM-DD', /\d{4}-\d\d-\d\d/],
  13. ['GGGG-[W]WW-E', /\d{4}-W\d\d-\d/],
  14. ['GGGG-[W]WW', /\d{4}-W\d\d/, false],
  15. ['YYYY-DDD', /\d{4}-\d{3}/],
  16. ['YYYY-MM', /\d{4}-\d\d/, false],
  17. ['YYYYYYMMDD', /[+-]\d{10}/],
  18. ['YYYYMMDD', /\d{8}/],
  19. // YYYYMM is NOT allowed by the standard
  20. ['GGGG[W]WWE', /\d{4}W\d{3}/],
  21. ['GGGG[W]WW', /\d{4}W\d{2}/, false],
  22. ['YYYYDDD', /\d{7}/]
  23. ];
  24. // iso time formats and regexes
  25. var isoTimes = [
  26. ['HH:mm:ss.SSSS', /\d\d:\d\d:\d\d\.\d+/],
  27. ['HH:mm:ss,SSSS', /\d\d:\d\d:\d\d,\d+/],
  28. ['HH:mm:ss', /\d\d:\d\d:\d\d/],
  29. ['HH:mm', /\d\d:\d\d/],
  30. ['HHmmss.SSSS', /\d\d\d\d\d\d\.\d+/],
  31. ['HHmmss,SSSS', /\d\d\d\d\d\d,\d+/],
  32. ['HHmmss', /\d\d\d\d\d\d/],
  33. ['HHmm', /\d\d\d\d/],
  34. ['HH', /\d\d/]
  35. ];
  36. var aspNetJsonRegex = /^\/?Date\((\-?\d+)/i;
  37. // date from iso format
  38. export function configFromISO(config) {
  39. var i, l,
  40. string = config._i,
  41. match = extendedIsoRegex.exec(string) || basicIsoRegex.exec(string),
  42. allowTime, dateFormat, timeFormat, tzFormat;
  43. if (match) {
  44. getParsingFlags(config).iso = true;
  45. for (i = 0, l = isoDates.length; i < l; i++) {
  46. if (isoDates[i][1].exec(match[1])) {
  47. dateFormat = isoDates[i][0];
  48. allowTime = isoDates[i][2] !== false;
  49. break;
  50. }
  51. }
  52. if (dateFormat == null) {
  53. config._isValid = false;
  54. return;
  55. }
  56. if (match[3]) {
  57. for (i = 0, l = isoTimes.length; i < l; i++) {
  58. if (isoTimes[i][1].exec(match[3])) {
  59. // match[2] should be 'T' or space
  60. timeFormat = (match[2] || ' ') + isoTimes[i][0];
  61. break;
  62. }
  63. }
  64. if (timeFormat == null) {
  65. config._isValid = false;
  66. return;
  67. }
  68. }
  69. if (!allowTime && timeFormat != null) {
  70. config._isValid = false;
  71. return;
  72. }
  73. if (match[4]) {
  74. if (tzRegex.exec(match[4])) {
  75. tzFormat = 'Z';
  76. } else {
  77. config._isValid = false;
  78. return;
  79. }
  80. }
  81. config._f = dateFormat + (timeFormat || '') + (tzFormat || '');
  82. configFromStringAndFormat(config);
  83. } else {
  84. config._isValid = false;
  85. }
  86. }
  87. // date from iso format or fallback
  88. export function configFromString(config) {
  89. var matched = aspNetJsonRegex.exec(config._i);
  90. if (matched !== null) {
  91. config._d = new Date(+matched[1]);
  92. return;
  93. }
  94. configFromISO(config);
  95. if (config._isValid === false) {
  96. delete config._isValid;
  97. hooks.createFromInputFallback(config);
  98. }
  99. }
  100. hooks.createFromInputFallback = deprecate(
  101. 'value provided is not in a recognized ISO format. moment construction falls back to js Date(), ' +
  102. 'which is not reliable across all browsers and versions. Non ISO date formats are ' +
  103. 'discouraged and will be removed in an upcoming major release. Please refer to ' +
  104. 'http://momentjs.com/guides/#/warnings/js-date/ for more info.',
  105. function (config) {
  106. config._d = new Date(config._i + (config._useUTC ? ' UTC' : ''));
  107. }
  108. );