|
| 1 | +import { |
| 2 | + isInstantConstructor, |
| 3 | + isPlainDateTimeConstructor, |
| 4 | +} from "../type-utils.js"; |
| 5 | +import type { Temporal } from "../types.js"; |
| 6 | + |
| 7 | +// spec: https://datatracker.ietf.org/doc/html/rfc2822#section-3.3 https://datatracker.ietf.org/doc/html/rfc2822#section-4.3 |
| 8 | + |
| 9 | +function removeComment(str: string) { |
| 10 | + const r = /(?<!\\)[()]/g; |
| 11 | + let res = ""; |
| 12 | + let commentNestLevel = 0; |
| 13 | + let lastNonCommentStarted = 0; |
| 14 | + for (const m of str.matchAll(r)) { |
| 15 | + if (m[0] === "(") { |
| 16 | + if (commentNestLevel === 0) { |
| 17 | + // comment started |
| 18 | + res += str.slice(lastNonCommentStarted, m.index); |
| 19 | + } |
| 20 | + commentNestLevel++; |
| 21 | + } else { |
| 22 | + commentNestLevel--; |
| 23 | + if (commentNestLevel === 0) { |
| 24 | + // comment ended |
| 25 | + lastNonCommentStarted = m.index + 1; |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + return res; |
| 30 | +} |
| 31 | + |
| 32 | +const dateTimeFormatRegex = |
| 33 | + /^[ \t\r\n]*(?:(Mon|Tue|Wed|Thu|Fri|Sat|Sun),)?[ \t\r\n]*(\d\d)[ \t\r\n]+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ \t\r\n]+(\d\d|\d\d\d\d)[ \t\r\n]+(\d\d):(\d\d)(?::(\d\d))?[ \t\r\n]+([+-]\d{4}|[A-Za-z]+)[ \t\r\n]*$/; |
| 34 | + |
| 35 | +function fullYear(year: string) { |
| 36 | + const yearNum = parseInt(year, 10); |
| 37 | + if (year.length === 4) { |
| 38 | + return yearNum; |
| 39 | + } |
| 40 | + return yearNum >= 50 ? 1900 + yearNum : 2000 + yearNum; |
| 41 | +} |
| 42 | + |
| 43 | +function getDayOfWeek(year: number, month: number, day: number) { |
| 44 | + const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; |
| 45 | + const week = days[new Date(Date.UTC(year, month - 1, day)).getUTCDay()]; |
| 46 | + if (week === undefined) { |
| 47 | + throw new Error("something wrong"); |
| 48 | + } |
| 49 | + return week; |
| 50 | +} |
| 51 | + |
| 52 | +function monthNumber(monthName: string) { |
| 53 | + const monthNum = |
| 54 | + [ |
| 55 | + "Jan", |
| 56 | + "Feb", |
| 57 | + "Mar", |
| 58 | + "Apr", |
| 59 | + "May", |
| 60 | + "Jun", |
| 61 | + "Jul", |
| 62 | + "Aug", |
| 63 | + "Sep", |
| 64 | + "Oct", |
| 65 | + "Nov", |
| 66 | + "Dec", |
| 67 | + ].indexOf(monthName) + 1; |
| 68 | + if (monthNum === 0) { |
| 69 | + throw new Error(`Invalid month name: ${monthName}`); |
| 70 | + } |
| 71 | + return monthNum; |
| 72 | +} |
| 73 | + |
| 74 | +function getOffset(timeZone: string): string { |
| 75 | + if (["UT", "GMT", "z", "Z"].includes(timeZone)) { |
| 76 | + return "+00:00"; |
| 77 | + } |
| 78 | + if (timeZone === "-0000" || /^[A-IK-Za-ik-z]$/.test(timeZone)) { |
| 79 | + // according to the spec, military zone except 'Z' should be considered equivalent to "-0000", |
| 80 | + // which means the date-time contains no information about the local time zone |
| 81 | + throw new Error("No offset info"); |
| 82 | + } |
| 83 | + if (/^[+-]\d{4}$/.test(timeZone)) { |
| 84 | + return `${timeZone.slice(0, 3)}:${timeZone.slice(3)}`; |
| 85 | + } |
| 86 | + const table = Object.assign(Object.create(null) as Record<string, string>, { |
| 87 | + EDT: "-04:00", |
| 88 | + EST: "-05:00", |
| 89 | + CDT: "-05:00", |
| 90 | + CST: "-06:00", |
| 91 | + MDT: "-06:00", |
| 92 | + MST: "-07:00", |
| 93 | + PDT: "-07:00", |
| 94 | + PST: "-08:00", |
| 95 | + }); |
| 96 | + if (table[timeZone] !== undefined) { |
| 97 | + return table[timeZone]; |
| 98 | + } |
| 99 | + throw new Error("Unknown time zone"); |
| 100 | +} |
| 101 | + |
| 102 | +function parse(date: string) { |
| 103 | + const result = dateTimeFormatRegex.exec(date); |
| 104 | + if (result === null) { |
| 105 | + throw new Error(`Invalid date and time format: ${date}`); |
| 106 | + } |
| 107 | + const [ |
| 108 | + , |
| 109 | + dayOfWeek, |
| 110 | + day, |
| 111 | + monthName, |
| 112 | + year, |
| 113 | + hour, |
| 114 | + minute, |
| 115 | + second = "00", |
| 116 | + timeZone, |
| 117 | + ] = result; |
| 118 | + if ( |
| 119 | + day === undefined || |
| 120 | + monthName === undefined || |
| 121 | + year === undefined || |
| 122 | + hour === undefined || |
| 123 | + minute === undefined || |
| 124 | + timeZone === undefined |
| 125 | + ) { |
| 126 | + throw new Error("something wrong"); |
| 127 | + } |
| 128 | + return { |
| 129 | + year: fullYear(year), |
| 130 | + month: monthNumber(monthName), |
| 131 | + day: parseInt(day, 10), |
| 132 | + hour: parseInt(hour, 10), |
| 133 | + minute: parseInt(minute, 10), |
| 134 | + second: parseInt(second), |
| 135 | + dayOfWeek, |
| 136 | + timeZone, |
| 137 | + }; |
| 138 | +} |
| 139 | + |
| 140 | +function formatInstantIso( |
| 141 | + year: number, |
| 142 | + month: number, |
| 143 | + day: number, |
| 144 | + hour: number, |
| 145 | + minute: number, |
| 146 | + second: number, |
| 147 | + offsetString: string, |
| 148 | +) { |
| 149 | + const yearStr = year.toString(); |
| 150 | + const monthStr = month.toString().padStart(2, "0"); |
| 151 | + const dayStr = day.toString().padStart(2, "0"); |
| 152 | + const hourStr = hour.toString().padStart(2, "0"); |
| 153 | + const minuteStr = minute.toString().padStart(2, "0"); |
| 154 | + const secondStr = second.toString().padStart(2, "0"); |
| 155 | + return `${yearStr}-${monthStr}-${dayStr}T${hourStr}:${minuteStr}:${secondStr}${offsetString}`; |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * Creates Temporal object from datetime string in RFC 2822's format. |
| 160 | + * |
| 161 | + * @param date datetime string in RFC 2822's format |
| 162 | + * @param TemporalClass Temporal class (such as `Temporal.PlainDateTime` or `Temporal.Instant`) which will be returned |
| 163 | + * @returns an instance of Temporal class specified in `TemporalClass` argument |
| 164 | + */ |
| 165 | +export function fromRfc2822< |
| 166 | + TemporalClassType extends |
| 167 | + | typeof Temporal.Instant |
| 168 | + | typeof Temporal.ZonedDateTime |
| 169 | + | typeof Temporal.PlainDateTime, |
| 170 | +>( |
| 171 | + date: string, |
| 172 | + TemporalClass: TemporalClassType, |
| 173 | +): InstanceType<TemporalClassType> { |
| 174 | + const dateWithoutComment = date.includes("(") ? removeComment(date) : date; |
| 175 | + |
| 176 | + const { year, month, day, hour, minute, second, dayOfWeek, timeZone } = |
| 177 | + parse(dateWithoutComment); |
| 178 | + if (dayOfWeek !== undefined && getDayOfWeek(year, month, day) !== dayOfWeek) { |
| 179 | + throw new Error(`Wrong day of week: ${dayOfWeek}`); |
| 180 | + } |
| 181 | + |
| 182 | + if (isPlainDateTimeConstructor(TemporalClass)) { |
| 183 | + return TemporalClass.from({ |
| 184 | + year, |
| 185 | + month, |
| 186 | + day, |
| 187 | + hour, |
| 188 | + minute, |
| 189 | + second, |
| 190 | + calendarId: "iso8601", |
| 191 | + }) as InstanceType<TemporalClassType>; |
| 192 | + } |
| 193 | + |
| 194 | + const offsetIso = getOffset(timeZone); |
| 195 | + if (isInstantConstructor(TemporalClass)) { |
| 196 | + return TemporalClass.from( |
| 197 | + formatInstantIso(year, month, day, hour, minute, second, offsetIso), |
| 198 | + ) as InstanceType<TemporalClassType>; |
| 199 | + } |
| 200 | + return TemporalClass.from({ |
| 201 | + year, |
| 202 | + month, |
| 203 | + day, |
| 204 | + hour, |
| 205 | + minute, |
| 206 | + second, |
| 207 | + calendarId: "iso8601", |
| 208 | + timeZone: offsetIso, |
| 209 | + }) as InstanceType<TemporalClassType>; |
| 210 | +} |
0 commit comments