index.js 33 KB

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