(text: string)
| 553 | |
| 554 | /** Parses `[-]YYYY-MM-DD`, with as many year digits as the caller likes. */ |
| 555 | const parseDate = (text: string): number => { |
| 556 | const yearStart = text.charCodeAt(0) === 45 ? 1 : 0 |
| 557 | const yearEnd = text.length - 6 |
| 558 | if (yearEnd - yearStart < 4 || text.charCodeAt(yearEnd) !== 45 || text.charCodeAt(yearEnd + 3) !== 45) { |
| 559 | return fail(`Expected a YYYY-MM-DD date, received "${text}"`) |
| 560 | } |
| 561 | let year = 0 |
| 562 | for (let index = yearStart; index < yearEnd; index++) { |
| 563 | const digit = text.charCodeAt(index) - 48 |
| 564 | if (digit < 0 || digit > 9) { |
| 565 | return fail(`Expected a YYYY-MM-DD date, received "${text}"`) |
| 566 | } |
| 567 | year = year * 10 + digit |
| 568 | } |
| 569 | const month = twoDigitsAt(text, yearEnd + 1) |
| 570 | const day = twoDigitsAt(text, yearEnd + 4) |
| 571 | if (month === -1 || day === -1) { |
| 572 | return fail(`Expected a YYYY-MM-DD date, received "${text}"`) |
| 573 | } |
| 574 | if (yearStart === 1) year = -year |
| 575 | // Enough digits and the year is Infinity, which `daysFromCivil` turns into a |
| 576 | // NaN the range check below would let through as a zero. |
| 577 | if (!Number.isFinite(year)) { |
| 578 | return fail(`date out of range: "${text}"`) |
| 579 | } |
| 580 | const monthLength = month === 2 && isLeapYear(year) ? 29 : monthLengths[month] |
| 581 | if (month < 1 || month > 12 || day < 1 || day > monthLength) { |
| 582 | return fail(`Invalid date "${text}"`) |
| 583 | } |
| 584 | return daysFromCivil(year, month, day) |
| 585 | } |
| 586 | |
| 587 | const formatDate = (days: number): string => { |
| 588 | const { day, month, year } = civilFromDays(days) |
no test coverage detected