MCPcopy Create free account
hub / github.com/Effect-TS/effect / parseDate

Function parseDate

packages/sql/pg/src/PgTypes.ts:555–585  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

553
554/** Parses `[-]YYYY-MM-DD`, with as many year digits as the caller likes. */
555const 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
587const formatDate = (days: number): string => {
588 const { day, month, year } = civilFromDays(days)

Callers 1

dateDaysFunction · 0.70

Calls 4

twoDigitsAtFunction · 0.85
isLeapYearFunction · 0.85
daysFromCivilFunction · 0.85
failFunction · 0.70

Tested by

no test coverage detected