index.js 32 KB

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