index.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _index = _interopRequireDefault(require("../../../_lib/getUTCWeekYear/index.js"));
  7. var _index2 = _interopRequireDefault(require("../../../_lib/setUTCDay/index.js"));
  8. var _index3 = _interopRequireDefault(require("../../../_lib/setUTCISODay/index.js"));
  9. var _index4 = _interopRequireDefault(require("../../../_lib/setUTCISOWeek/index.js"));
  10. var _index5 = _interopRequireDefault(require("../../../_lib/setUTCWeek/index.js"));
  11. var _index6 = _interopRequireDefault(require("../../../_lib/startOfUTCISOWeek/index.js"));
  12. var _index7 = _interopRequireDefault(require("../../../_lib/startOfUTCWeek/index.js"));
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. var MILLISECONDS_IN_HOUR = 3600000;
  15. var MILLISECONDS_IN_MINUTE = 60000;
  16. var MILLISECONDS_IN_SECOND = 1000;
  17. var numericPatterns = {
  18. month: /^(1[0-2]|0?\d)/,
  19. // 0 to 12
  20. date: /^(3[0-1]|[0-2]?\d)/,
  21. // 0 to 31
  22. dayOfYear: /^(36[0-6]|3[0-5]\d|[0-2]?\d?\d)/,
  23. // 0 to 366
  24. week: /^(5[0-3]|[0-4]?\d)/,
  25. // 0 to 53
  26. hour23h: /^(2[0-3]|[0-1]?\d)/,
  27. // 0 to 23
  28. hour24h: /^(2[0-4]|[0-1]?\d)/,
  29. // 0 to 24
  30. hour11h: /^(1[0-1]|0?\d)/,
  31. // 0 to 11
  32. hour12h: /^(1[0-2]|0?\d)/,
  33. // 0 to 12
  34. minute: /^[0-5]?\d/,
  35. // 0 to 59
  36. second: /^[0-5]?\d/,
  37. // 0 to 59
  38. singleDigit: /^\d/,
  39. // 0 to 9
  40. twoDigits: /^\d{1,2}/,
  41. // 0 to 99
  42. threeDigits: /^\d{1,3}/,
  43. // 0 to 999
  44. fourDigits: /^\d{1,4}/,
  45. // 0 to 9999
  46. anyDigitsSigned: /^-?\d+/,
  47. singleDigitSigned: /^-?\d/,
  48. // 0 to 9, -0 to -9
  49. twoDigitsSigned: /^-?\d{1,2}/,
  50. // 0 to 99, -0 to -99
  51. threeDigitsSigned: /^-?\d{1,3}/,
  52. // 0 to 999, -0 to -999
  53. fourDigitsSigned: /^-?\d{1,4}/ // 0 to 9999, -0 to -9999
  54. };
  55. var timezonePatterns = {
  56. basicOptionalMinutes: /^([+-])(\d{2})(\d{2})?|Z/,
  57. basic: /^([+-])(\d{2})(\d{2})|Z/,
  58. basicOptionalSeconds: /^([+-])(\d{2})(\d{2})((\d{2}))?|Z/,
  59. extended: /^([+-])(\d{2}):(\d{2})|Z/,
  60. extendedOptionalSeconds: /^([+-])(\d{2}):(\d{2})(:(\d{2}))?|Z/
  61. };
  62. function parseNumericPattern(pattern, string, valueCallback) {
  63. var matchResult = string.match(pattern);
  64. if (!matchResult) {
  65. return null;
  66. }
  67. var value = parseInt(matchResult[0], 10);
  68. return {
  69. value: valueCallback ? valueCallback(value) : value,
  70. rest: string.slice(matchResult[0].length)
  71. };
  72. }
  73. function parseTimezonePattern(pattern, string) {
  74. var matchResult = string.match(pattern);
  75. if (!matchResult) {
  76. return null;
  77. } // Input is 'Z'
  78. if (matchResult[0] === 'Z') {
  79. return {
  80. value: 0,
  81. rest: string.slice(1)
  82. };
  83. }
  84. var sign = matchResult[1] === '+' ? 1 : -1;
  85. var hours = matchResult[2] ? parseInt(matchResult[2], 10) : 0;
  86. var minutes = matchResult[3] ? parseInt(matchResult[3], 10) : 0;
  87. var seconds = matchResult[5] ? parseInt(matchResult[5], 10) : 0;
  88. return {
  89. value: sign * (hours * MILLISECONDS_IN_HOUR + minutes * MILLISECONDS_IN_MINUTE + seconds * MILLISECONDS_IN_SECOND),
  90. rest: string.slice(matchResult[0].length)
  91. };
  92. }
  93. function parseAnyDigitsSigned(string, valueCallback) {
  94. return parseNumericPattern(numericPatterns.anyDigitsSigned, string, valueCallback);
  95. }
  96. function parseNDigits(n, string, valueCallback) {
  97. switch (n) {
  98. case 1:
  99. return parseNumericPattern(numericPatterns.singleDigit, string, valueCallback);
  100. case 2:
  101. return parseNumericPattern(numericPatterns.twoDigits, string, valueCallback);
  102. case 3:
  103. return parseNumericPattern(numericPatterns.threeDigits, string, valueCallback);
  104. case 4:
  105. return parseNumericPattern(numericPatterns.fourDigits, string, valueCallback);
  106. default:
  107. return parseNumericPattern(new RegExp('^\\d{1,' + n + '}'), string, valueCallback);
  108. }
  109. }
  110. function parseNDigitsSigned(n, string, valueCallback) {
  111. switch (n) {
  112. case 1:
  113. return parseNumericPattern(numericPatterns.singleDigitSigned, string, valueCallback);
  114. case 2:
  115. return parseNumericPattern(numericPatterns.twoDigitsSigned, string, valueCallback);
  116. case 3:
  117. return parseNumericPattern(numericPatterns.threeDigitsSigned, string, valueCallback);
  118. case 4:
  119. return parseNumericPattern(numericPatterns.fourDigitsSigned, string, valueCallback);
  120. default:
  121. return parseNumericPattern(new RegExp('^-?\\d{1,' + n + '}'), string, valueCallback);
  122. }
  123. }
  124. function dayPeriodEnumToHours(enumValue) {
  125. switch (enumValue) {
  126. case 'morning':
  127. return 4;
  128. case 'evening':
  129. return 17;
  130. case 'pm':
  131. case 'noon':
  132. case 'afternoon':
  133. return 12;
  134. case 'am':
  135. case 'midnight':
  136. case 'night':
  137. default:
  138. return 0;
  139. }
  140. }
  141. function normalizeTwoDigitYear(twoDigitYear, currentYear) {
  142. var isCommonEra = currentYear > 0; // Absolute number of the current year:
  143. // 1 -> 1 AC
  144. // 0 -> 1 BC
  145. // -1 -> 2 BC
  146. var absCurrentYear = isCommonEra ? currentYear : 1 - currentYear;
  147. var result;
  148. if (absCurrentYear <= 50) {
  149. result = twoDigitYear || 100;
  150. } else {
  151. var rangeEnd = absCurrentYear + 50;
  152. var rangeEndCentury = Math.floor(rangeEnd / 100) * 100;
  153. var isPreviousCentury = twoDigitYear >= rangeEnd % 100;
  154. result = twoDigitYear + rangeEndCentury - (isPreviousCentury ? 100 : 0);
  155. }
  156. return isCommonEra ? result : 1 - result;
  157. }
  158. var DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  159. var DAYS_IN_MONTH_LEAP_YEAR = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; // User for validation
  160. function isLeapYearIndex(year) {
  161. return year % 400 === 0 || year % 4 === 0 && year % 100 !== 0;
  162. }
  163. /*
  164. * | | Unit | | Unit |
  165. * |-----|--------------------------------|-----|--------------------------------|
  166. * | a | AM, PM | A* | Milliseconds in day |
  167. * | b | AM, PM, noon, midnight | B | Flexible day period |
  168. * | c | Stand-alone local day of week | C* | Localized hour w/ day period |
  169. * | d | Day of month | D | Day of year |
  170. * | e | Local day of week | E | Day of week |
  171. * | f | | F* | Day of week in month |
  172. * | g* | Modified Julian day | G | Era |
  173. * | h | Hour [1-12] | H | Hour [0-23] |
  174. * | i! | ISO day of week | I! | ISO week of year |
  175. * | j* | Localized hour w/ day period | J* | Localized hour w/o day period |
  176. * | k | Hour [1-24] | K | Hour [0-11] |
  177. * | l* | (deprecated) | L | Stand-alone month |
  178. * | m | Minute | M | Month |
  179. * | n | | N | |
  180. * | o! | Ordinal number modifier | O* | Timezone (GMT) |
  181. * | p | | P | |
  182. * | q | Stand-alone quarter | Q | Quarter |
  183. * | r* | Related Gregorian year | R! | ISO week-numbering year |
  184. * | s | Second | S | Fraction of second |
  185. * | t! | Seconds timestamp | T! | Milliseconds timestamp |
  186. * | u | Extended year | U* | Cyclic year |
  187. * | v* | Timezone (generic non-locat.) | V* | Timezone (location) |
  188. * | w | Local week of year | W* | Week of month |
  189. * | x | Timezone (ISO-8601 w/o Z) | X | Timezone (ISO-8601) |
  190. * | y | Year (abs) | Y | Local week-numbering year |
  191. * | z* | Timezone (specific non-locat.) | Z* | Timezone (aliases) |
  192. *
  193. * Letters marked by * are not implemented but reserved by Unicode standard.
  194. *
  195. * Letters marked by ! are non-standard, but implemented by date-fns:
  196. * - `o` modifies the previous token to turn it into an ordinal (see `parse` docs)
  197. * - `i` is ISO day of week. For `i` and `ii` is returns numeric ISO week days,
  198. * i.e. 7 for Sunday, 1 for Monday, etc.
  199. * - `I` is ISO week of year, as opposed to `w` which is local week of year.
  200. * - `R` is ISO week-numbering year, as opposed to `Y` which is local week-numbering year.
  201. * `R` is supposed to be used in conjunction with `I` and `i`
  202. * for universal ISO week-numbering date, whereas
  203. * `Y` is supposed to be used in conjunction with `w` and `e`
  204. * for week-numbering date specific to the locale.
  205. */
  206. var parsers = {
  207. // Era
  208. G: {
  209. priority: 140,
  210. parse: function (string, token, match, _options) {
  211. switch (token) {
  212. // AD, BC
  213. case 'G':
  214. case 'GG':
  215. case 'GGG':
  216. return match.era(string, {
  217. width: 'abbreviated'
  218. }) || match.era(string, {
  219. width: 'narrow'
  220. });
  221. // A, B
  222. case 'GGGGG':
  223. return match.era(string, {
  224. width: 'narrow'
  225. });
  226. // Anno Domini, Before Christ
  227. case 'GGGG':
  228. default:
  229. return match.era(string, {
  230. width: 'wide'
  231. }) || match.era(string, {
  232. width: 'abbreviated'
  233. }) || match.era(string, {
  234. width: 'narrow'
  235. });
  236. }
  237. },
  238. set: function (date, flags, value, _options) {
  239. flags.era = value;
  240. date.setUTCFullYear(value, 0, 1);
  241. date.setUTCHours(0, 0, 0, 0);
  242. return date;
  243. },
  244. incompatibleTokens: ['R', 'u', 't', 'T']
  245. },
  246. // Year
  247. y: {
  248. // From http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
  249. // | Year | y | yy | yyy | yyyy | yyyyy |
  250. // |----------|-------|----|-------|-------|-------|
  251. // | AD 1 | 1 | 01 | 001 | 0001 | 00001 |
  252. // | AD 12 | 12 | 12 | 012 | 0012 | 00012 |
  253. // | AD 123 | 123 | 23 | 123 | 0123 | 00123 |
  254. // | AD 1234 | 1234 | 34 | 1234 | 1234 | 01234 |
  255. // | AD 12345 | 12345 | 45 | 12345 | 12345 | 12345 |
  256. priority: 130,
  257. parse: function (string, token, match, _options) {
  258. var valueCallback = function (year) {
  259. return {
  260. year: year,
  261. isTwoDigitYear: token === 'yy'
  262. };
  263. };
  264. switch (token) {
  265. case 'y':
  266. return parseNDigits(4, string, valueCallback);
  267. case 'yo':
  268. return match.ordinalNumber(string, {
  269. unit: 'year',
  270. valueCallback: valueCallback
  271. });
  272. default:
  273. return parseNDigits(token.length, string, valueCallback);
  274. }
  275. },
  276. validate: function (_date, value, _options) {
  277. return value.isTwoDigitYear || value.year > 0;
  278. },
  279. set: function (date, flags, value, _options) {
  280. var currentYear = date.getUTCFullYear();
  281. if (value.isTwoDigitYear) {
  282. var normalizedTwoDigitYear = normalizeTwoDigitYear(value.year, currentYear);
  283. date.setUTCFullYear(normalizedTwoDigitYear, 0, 1);
  284. date.setUTCHours(0, 0, 0, 0);
  285. return date;
  286. }
  287. var year = !('era' in flags) || flags.era === 1 ? value.year : 1 - value.year;
  288. date.setUTCFullYear(year, 0, 1);
  289. date.setUTCHours(0, 0, 0, 0);
  290. return date;
  291. },
  292. incompatibleTokens: ['Y', 'R', 'u', 'w', 'I', 'i', 'e', 'c', 't', 'T']
  293. },
  294. // Local week-numbering year
  295. Y: {
  296. priority: 130,
  297. parse: function (string, token, match, _options) {
  298. var valueCallback = function (year) {
  299. return {
  300. year: year,
  301. isTwoDigitYear: token === 'YY'
  302. };
  303. };
  304. switch (token) {
  305. case 'Y':
  306. return parseNDigits(4, string, valueCallback);
  307. case 'Yo':
  308. return match.ordinalNumber(string, {
  309. unit: 'year',
  310. valueCallback: valueCallback
  311. });
  312. default:
  313. return parseNDigits(token.length, string, valueCallback);
  314. }
  315. },
  316. validate: function (_date, value, _options) {
  317. return value.isTwoDigitYear || value.year > 0;
  318. },
  319. set: function (date, flags, value, options) {
  320. var currentYear = (0, _index.default)(date, options);
  321. if (value.isTwoDigitYear) {
  322. var normalizedTwoDigitYear = normalizeTwoDigitYear(value.year, currentYear);
  323. date.setUTCFullYear(normalizedTwoDigitYear, 0, options.firstWeekContainsDate);
  324. date.setUTCHours(0, 0, 0, 0);
  325. return (0, _index7.default)(date, options);
  326. }
  327. var year = !('era' in flags) || flags.era === 1 ? value.year : 1 - value.year;
  328. date.setUTCFullYear(year, 0, options.firstWeekContainsDate);
  329. date.setUTCHours(0, 0, 0, 0);
  330. return (0, _index7.default)(date, options);
  331. },
  332. incompatibleTokens: ['y', 'R', 'u', 'Q', 'q', 'M', 'L', 'I', 'd', 'D', 'i', 't', 'T']
  333. },
  334. // ISO week-numbering year
  335. R: {
  336. priority: 130,
  337. parse: function (string, token, _match, _options) {
  338. if (token === 'R') {
  339. return parseNDigitsSigned(4, string);
  340. }
  341. return parseNDigitsSigned(token.length, string);
  342. },
  343. set: function (_date, _flags, value, _options) {
  344. var firstWeekOfYear = new Date(0);
  345. firstWeekOfYear.setUTCFullYear(value, 0, 4);
  346. firstWeekOfYear.setUTCHours(0, 0, 0, 0);
  347. return (0, _index6.default)(firstWeekOfYear);
  348. },
  349. incompatibleTokens: ['G', 'y', 'Y', 'u', 'Q', 'q', 'M', 'L', 'w', 'd', 'D', 'e', 'c', 't', 'T']
  350. },
  351. // Extended year
  352. u: {
  353. priority: 130,
  354. parse: function (string, token, _match, _options) {
  355. if (token === 'u') {
  356. return parseNDigitsSigned(4, string);
  357. }
  358. return parseNDigitsSigned(token.length, string);
  359. },
  360. set: function (date, _flags, value, _options) {
  361. date.setUTCFullYear(value, 0, 1);
  362. date.setUTCHours(0, 0, 0, 0);
  363. return date;
  364. },
  365. incompatibleTokens: ['G', 'y', 'Y', 'R', 'w', 'I', 'i', 'e', 'c', 't', 'T']
  366. },
  367. // Quarter
  368. Q: {
  369. priority: 120,
  370. parse: function (string, token, match, _options) {
  371. switch (token) {
  372. // 1, 2, 3, 4
  373. case 'Q':
  374. case 'QQ':
  375. // 01, 02, 03, 04
  376. return parseNDigits(token.length, string);
  377. // 1st, 2nd, 3rd, 4th
  378. case 'Qo':
  379. return match.ordinalNumber(string, {
  380. unit: 'quarter'
  381. });
  382. // Q1, Q2, Q3, Q4
  383. case 'QQQ':
  384. return match.quarter(string, {
  385. width: 'abbreviated',
  386. context: 'formatting'
  387. }) || match.quarter(string, {
  388. width: 'narrow',
  389. context: 'formatting'
  390. });
  391. // 1, 2, 3, 4 (narrow quarter; could be not numerical)
  392. case 'QQQQQ':
  393. return match.quarter(string, {
  394. width: 'narrow',
  395. context: 'formatting'
  396. });
  397. // 1st quarter, 2nd quarter, ...
  398. case 'QQQQ':
  399. default:
  400. return match.quarter(string, {
  401. width: 'wide',
  402. context: 'formatting'
  403. }) || match.quarter(string, {
  404. width: 'abbreviated',
  405. context: 'formatting'
  406. }) || match.quarter(string, {
  407. width: 'narrow',
  408. context: 'formatting'
  409. });
  410. }
  411. },
  412. validate: function (_date, value, _options) {
  413. return value >= 1 && value <= 4;
  414. },
  415. set: function (date, _flags, value, _options) {
  416. date.setUTCMonth((value - 1) * 3, 1);
  417. date.setUTCHours(0, 0, 0, 0);
  418. return date;
  419. },
  420. incompatibleTokens: ['Y', 'R', 'q', 'M', 'L', 'w', 'I', 'd', 'D', 'i', 'e', 'c', 't', 'T']
  421. },
  422. // Stand-alone quarter
  423. q: {
  424. priority: 120,
  425. parse: function (string, token, match, _options) {
  426. switch (token) {
  427. // 1, 2, 3, 4
  428. case 'q':
  429. case 'qq':
  430. // 01, 02, 03, 04
  431. return parseNDigits(token.length, string);
  432. // 1st, 2nd, 3rd, 4th
  433. case 'qo':
  434. return match.ordinalNumber(string, {
  435. unit: 'quarter'
  436. });
  437. // Q1, Q2, Q3, Q4
  438. case 'qqq':
  439. return match.quarter(string, {
  440. width: 'abbreviated',
  441. context: 'standalone'
  442. }) || match.quarter(string, {
  443. width: 'narrow',
  444. context: 'standalone'
  445. });
  446. // 1, 2, 3, 4 (narrow quarter; could be not numerical)
  447. case 'qqqqq':
  448. return match.quarter(string, {
  449. width: 'narrow',
  450. context: 'standalone'
  451. });
  452. // 1st quarter, 2nd quarter, ...
  453. case 'qqqq':
  454. default:
  455. return match.quarter(string, {
  456. width: 'wide',
  457. context: 'standalone'
  458. }) || match.quarter(string, {
  459. width: 'abbreviated',
  460. context: 'standalone'
  461. }) || match.quarter(string, {
  462. width: 'narrow',
  463. context: 'standalone'
  464. });
  465. }
  466. },
  467. validate: function (_date, value, _options) {
  468. return value >= 1 && value <= 4;
  469. },
  470. set: function (date, _flags, value, _options) {
  471. date.setUTCMonth((value - 1) * 3, 1);
  472. date.setUTCHours(0, 0, 0, 0);
  473. return date;
  474. },
  475. incompatibleTokens: ['Y', 'R', 'Q', 'M', 'L', 'w', 'I', 'd', 'D', 'i', 'e', 'c', 't', 'T']
  476. },
  477. // Month
  478. M: {
  479. priority: 110,
  480. parse: function (string, token, match, _options) {
  481. var valueCallback = function (value) {
  482. return value - 1;
  483. };
  484. switch (token) {
  485. // 1, 2, ..., 12
  486. case 'M':
  487. return parseNumericPattern(numericPatterns.month, string, valueCallback);
  488. // 01, 02, ..., 12
  489. case 'MM':
  490. return parseNDigits(2, string, valueCallback);
  491. // 1st, 2nd, ..., 12th
  492. case 'Mo':
  493. return match.ordinalNumber(string, {
  494. unit: 'month',
  495. valueCallback: valueCallback
  496. });
  497. // Jan, Feb, ..., Dec
  498. case 'MMM':
  499. return match.month(string, {
  500. width: 'abbreviated',
  501. context: 'formatting'
  502. }) || match.month(string, {
  503. width: 'narrow',
  504. context: 'formatting'
  505. });
  506. // J, F, ..., D
  507. case 'MMMMM':
  508. return match.month(string, {
  509. width: 'narrow',
  510. context: 'formatting'
  511. });
  512. // January, February, ..., December
  513. case 'MMMM':
  514. default:
  515. return match.month(string, {
  516. width: 'wide',
  517. context: 'formatting'
  518. }) || match.month(string, {
  519. width: 'abbreviated',
  520. context: 'formatting'
  521. }) || match.month(string, {
  522. width: 'narrow',
  523. context: 'formatting'
  524. });
  525. }
  526. },
  527. validate: function (_date, value, _options) {
  528. return value >= 0 && value <= 11;
  529. },
  530. set: function (date, _flags, value, _options) {
  531. date.setUTCMonth(value, 1);
  532. date.setUTCHours(0, 0, 0, 0);
  533. return date;
  534. },
  535. incompatibleTokens: ['Y', 'R', 'q', 'Q', 'L', 'w', 'I', 'D', 'i', 'e', 'c', 't', 'T']
  536. },
  537. // Stand-alone month
  538. L: {
  539. priority: 110,
  540. parse: function (string, token, match, _options) {
  541. var valueCallback = function (value) {
  542. return value - 1;
  543. };
  544. switch (token) {
  545. // 1, 2, ..., 12
  546. case 'L':
  547. return parseNumericPattern(numericPatterns.month, string, valueCallback);
  548. // 01, 02, ..., 12
  549. case 'LL':
  550. return parseNDigits(2, string, valueCallback);
  551. // 1st, 2nd, ..., 12th
  552. case 'Lo':
  553. return match.ordinalNumber(string, {
  554. unit: 'month',
  555. valueCallback: valueCallback
  556. });
  557. // Jan, Feb, ..., Dec
  558. case 'LLL':
  559. return match.month(string, {
  560. width: 'abbreviated',
  561. context: 'standalone'
  562. }) || match.month(string, {
  563. width: 'narrow',
  564. context: 'standalone'
  565. });
  566. // J, F, ..., D
  567. case 'LLLLL':
  568. return match.month(string, {
  569. width: 'narrow',
  570. context: 'standalone'
  571. });
  572. // January, February, ..., December
  573. case 'LLLL':
  574. default:
  575. return match.month(string, {
  576. width: 'wide',
  577. context: 'standalone'
  578. }) || match.month(string, {
  579. width: 'abbreviated',
  580. context: 'standalone'
  581. }) || match.month(string, {
  582. width: 'narrow',
  583. context: 'standalone'
  584. });
  585. }
  586. },
  587. validate: function (_date, value, _options) {
  588. return value >= 0 && value <= 11;
  589. },
  590. set: function (date, _flags, value, _options) {
  591. date.setUTCMonth(value, 1);
  592. date.setUTCHours(0, 0, 0, 0);
  593. return date;
  594. },
  595. incompatibleTokens: ['Y', 'R', 'q', 'Q', 'M', 'w', 'I', 'D', 'i', 'e', 'c', 't', 'T']
  596. },
  597. // Local week of year
  598. w: {
  599. priority: 100,
  600. parse: function (string, token, match, _options) {
  601. switch (token) {
  602. case 'w':
  603. return parseNumericPattern(numericPatterns.week, string);
  604. case 'wo':
  605. return match.ordinalNumber(string, {
  606. unit: 'week'
  607. });
  608. default:
  609. return parseNDigits(token.length, string);
  610. }
  611. },
  612. validate: function (_date, value, _options) {
  613. return value >= 1 && value <= 53;
  614. },
  615. set: function (date, _flags, value, options) {
  616. return (0, _index7.default)((0, _index5.default)(date, value, options), options);
  617. },
  618. incompatibleTokens: ['y', 'R', 'u', 'q', 'Q', 'M', 'L', 'I', 'd', 'D', 'i', 't', 'T']
  619. },
  620. // ISO week of year
  621. I: {
  622. priority: 100,
  623. parse: function (string, token, match, _options) {
  624. switch (token) {
  625. case 'I':
  626. return parseNumericPattern(numericPatterns.week, string);
  627. case 'Io':
  628. return match.ordinalNumber(string, {
  629. unit: 'week'
  630. });
  631. default:
  632. return parseNDigits(token.length, string);
  633. }
  634. },
  635. validate: function (_date, value, _options) {
  636. return value >= 1 && value <= 53;
  637. },
  638. set: function (date, _flags, value, options) {
  639. return (0, _index6.default)((0, _index4.default)(date, value, options), options);
  640. },
  641. incompatibleTokens: ['y', 'Y', 'u', 'q', 'Q', 'M', 'L', 'w', 'd', 'D', 'e', 'c', 't', 'T']
  642. },
  643. // Day of the month
  644. d: {
  645. priority: 90,
  646. subPriority: 1,
  647. parse: function (string, token, match, _options) {
  648. switch (token) {
  649. case 'd':
  650. return parseNumericPattern(numericPatterns.date, string);
  651. case 'do':
  652. return match.ordinalNumber(string, {
  653. unit: 'date'
  654. });
  655. default:
  656. return parseNDigits(token.length, string);
  657. }
  658. },
  659. validate: function (date, value, _options) {
  660. var year = date.getUTCFullYear();
  661. var isLeapYear = isLeapYearIndex(year);
  662. var month = date.getUTCMonth();
  663. if (isLeapYear) {
  664. return value >= 1 && value <= DAYS_IN_MONTH_LEAP_YEAR[month];
  665. } else {
  666. return value >= 1 && value <= DAYS_IN_MONTH[month];
  667. }
  668. },
  669. set: function (date, _flags, value, _options) {
  670. date.setUTCDate(value);
  671. date.setUTCHours(0, 0, 0, 0);
  672. return date;
  673. },
  674. incompatibleTokens: ['Y', 'R', 'q', 'Q', 'w', 'I', 'D', 'i', 'e', 'c', 't', 'T']
  675. },
  676. // Day of year
  677. D: {
  678. priority: 90,
  679. subPriority: 1,
  680. parse: function (string, token, match, _options) {
  681. switch (token) {
  682. case 'D':
  683. case 'DD':
  684. return parseNumericPattern(numericPatterns.dayOfYear, string);
  685. case 'Do':
  686. return match.ordinalNumber(string, {
  687. unit: 'date'
  688. });
  689. default:
  690. return parseNDigits(token.length, string);
  691. }
  692. },
  693. validate: function (date, value, _options) {
  694. var year = date.getUTCFullYear();
  695. var isLeapYear = isLeapYearIndex(year);
  696. if (isLeapYear) {
  697. return value >= 1 && value <= 366;
  698. } else {
  699. return value >= 1 && value <= 365;
  700. }
  701. },
  702. set: function (date, _flags, value, _options) {
  703. date.setUTCMonth(0, value);
  704. date.setUTCHours(0, 0, 0, 0);
  705. return date;
  706. },
  707. incompatibleTokens: ['Y', 'R', 'q', 'Q', 'M', 'L', 'w', 'I', 'd', 'E', 'i', 'e', 'c', 't', 'T']
  708. },
  709. // Day of week
  710. E: {
  711. priority: 90,
  712. parse: function (string, token, match, _options) {
  713. switch (token) {
  714. // Tue
  715. case 'E':
  716. case 'EE':
  717. case 'EEE':
  718. return match.day(string, {
  719. width: 'abbreviated',
  720. context: 'formatting'
  721. }) || match.day(string, {
  722. width: 'short',
  723. context: 'formatting'
  724. }) || match.day(string, {
  725. width: 'narrow',
  726. context: 'formatting'
  727. });
  728. // T
  729. case 'EEEEE':
  730. return match.day(string, {
  731. width: 'narrow',
  732. context: 'formatting'
  733. });
  734. // Tu
  735. case 'EEEEEE':
  736. return match.day(string, {
  737. width: 'short',
  738. context: 'formatting'
  739. }) || match.day(string, {
  740. width: 'narrow',
  741. context: 'formatting'
  742. });
  743. // Tuesday
  744. case 'EEEE':
  745. default:
  746. return match.day(string, {
  747. width: 'wide',
  748. context: 'formatting'
  749. }) || match.day(string, {
  750. width: 'abbreviated',
  751. context: 'formatting'
  752. }) || match.day(string, {
  753. width: 'short',
  754. context: 'formatting'
  755. }) || match.day(string, {
  756. width: 'narrow',
  757. context: 'formatting'
  758. });
  759. }
  760. },
  761. validate: function (_date, value, _options) {
  762. return value >= 0 && value <= 6;
  763. },
  764. set: function (date, _flags, value, options) {
  765. date = (0, _index2.default)(date, value, options);
  766. date.setUTCHours(0, 0, 0, 0);
  767. return date;
  768. },
  769. incompatibleTokens: ['D', 'i', 'e', 'c', 't', 'T']
  770. },
  771. // Local day of week
  772. e: {
  773. priority: 90,
  774. parse: function (string, token, match, options) {
  775. var valueCallback = function (value) {
  776. var wholeWeekDays = Math.floor((value - 1) / 7) * 7;
  777. return (value + options.weekStartsOn + 6) % 7 + wholeWeekDays;
  778. };
  779. switch (token) {
  780. // 3
  781. case 'e':
  782. case 'ee':
  783. // 03
  784. return parseNDigits(token.length, string, valueCallback);
  785. // 3rd
  786. case 'eo':
  787. return match.ordinalNumber(string, {
  788. unit: 'day',
  789. valueCallback: valueCallback
  790. });
  791. // Tue
  792. case 'eee':
  793. return match.day(string, {
  794. width: 'abbreviated',
  795. context: 'formatting'
  796. }) || match.day(string, {
  797. width: 'short',
  798. context: 'formatting'
  799. }) || match.day(string, {
  800. width: 'narrow',
  801. context: 'formatting'
  802. });
  803. // T
  804. case 'eeeee':
  805. return match.day(string, {
  806. width: 'narrow',
  807. context: 'formatting'
  808. });
  809. // Tu
  810. case 'eeeeee':
  811. return match.day(string, {
  812. width: 'short',
  813. context: 'formatting'
  814. }) || match.day(string, {
  815. width: 'narrow',
  816. context: 'formatting'
  817. });
  818. // Tuesday
  819. case 'eeee':
  820. default:
  821. return match.day(string, {
  822. width: 'wide',
  823. context: 'formatting'
  824. }) || match.day(string, {
  825. width: 'abbreviated',
  826. context: 'formatting'
  827. }) || match.day(string, {
  828. width: 'short',
  829. context: 'formatting'
  830. }) || match.day(string, {
  831. width: 'narrow',
  832. context: 'formatting'
  833. });
  834. }
  835. },
  836. validate: function (_date, value, _options) {
  837. return value >= 0 && value <= 6;
  838. },
  839. set: function (date, _flags, value, options) {
  840. date = (0, _index2.default)(date, value, options);
  841. date.setUTCHours(0, 0, 0, 0);
  842. return date;
  843. },
  844. incompatibleTokens: ['y', 'R', 'u', 'q', 'Q', 'M', 'L', 'I', 'd', 'D', 'E', 'i', 'c', 't', 'T']
  845. },
  846. // Stand-alone local day of week
  847. c: {
  848. priority: 90,
  849. parse: function (string, token, match, options) {
  850. var valueCallback = function (value) {
  851. var wholeWeekDays = Math.floor((value - 1) / 7) * 7;
  852. return (value + options.weekStartsOn + 6) % 7 + wholeWeekDays;
  853. };
  854. switch (token) {
  855. // 3
  856. case 'c':
  857. case 'cc':
  858. // 03
  859. return parseNDigits(token.length, string, valueCallback);
  860. // 3rd
  861. case 'co':
  862. return match.ordinalNumber(string, {
  863. unit: 'day',
  864. valueCallback: valueCallback
  865. });
  866. // Tue
  867. case 'ccc':
  868. return match.day(string, {
  869. width: 'abbreviated',
  870. context: 'standalone'
  871. }) || match.day(string, {
  872. width: 'short',
  873. context: 'standalone'
  874. }) || match.day(string, {
  875. width: 'narrow',
  876. context: 'standalone'
  877. });
  878. // T
  879. case 'ccccc':
  880. return match.day(string, {
  881. width: 'narrow',
  882. context: 'standalone'
  883. });
  884. // Tu
  885. case 'cccccc':
  886. return match.day(string, {
  887. width: 'short',
  888. context: 'standalone'
  889. }) || match.day(string, {
  890. width: 'narrow',
  891. context: 'standalone'
  892. });
  893. // Tuesday
  894. case 'cccc':
  895. default:
  896. return match.day(string, {
  897. width: 'wide',
  898. context: 'standalone'
  899. }) || match.day(string, {
  900. width: 'abbreviated',
  901. context: 'standalone'
  902. }) || match.day(string, {
  903. width: 'short',
  904. context: 'standalone'
  905. }) || match.day(string, {
  906. width: 'narrow',
  907. context: 'standalone'
  908. });
  909. }
  910. },
  911. validate: function (_date, value, _options) {
  912. return value >= 0 && value <= 6;
  913. },
  914. set: function (date, _flags, value, options) {
  915. date = (0, _index2.default)(date, value, options);
  916. date.setUTCHours(0, 0, 0, 0);
  917. return date;
  918. },
  919. incompatibleTokens: ['y', 'R', 'u', 'q', 'Q', 'M', 'L', 'I', 'd', 'D', 'E', 'i', 'e', 't', 'T']
  920. },
  921. // ISO day of week
  922. i: {
  923. priority: 90,
  924. parse: function (string, token, match, _options) {
  925. var valueCallback = function (value) {
  926. if (value === 0) {
  927. return 7;
  928. }
  929. return value;
  930. };
  931. switch (token) {
  932. // 2
  933. case 'i':
  934. case 'ii':
  935. // 02
  936. return parseNDigits(token.length, string);
  937. // 2nd
  938. case 'io':
  939. return match.ordinalNumber(string, {
  940. unit: 'day'
  941. });
  942. // Tue
  943. case 'iii':
  944. return match.day(string, {
  945. width: 'abbreviated',
  946. context: 'formatting',
  947. valueCallback: valueCallback
  948. }) || match.day(string, {
  949. width: 'short',
  950. context: 'formatting',
  951. valueCallback: valueCallback
  952. }) || match.day(string, {
  953. width: 'narrow',
  954. context: 'formatting',
  955. valueCallback: valueCallback
  956. });
  957. // T
  958. case 'iiiii':
  959. return match.day(string, {
  960. width: 'narrow',
  961. context: 'formatting',
  962. valueCallback: valueCallback
  963. });
  964. // Tu
  965. case 'iiiiii':
  966. return match.day(string, {
  967. width: 'short',
  968. context: 'formatting',
  969. valueCallback: valueCallback
  970. }) || match.day(string, {
  971. width: 'narrow',
  972. context: 'formatting',
  973. valueCallback: valueCallback
  974. });
  975. // Tuesday
  976. case 'iiii':
  977. default:
  978. return match.day(string, {
  979. width: 'wide',
  980. context: 'formatting',
  981. valueCallback: valueCallback
  982. }) || match.day(string, {
  983. width: 'abbreviated',
  984. context: 'formatting',
  985. valueCallback: valueCallback
  986. }) || match.day(string, {
  987. width: 'short',
  988. context: 'formatting',
  989. valueCallback: valueCallback
  990. }) || match.day(string, {
  991. width: 'narrow',
  992. context: 'formatting',
  993. valueCallback: valueCallback
  994. });
  995. }
  996. },
  997. validate: function (_date, value, _options) {
  998. return value >= 1 && value <= 7;
  999. },
  1000. set: function (date, _flags, value, options) {
  1001. date = (0, _index3.default)(date, value, options);
  1002. date.setUTCHours(0, 0, 0, 0);
  1003. return date;
  1004. },
  1005. incompatibleTokens: ['y', 'Y', 'u', 'q', 'Q', 'M', 'L', 'w', 'd', 'D', 'E', 'e', 'c', 't', 'T']
  1006. },
  1007. // AM or PM
  1008. a: {
  1009. priority: 80,
  1010. parse: function (string, token, match, _options) {
  1011. switch (token) {
  1012. case 'a':
  1013. case 'aa':
  1014. case 'aaa':
  1015. return match.dayPeriod(string, {
  1016. width: 'abbreviated',
  1017. context: 'formatting'
  1018. }) || match.dayPeriod(string, {
  1019. width: 'narrow',
  1020. context: 'formatting'
  1021. });
  1022. case 'aaaaa':
  1023. return match.dayPeriod(string, {
  1024. width: 'narrow',
  1025. context: 'formatting'
  1026. });
  1027. case 'aaaa':
  1028. default:
  1029. return match.dayPeriod(string, {
  1030. width: 'wide',
  1031. context: 'formatting'
  1032. }) || match.dayPeriod(string, {
  1033. width: 'abbreviated',
  1034. context: 'formatting'
  1035. }) || match.dayPeriod(string, {
  1036. width: 'narrow',
  1037. context: 'formatting'
  1038. });
  1039. }
  1040. },
  1041. set: function (date, _flags, value, _options) {
  1042. date.setUTCHours(dayPeriodEnumToHours(value), 0, 0, 0);
  1043. return date;
  1044. },
  1045. incompatibleTokens: ['b', 'B', 'H', 'K', 'k', 't', 'T']
  1046. },
  1047. // AM, PM, midnight
  1048. b: {
  1049. priority: 80,
  1050. parse: function (string, token, match, _options) {
  1051. switch (token) {
  1052. case 'b':
  1053. case 'bb':
  1054. case 'bbb':
  1055. return match.dayPeriod(string, {
  1056. width: 'abbreviated',
  1057. context: 'formatting'
  1058. }) || match.dayPeriod(string, {
  1059. width: 'narrow',
  1060. context: 'formatting'
  1061. });
  1062. case 'bbbbb':
  1063. return match.dayPeriod(string, {
  1064. width: 'narrow',
  1065. context: 'formatting'
  1066. });
  1067. case 'bbbb':
  1068. default:
  1069. return match.dayPeriod(string, {
  1070. width: 'wide',
  1071. context: 'formatting'
  1072. }) || match.dayPeriod(string, {
  1073. width: 'abbreviated',
  1074. context: 'formatting'
  1075. }) || match.dayPeriod(string, {
  1076. width: 'narrow',
  1077. context: 'formatting'
  1078. });
  1079. }
  1080. },
  1081. set: function (date, _flags, value, _options) {
  1082. date.setUTCHours(dayPeriodEnumToHours(value), 0, 0, 0);
  1083. return date;
  1084. },
  1085. incompatibleTokens: ['a', 'B', 'H', 'K', 'k', 't', 'T']
  1086. },
  1087. // in the morning, in the afternoon, in the evening, at night
  1088. B: {
  1089. priority: 80,
  1090. parse: function (string, token, match, _options) {
  1091. switch (token) {
  1092. case 'B':
  1093. case 'BB':
  1094. case 'BBB':
  1095. return match.dayPeriod(string, {
  1096. width: 'abbreviated',
  1097. context: 'formatting'
  1098. }) || match.dayPeriod(string, {
  1099. width: 'narrow',
  1100. context: 'formatting'
  1101. });
  1102. case 'BBBBB':
  1103. return match.dayPeriod(string, {
  1104. width: 'narrow',
  1105. context: 'formatting'
  1106. });
  1107. case 'BBBB':
  1108. default:
  1109. return match.dayPeriod(string, {
  1110. width: 'wide',
  1111. context: 'formatting'
  1112. }) || match.dayPeriod(string, {
  1113. width: 'abbreviated',
  1114. context: 'formatting'
  1115. }) || match.dayPeriod(string, {
  1116. width: 'narrow',
  1117. context: 'formatting'
  1118. });
  1119. }
  1120. },
  1121. set: function (date, _flags, value, _options) {
  1122. date.setUTCHours(dayPeriodEnumToHours(value), 0, 0, 0);
  1123. return date;
  1124. },
  1125. incompatibleTokens: ['a', 'b', 't', 'T']
  1126. },
  1127. // Hour [1-12]
  1128. h: {
  1129. priority: 70,
  1130. parse: function (string, token, match, _options) {
  1131. switch (token) {
  1132. case 'h':
  1133. return parseNumericPattern(numericPatterns.hour12h, string);
  1134. case 'ho':
  1135. return match.ordinalNumber(string, {
  1136. unit: 'hour'
  1137. });
  1138. default:
  1139. return parseNDigits(token.length, string);
  1140. }
  1141. },
  1142. validate: function (_date, value, _options) {
  1143. return value >= 1 && value <= 12;
  1144. },
  1145. set: function (date, _flags, value, _options) {
  1146. var isPM = date.getUTCHours() >= 12;
  1147. if (isPM && value < 12) {
  1148. date.setUTCHours(value + 12, 0, 0, 0);
  1149. } else if (!isPM && value === 12) {
  1150. date.setUTCHours(0, 0, 0, 0);
  1151. } else {
  1152. date.setUTCHours(value, 0, 0, 0);
  1153. }
  1154. return date;
  1155. },
  1156. incompatibleTokens: ['H', 'K', 'k', 't', 'T']
  1157. },
  1158. // Hour [0-23]
  1159. H: {
  1160. priority: 70,
  1161. parse: function (string, token, match, _options) {
  1162. switch (token) {
  1163. case 'H':
  1164. return parseNumericPattern(numericPatterns.hour23h, string);
  1165. case 'Ho':
  1166. return match.ordinalNumber(string, {
  1167. unit: 'hour'
  1168. });
  1169. default:
  1170. return parseNDigits(token.length, string);
  1171. }
  1172. },
  1173. validate: function (_date, value, _options) {
  1174. return value >= 0 && value <= 23;
  1175. },
  1176. set: function (date, _flags, value, _options) {
  1177. date.setUTCHours(value, 0, 0, 0);
  1178. return date;
  1179. },
  1180. incompatibleTokens: ['a', 'b', 'h', 'K', 'k', 't', 'T']
  1181. },
  1182. // Hour [0-11]
  1183. K: {
  1184. priority: 70,
  1185. parse: function (string, token, match, _options) {
  1186. switch (token) {
  1187. case 'K':
  1188. return parseNumericPattern(numericPatterns.hour11h, string);
  1189. case 'Ko':
  1190. return match.ordinalNumber(string, {
  1191. unit: 'hour'
  1192. });
  1193. default:
  1194. return parseNDigits(token.length, string);
  1195. }
  1196. },
  1197. validate: function (_date, value, _options) {
  1198. return value >= 0 && value <= 11;
  1199. },
  1200. set: function (date, _flags, value, _options) {
  1201. var isPM = date.getUTCHours() >= 12;
  1202. if (isPM && value < 12) {
  1203. date.setUTCHours(value + 12, 0, 0, 0);
  1204. } else {
  1205. date.setUTCHours(value, 0, 0, 0);
  1206. }
  1207. return date;
  1208. },
  1209. incompatibleTokens: ['a', 'b', 'h', 'H', 'k', 't', 'T']
  1210. },
  1211. // Hour [1-24]
  1212. k: {
  1213. priority: 70,
  1214. parse: function (string, token, match, _options) {
  1215. switch (token) {
  1216. case 'k':
  1217. return parseNumericPattern(numericPatterns.hour24h, string);
  1218. case 'ko':
  1219. return match.ordinalNumber(string, {
  1220. unit: 'hour'
  1221. });
  1222. default:
  1223. return parseNDigits(token.length, string);
  1224. }
  1225. },
  1226. validate: function (_date, value, _options) {
  1227. return value >= 1 && value <= 24;
  1228. },
  1229. set: function (date, _flags, value, _options) {
  1230. var hours = value <= 24 ? value % 24 : value;
  1231. date.setUTCHours(hours, 0, 0, 0);
  1232. return date;
  1233. },
  1234. incompatibleTokens: ['a', 'b', 'h', 'H', 'K', 't', 'T']
  1235. },
  1236. // Minute
  1237. m: {
  1238. priority: 60,
  1239. parse: function (string, token, match, _options) {
  1240. switch (token) {
  1241. case 'm':
  1242. return parseNumericPattern(numericPatterns.minute, string);
  1243. case 'mo':
  1244. return match.ordinalNumber(string, {
  1245. unit: 'minute'
  1246. });
  1247. default:
  1248. return parseNDigits(token.length, string);
  1249. }
  1250. },
  1251. validate: function (_date, value, _options) {
  1252. return value >= 0 && value <= 59;
  1253. },
  1254. set: function (date, _flags, value, _options) {
  1255. date.setUTCMinutes(value, 0, 0);
  1256. return date;
  1257. },
  1258. incompatibleTokens: ['t', 'T']
  1259. },
  1260. // Second
  1261. s: {
  1262. priority: 50,
  1263. parse: function (string, token, match, _options) {
  1264. switch (token) {
  1265. case 's':
  1266. return parseNumericPattern(numericPatterns.second, string);
  1267. case 'so':
  1268. return match.ordinalNumber(string, {
  1269. unit: 'second'
  1270. });
  1271. default:
  1272. return parseNDigits(token.length, string);
  1273. }
  1274. },
  1275. validate: function (_date, value, _options) {
  1276. return value >= 0 && value <= 59;
  1277. },
  1278. set: function (date, _flags, value, _options) {
  1279. date.setUTCSeconds(value, 0);
  1280. return date;
  1281. },
  1282. incompatibleTokens: ['t', 'T']
  1283. },
  1284. // Fraction of second
  1285. S: {
  1286. priority: 30,
  1287. parse: function (string, token, _match, _options) {
  1288. var valueCallback = function (value) {
  1289. return Math.floor(value * Math.pow(10, -token.length + 3));
  1290. };
  1291. return parseNDigits(token.length, string, valueCallback);
  1292. },
  1293. set: function (date, _flags, value, _options) {
  1294. date.setUTCMilliseconds(value);
  1295. return date;
  1296. },
  1297. incompatibleTokens: ['t', 'T']
  1298. },
  1299. // Timezone (ISO-8601. +00:00 is `'Z'`)
  1300. X: {
  1301. priority: 10,
  1302. parse: function (string, token, _match, _options) {
  1303. switch (token) {
  1304. case 'X':
  1305. return parseTimezonePattern(timezonePatterns.basicOptionalMinutes, string);
  1306. case 'XX':
  1307. return parseTimezonePattern(timezonePatterns.basic, string);
  1308. case 'XXXX':
  1309. return parseTimezonePattern(timezonePatterns.basicOptionalSeconds, string);
  1310. case 'XXXXX':
  1311. return parseTimezonePattern(timezonePatterns.extendedOptionalSeconds, string);
  1312. case 'XXX':
  1313. default:
  1314. return parseTimezonePattern(timezonePatterns.extended, string);
  1315. }
  1316. },
  1317. set: function (date, flags, value, _options) {
  1318. if (flags.timestampIsSet) {
  1319. return date;
  1320. }
  1321. return new Date(date.getTime() - value);
  1322. },
  1323. incompatibleTokens: ['t', 'T', 'x']
  1324. },
  1325. // Timezone (ISO-8601)
  1326. x: {
  1327. priority: 10,
  1328. parse: function (string, token, _match, _options) {
  1329. switch (token) {
  1330. case 'x':
  1331. return parseTimezonePattern(timezonePatterns.basicOptionalMinutes, string);
  1332. case 'xx':
  1333. return parseTimezonePattern(timezonePatterns.basic, string);
  1334. case 'xxxx':
  1335. return parseTimezonePattern(timezonePatterns.basicOptionalSeconds, string);
  1336. case 'xxxxx':
  1337. return parseTimezonePattern(timezonePatterns.extendedOptionalSeconds, string);
  1338. case 'xxx':
  1339. default:
  1340. return parseTimezonePattern(timezonePatterns.extended, string);
  1341. }
  1342. },
  1343. set: function (date, flags, value, _options) {
  1344. if (flags.timestampIsSet) {
  1345. return date;
  1346. }
  1347. return new Date(date.getTime() - value);
  1348. },
  1349. incompatibleTokens: ['t', 'T', 'X']
  1350. },
  1351. // Seconds timestamp
  1352. t: {
  1353. priority: 40,
  1354. parse: function (string, _token, _match, _options) {
  1355. return parseAnyDigitsSigned(string);
  1356. },
  1357. set: function (_date, _flags, value, _options) {
  1358. return [new Date(value * 1000), {
  1359. timestampIsSet: true
  1360. }];
  1361. },
  1362. incompatibleTokens: '*'
  1363. },
  1364. // Milliseconds timestamp
  1365. T: {
  1366. priority: 20,
  1367. parse: function (string, _token, _match, _options) {
  1368. return parseAnyDigitsSigned(string);
  1369. },
  1370. set: function (_date, _flags, value, _options) {
  1371. return [new Date(value), {
  1372. timestampIsSet: true
  1373. }];
  1374. },
  1375. incompatibleTokens: '*'
  1376. }
  1377. };
  1378. var _default = parsers;
  1379. exports.default = _default;
  1380. module.exports = exports.default;