parse.cjs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. "use strict";
  2. Object.defineProperty(exports, "longFormatters", {
  3. enumerable: true,
  4. get: function () {
  5. return _index2.longFormatters;
  6. },
  7. });
  8. exports.parse = parse;
  9. Object.defineProperty(exports, "parsers", {
  10. enumerable: true,
  11. get: function () {
  12. return _index7.parsers;
  13. },
  14. });
  15. var _index = require("./_lib/defaultLocale.cjs");
  16. var _index2 = require("./_lib/format/longFormatters.cjs");
  17. var _index3 = require("./_lib/protectedTokens.cjs");
  18. var _index4 = require("./constructFrom.cjs");
  19. var _index5 = require("./getDefaultOptions.cjs");
  20. var _index6 = require("./toDate.cjs");
  21. var _Setter = require("./parse/_lib/Setter.cjs");
  22. var _index7 = require("./parse/_lib/parsers.cjs");
  23. // Rexports of internal for libraries to use.
  24. // See: https://github.com/date-fns/date-fns/issues/3638#issuecomment-1877082874
  25. /**
  26. * The {@link parse} function options.
  27. */
  28. // This RegExp consists of three parts separated by `|`:
  29. // - [yYQqMLwIdDecihHKkms]o matches any available ordinal number token
  30. // (one of the certain letters followed by `o`)
  31. // - (\w)\1* matches any sequences of the same letter
  32. // - '' matches two quote characters in a row
  33. // - '(''|[^'])+('|$) matches anything surrounded by two quote characters ('),
  34. // except a single quote symbol, which ends the sequence.
  35. // Two quote characters do not end the sequence.
  36. // If there is no matching single quote
  37. // then the sequence will continue until the end of the string.
  38. // - . matches any single character unmatched by previous parts of the RegExps
  39. const formattingTokensRegExp =
  40. /[yYQqMLwIdDecihHKkms]o|(\w)\1*|''|'(''|[^'])+('|$)|./g;
  41. // This RegExp catches symbols escaped by quotes, and also
  42. // sequences of symbols P, p, and the combinations like `PPPPPPPppppp`
  43. const longFormattingTokensRegExp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g;
  44. const escapedStringRegExp = /^'([^]*?)'?$/;
  45. const doubleQuoteRegExp = /''/g;
  46. const notWhitespaceRegExp = /\S/;
  47. const unescapedLatinCharacterRegExp = /[a-zA-Z]/;
  48. /**
  49. * @name parse
  50. * @category Common Helpers
  51. * @summary Parse the date.
  52. *
  53. * @description
  54. * Return the date parsed from string using the given format string.
  55. *
  56. * > ⚠️ Please note that the `format` tokens differ from Moment.js and other libraries.
  57. * > See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  58. *
  59. * The characters in the format string wrapped between two single quotes characters (') are escaped.
  60. * Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.
  61. *
  62. * Format of the format string is based on Unicode Technical Standard #35:
  63. * https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
  64. * with a few additions (see note 5 below the table).
  65. *
  66. * Not all tokens are compatible. Combinations that don't make sense or could lead to bugs are prohibited
  67. * and will throw `RangeError`. For example usage of 24-hour format token with AM/PM token will throw an exception:
  68. *
  69. * ```javascript
  70. * parse('23 AM', 'HH a', new Date())
  71. * //=> RangeError: The format string mustn't contain `HH` and `a` at the same time
  72. * ```
  73. *
  74. * See the compatibility table: https://docs.google.com/spreadsheets/d/e/2PACX-1vQOPU3xUhplll6dyoMmVUXHKl_8CRDs6_ueLmex3SoqwhuolkuN3O05l4rqx5h1dKX8eb46Ul-CCSrq/pubhtml?gid=0&single=true
  75. *
  76. * Accepted format string patterns:
  77. * | Unit |Prior| Pattern | Result examples | Notes |
  78. * |---------------------------------|-----|---------|-----------------------------------|-------|
  79. * | Era | 140 | G..GGG | AD, BC | |
  80. * | | | GGGG | Anno Domini, Before Christ | 2 |
  81. * | | | GGGGG | A, B | |
  82. * | Calendar year | 130 | y | 44, 1, 1900, 2017, 9999 | 4 |
  83. * | | | yo | 44th, 1st, 1900th, 9999999th | 4,5 |
  84. * | | | yy | 44, 01, 00, 17 | 4 |
  85. * | | | yyy | 044, 001, 123, 999 | 4 |
  86. * | | | yyyy | 0044, 0001, 1900, 2017 | 4 |
  87. * | | | yyyyy | ... | 2,4 |
  88. * | Local week-numbering year | 130 | Y | 44, 1, 1900, 2017, 9000 | 4 |
  89. * | | | Yo | 44th, 1st, 1900th, 9999999th | 4,5 |
  90. * | | | YY | 44, 01, 00, 17 | 4,6 |
  91. * | | | YYY | 044, 001, 123, 999 | 4 |
  92. * | | | YYYY | 0044, 0001, 1900, 2017 | 4,6 |
  93. * | | | YYYYY | ... | 2,4 |
  94. * | ISO week-numbering year | 130 | R | -43, 1, 1900, 2017, 9999, -9999 | 4,5 |
  95. * | | | RR | -43, 01, 00, 17 | 4,5 |
  96. * | | | RRR | -043, 001, 123, 999, -999 | 4,5 |
  97. * | | | RRRR | -0043, 0001, 2017, 9999, -9999 | 4,5 |
  98. * | | | RRRRR | ... | 2,4,5 |
  99. * | Extended year | 130 | u | -43, 1, 1900, 2017, 9999, -999 | 4 |
  100. * | | | uu | -43, 01, 99, -99 | 4 |
  101. * | | | uuu | -043, 001, 123, 999, -999 | 4 |
  102. * | | | uuuu | -0043, 0001, 2017, 9999, -9999 | 4 |
  103. * | | | uuuuu | ... | 2,4 |
  104. * | Quarter (formatting) | 120 | Q | 1, 2, 3, 4 | |
  105. * | | | Qo | 1st, 2nd, 3rd, 4th | 5 |
  106. * | | | QQ | 01, 02, 03, 04 | |
  107. * | | | QQQ | Q1, Q2, Q3, Q4 | |
  108. * | | | QQQQ | 1st quarter, 2nd quarter, ... | 2 |
  109. * | | | QQQQQ | 1, 2, 3, 4 | 4 |
  110. * | Quarter (stand-alone) | 120 | q | 1, 2, 3, 4 | |
  111. * | | | qo | 1st, 2nd, 3rd, 4th | 5 |
  112. * | | | qq | 01, 02, 03, 04 | |
  113. * | | | qqq | Q1, Q2, Q3, Q4 | |
  114. * | | | qqqq | 1st quarter, 2nd quarter, ... | 2 |
  115. * | | | qqqqq | 1, 2, 3, 4 | 3 |
  116. * | Month (formatting) | 110 | M | 1, 2, ..., 12 | |
  117. * | | | Mo | 1st, 2nd, ..., 12th | 5 |
  118. * | | | MM | 01, 02, ..., 12 | |
  119. * | | | MMM | Jan, Feb, ..., Dec | |
  120. * | | | MMMM | January, February, ..., December | 2 |
  121. * | | | MMMMM | J, F, ..., D | |
  122. * | Month (stand-alone) | 110 | L | 1, 2, ..., 12 | |
  123. * | | | Lo | 1st, 2nd, ..., 12th | 5 |
  124. * | | | LL | 01, 02, ..., 12 | |
  125. * | | | LLL | Jan, Feb, ..., Dec | |
  126. * | | | LLLL | January, February, ..., December | 2 |
  127. * | | | LLLLL | J, F, ..., D | |
  128. * | Local week of year | 100 | w | 1, 2, ..., 53 | |
  129. * | | | wo | 1st, 2nd, ..., 53th | 5 |
  130. * | | | ww | 01, 02, ..., 53 | |
  131. * | ISO week of year | 100 | I | 1, 2, ..., 53 | 5 |
  132. * | | | Io | 1st, 2nd, ..., 53th | 5 |
  133. * | | | II | 01, 02, ..., 53 | 5 |
  134. * | Day of month | 90 | d | 1, 2, ..., 31 | |
  135. * | | | do | 1st, 2nd, ..., 31st | 5 |
  136. * | | | dd | 01, 02, ..., 31 | |
  137. * | Day of year | 90 | D | 1, 2, ..., 365, 366 | 7 |
  138. * | | | Do | 1st, 2nd, ..., 365th, 366th | 5 |
  139. * | | | DD | 01, 02, ..., 365, 366 | 7 |
  140. * | | | DDD | 001, 002, ..., 365, 366 | |
  141. * | | | DDDD | ... | 2 |
  142. * | Day of week (formatting) | 90 | E..EEE | Mon, Tue, Wed, ..., Sun | |
  143. * | | | EEEE | Monday, Tuesday, ..., Sunday | 2 |
  144. * | | | EEEEE | M, T, W, T, F, S, S | |
  145. * | | | EEEEEE | Mo, Tu, We, Th, Fr, Sa, Su | |
  146. * | ISO day of week (formatting) | 90 | i | 1, 2, 3, ..., 7 | 5 |
  147. * | | | io | 1st, 2nd, ..., 7th | 5 |
  148. * | | | ii | 01, 02, ..., 07 | 5 |
  149. * | | | iii | Mon, Tue, Wed, ..., Sun | 5 |
  150. * | | | iiii | Monday, Tuesday, ..., Sunday | 2,5 |
  151. * | | | iiiii | M, T, W, T, F, S, S | 5 |
  152. * | | | iiiiii | Mo, Tu, We, Th, Fr, Sa, Su | 5 |
  153. * | Local day of week (formatting) | 90 | e | 2, 3, 4, ..., 1 | |
  154. * | | | eo | 2nd, 3rd, ..., 1st | 5 |
  155. * | | | ee | 02, 03, ..., 01 | |
  156. * | | | eee | Mon, Tue, Wed, ..., Sun | |
  157. * | | | eeee | Monday, Tuesday, ..., Sunday | 2 |
  158. * | | | eeeee | M, T, W, T, F, S, S | |
  159. * | | | eeeeee | Mo, Tu, We, Th, Fr, Sa, Su | |
  160. * | Local day of week (stand-alone) | 90 | c | 2, 3, 4, ..., 1 | |
  161. * | | | co | 2nd, 3rd, ..., 1st | 5 |
  162. * | | | cc | 02, 03, ..., 01 | |
  163. * | | | ccc | Mon, Tue, Wed, ..., Sun | |
  164. * | | | cccc | Monday, Tuesday, ..., Sunday | 2 |
  165. * | | | ccccc | M, T, W, T, F, S, S | |
  166. * | | | cccccc | Mo, Tu, We, Th, Fr, Sa, Su | |
  167. * | AM, PM | 80 | a..aaa | AM, PM | |
  168. * | | | aaaa | a.m., p.m. | 2 |
  169. * | | | aaaaa | a, p | |
  170. * | AM, PM, noon, midnight | 80 | b..bbb | AM, PM, noon, midnight | |
  171. * | | | bbbb | a.m., p.m., noon, midnight | 2 |
  172. * | | | bbbbb | a, p, n, mi | |
  173. * | Flexible day period | 80 | B..BBB | at night, in the morning, ... | |
  174. * | | | BBBB | at night, in the morning, ... | 2 |
  175. * | | | BBBBB | at night, in the morning, ... | |
  176. * | Hour [1-12] | 70 | h | 1, 2, ..., 11, 12 | |
  177. * | | | ho | 1st, 2nd, ..., 11th, 12th | 5 |
  178. * | | | hh | 01, 02, ..., 11, 12 | |
  179. * | Hour [0-23] | 70 | H | 0, 1, 2, ..., 23 | |
  180. * | | | Ho | 0th, 1st, 2nd, ..., 23rd | 5 |
  181. * | | | HH | 00, 01, 02, ..., 23 | |
  182. * | Hour [0-11] | 70 | K | 1, 2, ..., 11, 0 | |
  183. * | | | Ko | 1st, 2nd, ..., 11th, 0th | 5 |
  184. * | | | KK | 01, 02, ..., 11, 00 | |
  185. * | Hour [1-24] | 70 | k | 24, 1, 2, ..., 23 | |
  186. * | | | ko | 24th, 1st, 2nd, ..., 23rd | 5 |
  187. * | | | kk | 24, 01, 02, ..., 23 | |
  188. * | Minute | 60 | m | 0, 1, ..., 59 | |
  189. * | | | mo | 0th, 1st, ..., 59th | 5 |
  190. * | | | mm | 00, 01, ..., 59 | |
  191. * | Second | 50 | s | 0, 1, ..., 59 | |
  192. * | | | so | 0th, 1st, ..., 59th | 5 |
  193. * | | | ss | 00, 01, ..., 59 | |
  194. * | Seconds timestamp | 40 | t | 512969520 | |
  195. * | | | tt | ... | 2 |
  196. * | Fraction of second | 30 | S | 0, 1, ..., 9 | |
  197. * | | | SS | 00, 01, ..., 99 | |
  198. * | | | SSS | 000, 001, ..., 999 | |
  199. * | | | SSSS | ... | 2 |
  200. * | Milliseconds timestamp | 20 | T | 512969520900 | |
  201. * | | | TT | ... | 2 |
  202. * | Timezone (ISO-8601 w/ Z) | 10 | X | -08, +0530, Z | |
  203. * | | | XX | -0800, +0530, Z | |
  204. * | | | XXX | -08:00, +05:30, Z | |
  205. * | | | XXXX | -0800, +0530, Z, +123456 | 2 |
  206. * | | | XXXXX | -08:00, +05:30, Z, +12:34:56 | |
  207. * | Timezone (ISO-8601 w/o Z) | 10 | x | -08, +0530, +00 | |
  208. * | | | xx | -0800, +0530, +0000 | |
  209. * | | | xxx | -08:00, +05:30, +00:00 | 2 |
  210. * | | | xxxx | -0800, +0530, +0000, +123456 | |
  211. * | | | xxxxx | -08:00, +05:30, +00:00, +12:34:56 | |
  212. * | Long localized date | NA | P | 05/29/1453 | 5,8 |
  213. * | | | PP | May 29, 1453 | |
  214. * | | | PPP | May 29th, 1453 | |
  215. * | | | PPPP | Sunday, May 29th, 1453 | 2,5,8 |
  216. * | Long localized time | NA | p | 12:00 AM | 5,8 |
  217. * | | | pp | 12:00:00 AM | |
  218. * | Combination of date and time | NA | Pp | 05/29/1453, 12:00 AM | |
  219. * | | | PPpp | May 29, 1453, 12:00:00 AM | |
  220. * | | | PPPpp | May 29th, 1453 at ... | |
  221. * | | | PPPPpp | Sunday, May 29th, 1453 at ... | 2,5,8 |
  222. * Notes:
  223. * 1. "Formatting" units (e.g. formatting quarter) in the default en-US locale
  224. * are the same as "stand-alone" units, but are different in some languages.
  225. * "Formatting" units are declined according to the rules of the language
  226. * in the context of a date. "Stand-alone" units are always nominative singular.
  227. * In `format` function, they will produce different result:
  228. *
  229. * `format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'`
  230. *
  231. * `format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'`
  232. *
  233. * `parse` will try to match both formatting and stand-alone units interchangeably.
  234. *
  235. * 2. Any sequence of the identical letters is a pattern, unless it is escaped by
  236. * the single quote characters (see below).
  237. * If the sequence is longer than listed in table:
  238. * - for numerical units (`yyyyyyyy`) `parse` will try to match a number
  239. * as wide as the sequence
  240. * - for text units (`MMMMMMMM`) `parse` will try to match the widest variation of the unit.
  241. * These variations are marked with "2" in the last column of the table.
  242. *
  243. * 3. `QQQQQ` and `qqqqq` could be not strictly numerical in some locales.
  244. * These tokens represent the shortest form of the quarter.
  245. *
  246. * 4. The main difference between `y` and `u` patterns are B.C. years:
  247. *
  248. * | Year | `y` | `u` |
  249. * |------|-----|-----|
  250. * | AC 1 | 1 | 1 |
  251. * | BC 1 | 1 | 0 |
  252. * | BC 2 | 2 | -1 |
  253. *
  254. * Also `yy` will try to guess the century of two digit year by proximity with `referenceDate`:
  255. *
  256. * `parse('50', 'yy', new Date(2018, 0, 1)) //=> Sat Jan 01 2050 00:00:00`
  257. *
  258. * `parse('75', 'yy', new Date(2018, 0, 1)) //=> Wed Jan 01 1975 00:00:00`
  259. *
  260. * while `uu` will just assign the year as is:
  261. *
  262. * `parse('50', 'uu', new Date(2018, 0, 1)) //=> Sat Jan 01 0050 00:00:00`
  263. *
  264. * `parse('75', 'uu', new Date(2018, 0, 1)) //=> Tue Jan 01 0075 00:00:00`
  265. *
  266. * The same difference is true for local and ISO week-numbering years (`Y` and `R`),
  267. * except local week-numbering years are dependent on `options.weekStartsOn`
  268. * and `options.firstWeekContainsDate` (compare [setISOWeekYear](https://date-fns.org/docs/setISOWeekYear)
  269. * and [setWeekYear](https://date-fns.org/docs/setWeekYear)).
  270. *
  271. * 5. These patterns are not in the Unicode Technical Standard #35:
  272. * - `i`: ISO day of week
  273. * - `I`: ISO week of year
  274. * - `R`: ISO week-numbering year
  275. * - `o`: ordinal number modifier
  276. * - `P`: long localized date
  277. * - `p`: long localized time
  278. *
  279. * 6. `YY` and `YYYY` tokens represent week-numbering years but they are often confused with years.
  280. * You should enable `options.useAdditionalWeekYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  281. *
  282. * 7. `D` and `DD` tokens represent days of the year but they are often confused with days of the month.
  283. * You should enable `options.useAdditionalDayOfYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  284. *
  285. * 8. `P+` tokens do not have a defined priority since they are merely aliases to other tokens based
  286. * on the given locale.
  287. *
  288. * using `en-US` locale: `P` => `MM/dd/yyyy`
  289. * using `en-US` locale: `p` => `hh:mm a`
  290. * using `pt-BR` locale: `P` => `dd/MM/yyyy`
  291. * using `pt-BR` locale: `p` => `HH:mm`
  292. *
  293. * Values will be assigned to the date in the descending order of its unit's priority.
  294. * Units of an equal priority overwrite each other in the order of appearance.
  295. *
  296. * If no values of higher priority are parsed (e.g. when parsing string 'January 1st' without a year),
  297. * the values will be taken from 3rd argument `referenceDate` which works as a context of parsing.
  298. *
  299. * `referenceDate` must be passed for correct work of the function.
  300. * If you're not sure which `referenceDate` to supply, create a new instance of Date:
  301. * `parse('02/11/2014', 'MM/dd/yyyy', new Date())`
  302. * In this case parsing will be done in the context of the current date.
  303. * If `referenceDate` is `Invalid Date` or a value not convertible to valid `Date`,
  304. * then `Invalid Date` will be returned.
  305. *
  306. * The result may vary by locale.
  307. *
  308. * If `formatString` matches with `dateString` but does not provides tokens, `referenceDate` will be returned.
  309. *
  310. * If parsing failed, `Invalid Date` will be returned.
  311. * Invalid Date is a Date, whose time value is NaN.
  312. * Time value of Date: http://es5.github.io/#x15.9.1.1
  313. *
  314. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  315. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  316. *
  317. * @param dateStr - The string to parse
  318. * @param formatStr - The string of tokens
  319. * @param referenceDate - defines values missing from the parsed dateString
  320. * @param options - An object with options.
  321. * see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  322. * see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  323. *
  324. * @returns The parsed date
  325. *
  326. * @throws `options.locale` must contain `match` property
  327. * @throws use `yyyy` instead of `YYYY` for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  328. * @throws use `yy` instead of `YY` for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  329. * @throws use `d` instead of `D` for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  330. * @throws use `dd` instead of `DD` for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  331. * @throws format string contains an unescaped latin alphabet character
  332. *
  333. * @example
  334. * // Parse 11 February 2014 from middle-endian format:
  335. * var result = parse('02/11/2014', 'MM/dd/yyyy', new Date())
  336. * //=> Tue Feb 11 2014 00:00:00
  337. *
  338. * @example
  339. * // Parse 28th of February in Esperanto locale in the context of 2010 year:
  340. * import eo from 'date-fns/locale/eo'
  341. * var result = parse('28-a de februaro', "do 'de' MMMM", new Date(2010, 0, 1), {
  342. * locale: eo
  343. * })
  344. * //=> Sun Feb 28 2010 00:00:00
  345. */
  346. function parse(dateStr, formatStr, referenceDate, options) {
  347. const invalidDate = () =>
  348. (0, _index4.constructFrom)(options?.in || referenceDate, NaN);
  349. const defaultOptions = (0, _index5.getDefaultOptions)();
  350. const locale =
  351. options?.locale ?? defaultOptions.locale ?? _index.defaultLocale;
  352. const firstWeekContainsDate =
  353. options?.firstWeekContainsDate ??
  354. options?.locale?.options?.firstWeekContainsDate ??
  355. defaultOptions.firstWeekContainsDate ??
  356. defaultOptions.locale?.options?.firstWeekContainsDate ??
  357. 1;
  358. const weekStartsOn =
  359. options?.weekStartsOn ??
  360. options?.locale?.options?.weekStartsOn ??
  361. defaultOptions.weekStartsOn ??
  362. defaultOptions.locale?.options?.weekStartsOn ??
  363. 0;
  364. if (!formatStr)
  365. return dateStr
  366. ? invalidDate()
  367. : (0, _index6.toDate)(referenceDate, options?.in);
  368. const subFnOptions = {
  369. firstWeekContainsDate,
  370. weekStartsOn,
  371. locale,
  372. };
  373. // If timezone isn't specified, it will try to use the context or
  374. // the reference date and fallback to the system time zone.
  375. const setters = [new _Setter.DateTimezoneSetter(options?.in, referenceDate)];
  376. const tokens = formatStr
  377. .match(longFormattingTokensRegExp)
  378. .map((substring) => {
  379. const firstCharacter = substring[0];
  380. if (firstCharacter in _index2.longFormatters) {
  381. const longFormatter = _index2.longFormatters[firstCharacter];
  382. return longFormatter(substring, locale.formatLong);
  383. }
  384. return substring;
  385. })
  386. .join("")
  387. .match(formattingTokensRegExp);
  388. const usedTokens = [];
  389. for (let token of tokens) {
  390. if (
  391. !options?.useAdditionalWeekYearTokens &&
  392. (0, _index3.isProtectedWeekYearToken)(token)
  393. ) {
  394. (0, _index3.warnOrThrowProtectedError)(token, formatStr, dateStr);
  395. }
  396. if (
  397. !options?.useAdditionalDayOfYearTokens &&
  398. (0, _index3.isProtectedDayOfYearToken)(token)
  399. ) {
  400. (0, _index3.warnOrThrowProtectedError)(token, formatStr, dateStr);
  401. }
  402. const firstCharacter = token[0];
  403. const parser = _index7.parsers[firstCharacter];
  404. if (parser) {
  405. const { incompatibleTokens } = parser;
  406. if (Array.isArray(incompatibleTokens)) {
  407. const incompatibleToken = usedTokens.find(
  408. (usedToken) =>
  409. incompatibleTokens.includes(usedToken.token) ||
  410. usedToken.token === firstCharacter,
  411. );
  412. if (incompatibleToken) {
  413. throw new RangeError(
  414. `The format string mustn't contain \`${incompatibleToken.fullToken}\` and \`${token}\` at the same time`,
  415. );
  416. }
  417. } else if (parser.incompatibleTokens === "*" && usedTokens.length > 0) {
  418. throw new RangeError(
  419. `The format string mustn't contain \`${token}\` and any other token at the same time`,
  420. );
  421. }
  422. usedTokens.push({ token: firstCharacter, fullToken: token });
  423. const parseResult = parser.run(
  424. dateStr,
  425. token,
  426. locale.match,
  427. subFnOptions,
  428. );
  429. if (!parseResult) {
  430. return invalidDate();
  431. }
  432. setters.push(parseResult.setter);
  433. dateStr = parseResult.rest;
  434. } else {
  435. if (firstCharacter.match(unescapedLatinCharacterRegExp)) {
  436. throw new RangeError(
  437. "Format string contains an unescaped latin alphabet character `" +
  438. firstCharacter +
  439. "`",
  440. );
  441. }
  442. // Replace two single quote characters with one single quote character
  443. if (token === "''") {
  444. token = "'";
  445. } else if (firstCharacter === "'") {
  446. token = cleanEscapedString(token);
  447. }
  448. // Cut token from string, or, if string doesn't match the token, return Invalid Date
  449. if (dateStr.indexOf(token) === 0) {
  450. dateStr = dateStr.slice(token.length);
  451. } else {
  452. return invalidDate();
  453. }
  454. }
  455. }
  456. // Check if the remaining input contains something other than whitespace
  457. if (dateStr.length > 0 && notWhitespaceRegExp.test(dateStr)) {
  458. return invalidDate();
  459. }
  460. const uniquePrioritySetters = setters
  461. .map((setter) => setter.priority)
  462. .sort((a, b) => b - a)
  463. .filter((priority, index, array) => array.indexOf(priority) === index)
  464. .map((priority) =>
  465. setters
  466. .filter((setter) => setter.priority === priority)
  467. .sort((a, b) => b.subPriority - a.subPriority),
  468. )
  469. .map((setterArray) => setterArray[0]);
  470. let date = (0, _index6.toDate)(referenceDate, options?.in);
  471. if (isNaN(+date)) return invalidDate();
  472. const flags = {};
  473. for (const setter of uniquePrioritySetters) {
  474. if (!setter.validate(date, subFnOptions)) {
  475. return invalidDate();
  476. }
  477. const result = setter.set(date, flags, subFnOptions);
  478. // Result is tuple (date, flags)
  479. if (Array.isArray(result)) {
  480. date = result[0];
  481. Object.assign(flags, result[1]);
  482. // Result is date
  483. } else {
  484. date = result;
  485. }
  486. }
  487. return date;
  488. }
  489. function cleanEscapedString(input) {
  490. return input.match(escapedStringRegExp)[1].replace(doubleQuoteRegExp, "'");
  491. }