index.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. import isValid from "../isValid/index.js";
  2. import subMilliseconds from "../subMilliseconds/index.js";
  3. import toDate from "../toDate/index.js";
  4. import formatters from "../_lib/format/formatters/index.js";
  5. import longFormatters from "../_lib/format/longFormatters/index.js";
  6. import getTimezoneOffsetInMilliseconds from "../_lib/getTimezoneOffsetInMilliseconds/index.js";
  7. import { isProtectedDayOfYearToken, isProtectedWeekYearToken, throwProtectedError } from "../_lib/protectedTokens/index.js";
  8. import toInteger from "../_lib/toInteger/index.js";
  9. import requiredArgs from "../_lib/requiredArgs/index.js";
  10. import { getDefaultOptions } from "../_lib/defaultOptions/index.js";
  11. import defaultLocale from "../_lib/defaultLocale/index.js"; // This RegExp consists of three parts separated by `|`:
  12. // - [yYQqMLwIdDecihHKkms]o matches any available ordinal number token
  13. // (one of the certain letters followed by `o`)
  14. // - (\w)\1* matches any sequences of the same letter
  15. // - '' matches two quote characters in a row
  16. // - '(''|[^'])+('|$) matches anything surrounded by two quote characters ('),
  17. // except a single quote symbol, which ends the sequence.
  18. // Two quote characters do not end the sequence.
  19. // If there is no matching single quote
  20. // then the sequence will continue until the end of the string.
  21. // - . matches any single character unmatched by previous parts of the RegExps
  22. var formattingTokensRegExp = /[yYQqMLwIdDecihHKkms]o|(\w)\1*|''|'(''|[^'])+('|$)|./g;
  23. // This RegExp catches symbols escaped by quotes, and also
  24. // sequences of symbols P, p, and the combinations like `PPPPPPPppppp`
  25. var longFormattingTokensRegExp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g;
  26. var escapedStringRegExp = /^'([^]*?)'?$/;
  27. var doubleQuoteRegExp = /''/g;
  28. var unescapedLatinCharacterRegExp = /[a-zA-Z]/;
  29. /**
  30. * @name format
  31. * @category Common Helpers
  32. * @summary Format the date.
  33. *
  34. * @description
  35. * Return the formatted date string in the given format. The result may vary by locale.
  36. *
  37. * > ⚠️ Please note that the `format` tokens differ from Moment.js and other libraries.
  38. * > See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  39. *
  40. * The characters wrapped between two single quotes characters (') are escaped.
  41. * Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.
  42. * (see the last example)
  43. *
  44. * Format of the string is based on Unicode Technical Standard #35:
  45. * https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
  46. * with a few additions (see note 7 below the table).
  47. *
  48. * Accepted patterns:
  49. * | Unit | Pattern | Result examples | Notes |
  50. * |---------------------------------|---------|-----------------------------------|-------|
  51. * | Era | G..GGG | AD, BC | |
  52. * | | GGGG | Anno Domini, Before Christ | 2 |
  53. * | | GGGGG | A, B | |
  54. * | Calendar year | y | 44, 1, 1900, 2017 | 5 |
  55. * | | yo | 44th, 1st, 0th, 17th | 5,7 |
  56. * | | yy | 44, 01, 00, 17 | 5 |
  57. * | | yyy | 044, 001, 1900, 2017 | 5 |
  58. * | | yyyy | 0044, 0001, 1900, 2017 | 5 |
  59. * | | yyyyy | ... | 3,5 |
  60. * | Local week-numbering year | Y | 44, 1, 1900, 2017 | 5 |
  61. * | | Yo | 44th, 1st, 1900th, 2017th | 5,7 |
  62. * | | YY | 44, 01, 00, 17 | 5,8 |
  63. * | | YYY | 044, 001, 1900, 2017 | 5 |
  64. * | | YYYY | 0044, 0001, 1900, 2017 | 5,8 |
  65. * | | YYYYY | ... | 3,5 |
  66. * | ISO week-numbering year | R | -43, 0, 1, 1900, 2017 | 5,7 |
  67. * | | RR | -43, 00, 01, 1900, 2017 | 5,7 |
  68. * | | RRR | -043, 000, 001, 1900, 2017 | 5,7 |
  69. * | | RRRR | -0043, 0000, 0001, 1900, 2017 | 5,7 |
  70. * | | RRRRR | ... | 3,5,7 |
  71. * | Extended year | u | -43, 0, 1, 1900, 2017 | 5 |
  72. * | | uu | -43, 01, 1900, 2017 | 5 |
  73. * | | uuu | -043, 001, 1900, 2017 | 5 |
  74. * | | uuuu | -0043, 0001, 1900, 2017 | 5 |
  75. * | | uuuuu | ... | 3,5 |
  76. * | Quarter (formatting) | Q | 1, 2, 3, 4 | |
  77. * | | Qo | 1st, 2nd, 3rd, 4th | 7 |
  78. * | | QQ | 01, 02, 03, 04 | |
  79. * | | QQQ | Q1, Q2, Q3, Q4 | |
  80. * | | QQQQ | 1st quarter, 2nd quarter, ... | 2 |
  81. * | | QQQQQ | 1, 2, 3, 4 | 4 |
  82. * | Quarter (stand-alone) | q | 1, 2, 3, 4 | |
  83. * | | qo | 1st, 2nd, 3rd, 4th | 7 |
  84. * | | qq | 01, 02, 03, 04 | |
  85. * | | qqq | Q1, Q2, Q3, Q4 | |
  86. * | | qqqq | 1st quarter, 2nd quarter, ... | 2 |
  87. * | | qqqqq | 1, 2, 3, 4 | 4 |
  88. * | Month (formatting) | M | 1, 2, ..., 12 | |
  89. * | | Mo | 1st, 2nd, ..., 12th | 7 |
  90. * | | MM | 01, 02, ..., 12 | |
  91. * | | MMM | Jan, Feb, ..., Dec | |
  92. * | | MMMM | January, February, ..., December | 2 |
  93. * | | MMMMM | J, F, ..., D | |
  94. * | Month (stand-alone) | L | 1, 2, ..., 12 | |
  95. * | | Lo | 1st, 2nd, ..., 12th | 7 |
  96. * | | LL | 01, 02, ..., 12 | |
  97. * | | LLL | Jan, Feb, ..., Dec | |
  98. * | | LLLL | January, February, ..., December | 2 |
  99. * | | LLLLL | J, F, ..., D | |
  100. * | Local week of year | w | 1, 2, ..., 53 | |
  101. * | | wo | 1st, 2nd, ..., 53th | 7 |
  102. * | | ww | 01, 02, ..., 53 | |
  103. * | ISO week of year | I | 1, 2, ..., 53 | 7 |
  104. * | | Io | 1st, 2nd, ..., 53th | 7 |
  105. * | | II | 01, 02, ..., 53 | 7 |
  106. * | Day of month | d | 1, 2, ..., 31 | |
  107. * | | do | 1st, 2nd, ..., 31st | 7 |
  108. * | | dd | 01, 02, ..., 31 | |
  109. * | Day of year | D | 1, 2, ..., 365, 366 | 9 |
  110. * | | Do | 1st, 2nd, ..., 365th, 366th | 7 |
  111. * | | DD | 01, 02, ..., 365, 366 | 9 |
  112. * | | DDD | 001, 002, ..., 365, 366 | |
  113. * | | DDDD | ... | 3 |
  114. * | Day of week (formatting) | E..EEE | Mon, Tue, Wed, ..., Sun | |
  115. * | | EEEE | Monday, Tuesday, ..., Sunday | 2 |
  116. * | | EEEEE | M, T, W, T, F, S, S | |
  117. * | | EEEEEE | Mo, Tu, We, Th, Fr, Sa, Su | |
  118. * | ISO day of week (formatting) | i | 1, 2, 3, ..., 7 | 7 |
  119. * | | io | 1st, 2nd, ..., 7th | 7 |
  120. * | | ii | 01, 02, ..., 07 | 7 |
  121. * | | iii | Mon, Tue, Wed, ..., Sun | 7 |
  122. * | | iiii | Monday, Tuesday, ..., Sunday | 2,7 |
  123. * | | iiiii | M, T, W, T, F, S, S | 7 |
  124. * | | iiiiii | Mo, Tu, We, Th, Fr, Sa, Su | 7 |
  125. * | Local day of week (formatting) | e | 2, 3, 4, ..., 1 | |
  126. * | | eo | 2nd, 3rd, ..., 1st | 7 |
  127. * | | ee | 02, 03, ..., 01 | |
  128. * | | eee | Mon, Tue, Wed, ..., Sun | |
  129. * | | eeee | Monday, Tuesday, ..., Sunday | 2 |
  130. * | | eeeee | M, T, W, T, F, S, S | |
  131. * | | eeeeee | Mo, Tu, We, Th, Fr, Sa, Su | |
  132. * | Local day of week (stand-alone) | c | 2, 3, 4, ..., 1 | |
  133. * | | co | 2nd, 3rd, ..., 1st | 7 |
  134. * | | cc | 02, 03, ..., 01 | |
  135. * | | ccc | Mon, Tue, Wed, ..., Sun | |
  136. * | | cccc | Monday, Tuesday, ..., Sunday | 2 |
  137. * | | ccccc | M, T, W, T, F, S, S | |
  138. * | | cccccc | Mo, Tu, We, Th, Fr, Sa, Su | |
  139. * | AM, PM | a..aa | AM, PM | |
  140. * | | aaa | am, pm | |
  141. * | | aaaa | a.m., p.m. | 2 |
  142. * | | aaaaa | a, p | |
  143. * | AM, PM, noon, midnight | b..bb | AM, PM, noon, midnight | |
  144. * | | bbb | am, pm, noon, midnight | |
  145. * | | bbbb | a.m., p.m., noon, midnight | 2 |
  146. * | | bbbbb | a, p, n, mi | |
  147. * | Flexible day period | B..BBB | at night, in the morning, ... | |
  148. * | | BBBB | at night, in the morning, ... | 2 |
  149. * | | BBBBB | at night, in the morning, ... | |
  150. * | Hour [1-12] | h | 1, 2, ..., 11, 12 | |
  151. * | | ho | 1st, 2nd, ..., 11th, 12th | 7 |
  152. * | | hh | 01, 02, ..., 11, 12 | |
  153. * | Hour [0-23] | H | 0, 1, 2, ..., 23 | |
  154. * | | Ho | 0th, 1st, 2nd, ..., 23rd | 7 |
  155. * | | HH | 00, 01, 02, ..., 23 | |
  156. * | Hour [0-11] | K | 1, 2, ..., 11, 0 | |
  157. * | | Ko | 1st, 2nd, ..., 11th, 0th | 7 |
  158. * | | KK | 01, 02, ..., 11, 00 | |
  159. * | Hour [1-24] | k | 24, 1, 2, ..., 23 | |
  160. * | | ko | 24th, 1st, 2nd, ..., 23rd | 7 |
  161. * | | kk | 24, 01, 02, ..., 23 | |
  162. * | Minute | m | 0, 1, ..., 59 | |
  163. * | | mo | 0th, 1st, ..., 59th | 7 |
  164. * | | mm | 00, 01, ..., 59 | |
  165. * | Second | s | 0, 1, ..., 59 | |
  166. * | | so | 0th, 1st, ..., 59th | 7 |
  167. * | | ss | 00, 01, ..., 59 | |
  168. * | Fraction of second | S | 0, 1, ..., 9 | |
  169. * | | SS | 00, 01, ..., 99 | |
  170. * | | SSS | 000, 001, ..., 999 | |
  171. * | | SSSS | ... | 3 |
  172. * | Timezone (ISO-8601 w/ Z) | X | -08, +0530, Z | |
  173. * | | XX | -0800, +0530, Z | |
  174. * | | XXX | -08:00, +05:30, Z | |
  175. * | | XXXX | -0800, +0530, Z, +123456 | 2 |
  176. * | | XXXXX | -08:00, +05:30, Z, +12:34:56 | |
  177. * | Timezone (ISO-8601 w/o Z) | x | -08, +0530, +00 | |
  178. * | | xx | -0800, +0530, +0000 | |
  179. * | | xxx | -08:00, +05:30, +00:00 | 2 |
  180. * | | xxxx | -0800, +0530, +0000, +123456 | |
  181. * | | xxxxx | -08:00, +05:30, +00:00, +12:34:56 | |
  182. * | Timezone (GMT) | O...OOO | GMT-8, GMT+5:30, GMT+0 | |
  183. * | | OOOO | GMT-08:00, GMT+05:30, GMT+00:00 | 2 |
  184. * | Timezone (specific non-locat.) | z...zzz | GMT-8, GMT+5:30, GMT+0 | 6 |
  185. * | | zzzz | GMT-08:00, GMT+05:30, GMT+00:00 | 2,6 |
  186. * | Seconds timestamp | t | 512969520 | 7 |
  187. * | | tt | ... | 3,7 |
  188. * | Milliseconds timestamp | T | 512969520900 | 7 |
  189. * | | TT | ... | 3,7 |
  190. * | Long localized date | P | 04/29/1453 | 7 |
  191. * | | PP | Apr 29, 1453 | 7 |
  192. * | | PPP | April 29th, 1453 | 7 |
  193. * | | PPPP | Friday, April 29th, 1453 | 2,7 |
  194. * | Long localized time | p | 12:00 AM | 7 |
  195. * | | pp | 12:00:00 AM | 7 |
  196. * | | ppp | 12:00:00 AM GMT+2 | 7 |
  197. * | | pppp | 12:00:00 AM GMT+02:00 | 2,7 |
  198. * | Combination of date and time | Pp | 04/29/1453, 12:00 AM | 7 |
  199. * | | PPpp | Apr 29, 1453, 12:00:00 AM | 7 |
  200. * | | PPPppp | April 29th, 1453 at ... | 7 |
  201. * | | PPPPpppp| Friday, April 29th, 1453 at ... | 2,7 |
  202. * Notes:
  203. * 1. "Formatting" units (e.g. formatting quarter) in the default en-US locale
  204. * are the same as "stand-alone" units, but are different in some languages.
  205. * "Formatting" units are declined according to the rules of the language
  206. * in the context of a date. "Stand-alone" units are always nominative singular:
  207. *
  208. * `format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'`
  209. *
  210. * `format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'`
  211. *
  212. * 2. Any sequence of the identical letters is a pattern, unless it is escaped by
  213. * the single quote characters (see below).
  214. * If the sequence is longer than listed in table (e.g. `EEEEEEEEEEE`)
  215. * the output will be the same as default pattern for this unit, usually
  216. * the longest one (in case of ISO weekdays, `EEEE`). Default patterns for units
  217. * are marked with "2" in the last column of the table.
  218. *
  219. * `format(new Date(2017, 10, 6), 'MMM') //=> 'Nov'`
  220. *
  221. * `format(new Date(2017, 10, 6), 'MMMM') //=> 'November'`
  222. *
  223. * `format(new Date(2017, 10, 6), 'MMMMM') //=> 'N'`
  224. *
  225. * `format(new Date(2017, 10, 6), 'MMMMMM') //=> 'November'`
  226. *
  227. * `format(new Date(2017, 10, 6), 'MMMMMMM') //=> 'November'`
  228. *
  229. * 3. Some patterns could be unlimited length (such as `yyyyyyyy`).
  230. * The output will be padded with zeros to match the length of the pattern.
  231. *
  232. * `format(new Date(2017, 10, 6), 'yyyyyyyy') //=> '00002017'`
  233. *
  234. * 4. `QQQQQ` and `qqqqq` could be not strictly numerical in some locales.
  235. * These tokens represent the shortest form of the quarter.
  236. *
  237. * 5. The main difference between `y` and `u` patterns are B.C. years:
  238. *
  239. * | Year | `y` | `u` |
  240. * |------|-----|-----|
  241. * | AC 1 | 1 | 1 |
  242. * | BC 1 | 1 | 0 |
  243. * | BC 2 | 2 | -1 |
  244. *
  245. * Also `yy` always returns the last two digits of a year,
  246. * while `uu` pads single digit years to 2 characters and returns other years unchanged:
  247. *
  248. * | Year | `yy` | `uu` |
  249. * |------|------|------|
  250. * | 1 | 01 | 01 |
  251. * | 14 | 14 | 14 |
  252. * | 376 | 76 | 376 |
  253. * | 1453 | 53 | 1453 |
  254. *
  255. * The same difference is true for local and ISO week-numbering years (`Y` and `R`),
  256. * except local week-numbering years are dependent on `options.weekStartsOn`
  257. * and `options.firstWeekContainsDate` (compare [getISOWeekYear]{@link https://date-fns.org/docs/getISOWeekYear}
  258. * and [getWeekYear]{@link https://date-fns.org/docs/getWeekYear}).
  259. *
  260. * 6. Specific non-location timezones are currently unavailable in `date-fns`,
  261. * so right now these tokens fall back to GMT timezones.
  262. *
  263. * 7. These patterns are not in the Unicode Technical Standard #35:
  264. * - `i`: ISO day of week
  265. * - `I`: ISO week of year
  266. * - `R`: ISO week-numbering year
  267. * - `t`: seconds timestamp
  268. * - `T`: milliseconds timestamp
  269. * - `o`: ordinal number modifier
  270. * - `P`: long localized date
  271. * - `p`: long localized time
  272. *
  273. * 8. `YY` and `YYYY` tokens represent week-numbering years but they are often confused with years.
  274. * You should enable `options.useAdditionalWeekYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  275. *
  276. * 9. `D` and `DD` tokens represent days of the year but they are often confused with days of the month.
  277. * You should enable `options.useAdditionalDayOfYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  278. *
  279. * @param {Date|Number} date - the original date
  280. * @param {String} format - the string of tokens
  281. * @param {Object} [options] - an object with options.
  282. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  283. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  284. * @param {Number} [options.firstWeekContainsDate=1] - the day of January, which is
  285. * @param {Boolean} [options.useAdditionalWeekYearTokens=false] - if true, allows usage of the week-numbering year tokens `YY` and `YYYY`;
  286. * see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  287. * @param {Boolean} [options.useAdditionalDayOfYearTokens=false] - if true, allows usage of the day of year tokens `D` and `DD`;
  288. * see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  289. * @returns {String} the formatted date string
  290. * @throws {TypeError} 2 arguments required
  291. * @throws {RangeError} `date` must not be Invalid Date
  292. * @throws {RangeError} `options.locale` must contain `localize` property
  293. * @throws {RangeError} `options.locale` must contain `formatLong` property
  294. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  295. * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
  296. * @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
  297. * @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
  298. * @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
  299. * @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
  300. * @throws {RangeError} format string contains an unescaped latin alphabet character
  301. *
  302. * @example
  303. * // Represent 11 February 2014 in middle-endian format:
  304. * const result = format(new Date(2014, 1, 11), 'MM/dd/yyyy')
  305. * //=> '02/11/2014'
  306. *
  307. * @example
  308. * // Represent 2 July 2014 in Esperanto:
  309. * import { eoLocale } from 'date-fns/locale/eo'
  310. * const result = format(new Date(2014, 6, 2), "do 'de' MMMM yyyy", {
  311. * locale: eoLocale
  312. * })
  313. * //=> '2-a de julio 2014'
  314. *
  315. * @example
  316. * // Escape string by single quote characters:
  317. * const result = format(new Date(2014, 6, 2, 15), "h 'o''clock'")
  318. * //=> "3 o'clock"
  319. */
  320. export default function format(dirtyDate, dirtyFormatStr, options) {
  321. 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;
  322. requiredArgs(2, arguments);
  323. var formatStr = String(dirtyFormatStr);
  324. var defaultOptions = getDefaultOptions();
  325. 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;
  326. 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);
  327. // Test if weekStartsOn is between 1 and 7 _and_ is not NaN
  328. if (!(firstWeekContainsDate >= 1 && firstWeekContainsDate <= 7)) {
  329. throw new RangeError('firstWeekContainsDate must be between 1 and 7 inclusively');
  330. }
  331. 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);
  332. // Test if weekStartsOn is between 0 and 6 _and_ is not NaN
  333. if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
  334. throw new RangeError('weekStartsOn must be between 0 and 6 inclusively');
  335. }
  336. if (!locale.localize) {
  337. throw new RangeError('locale must contain localize property');
  338. }
  339. if (!locale.formatLong) {
  340. throw new RangeError('locale must contain formatLong property');
  341. }
  342. var originalDate = toDate(dirtyDate);
  343. if (!isValid(originalDate)) {
  344. throw new RangeError('Invalid time value');
  345. }
  346. // Convert the date in system timezone to the same date in UTC+00:00 timezone.
  347. // This ensures that when UTC functions will be implemented, locales will be compatible with them.
  348. // See an issue about UTC functions: https://github.com/date-fns/date-fns/issues/376
  349. var timezoneOffset = getTimezoneOffsetInMilliseconds(originalDate);
  350. var utcDate = subMilliseconds(originalDate, timezoneOffset);
  351. var formatterOptions = {
  352. firstWeekContainsDate: firstWeekContainsDate,
  353. weekStartsOn: weekStartsOn,
  354. locale: locale,
  355. _originalDate: originalDate
  356. };
  357. var result = formatStr.match(longFormattingTokensRegExp).map(function (substring) {
  358. var firstCharacter = substring[0];
  359. if (firstCharacter === 'p' || firstCharacter === 'P') {
  360. var longFormatter = longFormatters[firstCharacter];
  361. return longFormatter(substring, locale.formatLong);
  362. }
  363. return substring;
  364. }).join('').match(formattingTokensRegExp).map(function (substring) {
  365. // Replace two single quote characters with one single quote character
  366. if (substring === "''") {
  367. return "'";
  368. }
  369. var firstCharacter = substring[0];
  370. if (firstCharacter === "'") {
  371. return cleanEscapedString(substring);
  372. }
  373. var formatter = formatters[firstCharacter];
  374. if (formatter) {
  375. if (!(options !== null && options !== void 0 && options.useAdditionalWeekYearTokens) && isProtectedWeekYearToken(substring)) {
  376. throwProtectedError(substring, dirtyFormatStr, String(dirtyDate));
  377. }
  378. if (!(options !== null && options !== void 0 && options.useAdditionalDayOfYearTokens) && isProtectedDayOfYearToken(substring)) {
  379. throwProtectedError(substring, dirtyFormatStr, String(dirtyDate));
  380. }
  381. return formatter(utcDate, substring, locale.localize, formatterOptions);
  382. }
  383. if (firstCharacter.match(unescapedLatinCharacterRegExp)) {
  384. throw new RangeError('Format string contains an unescaped latin alphabet character `' + firstCharacter + '`');
  385. }
  386. return substring;
  387. }).join('');
  388. return result;
  389. }
  390. function cleanEscapedString(input) {
  391. var matched = input.match(escapedStringRegExp);
  392. if (!matched) {
  393. return input;
  394. }
  395. return matched[1].replace(doubleQuoteRegExp, "'");
  396. }