index.js 27 KB

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