|
| 1 | +import { |
| 2 | + isInstantConstructor, |
| 3 | + isZonedDateTimeConstructor, |
| 4 | +} from "../type-utils.js"; |
| 5 | +import type { Temporal } from "../types.js"; |
| 6 | +import { formatExactTimeIso } from "./_formatExactTimeIso.js"; |
| 7 | +import { getDayOfWeekFromYmd } from "./_getDayOfWeekFromYmd.js"; |
| 8 | +import { getDayOfWeekNumberFromAbbreviation } from "./_getDayOfWeekNumberFromAbbreviation.js"; |
| 9 | +import { getMonthNumberFromAbbreviation } from "./_getMonthNumberFromAbbreviation.js"; |
| 10 | + |
| 11 | +const regex = |
| 12 | + /^([A-Za-z]{3}), (\d\d) ([A-Za-z]{3}) (\d{4}) (\d\d):(\d\d):(\d\d) GMT$/; |
| 13 | + |
| 14 | +function parse(date: string) { |
| 15 | + const result = regex.exec(date); |
| 16 | + if (result === null) { |
| 17 | + throw new Error("Invalid format"); |
| 18 | + } |
| 19 | + const [, dayOfWeek, day, monthName, year, hour, minute, second] = result; |
| 20 | + if ( |
| 21 | + dayOfWeek === undefined || |
| 22 | + day === undefined || |
| 23 | + monthName === undefined || |
| 24 | + year === undefined || |
| 25 | + hour === undefined || |
| 26 | + minute === undefined || |
| 27 | + second === undefined |
| 28 | + ) { |
| 29 | + throw new Error("something wrong"); |
| 30 | + } |
| 31 | + const y = parseInt(year); |
| 32 | + const m = getMonthNumberFromAbbreviation(monthName); |
| 33 | + const d = parseInt(day); |
| 34 | + const weekNum = getDayOfWeekNumberFromAbbreviation(dayOfWeek); |
| 35 | + if (getDayOfWeekFromYmd(y, m, d) !== weekNum) { |
| 36 | + throw new Error(`Wrong day of week: ${dayOfWeek}`); |
| 37 | + } |
| 38 | + return [y, m, d, parseInt(hour), parseInt(minute), parseInt(second)] as const; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Creates Temporal object from datetime string in RFC 7231's format (HTTP date format) |
| 43 | + * such as `Mon, 01 Jan 2024 01:23:45 GMT`. |
| 44 | + * This function doesn't support obsoleted formats. |
| 45 | + * |
| 46 | + * @param date datetime string in RFC 7231's format |
| 47 | + * @param TemporalClass Temporal class (such as `Temporal.PlainDateTime` or `Temporal.Instant`) which will be returned |
| 48 | + * @returns an instance of Temporal class specified in `TemporalClass` argument |
| 49 | + */ |
| 50 | +export function fromRfc7231< |
| 51 | + TemporalClassType extends |
| 52 | + | typeof Temporal.Instant |
| 53 | + | typeof Temporal.ZonedDateTime |
| 54 | + | typeof Temporal.PlainDateTime, |
| 55 | +>( |
| 56 | + date: string, |
| 57 | + TemporalClass: TemporalClassType, |
| 58 | +): InstanceType<TemporalClassType> { |
| 59 | + const result = parse(date); |
| 60 | + const [year, month, day, hour, minute, second] = result; |
| 61 | + if (isInstantConstructor(TemporalClass)) { |
| 62 | + return TemporalClass.from( |
| 63 | + formatExactTimeIso(year, month, day, hour, minute, second, 0, "Z"), |
| 64 | + ) as InstanceType<TemporalClassType>; |
| 65 | + } |
| 66 | + if (isZonedDateTimeConstructor(TemporalClass)) { |
| 67 | + return TemporalClass.from({ |
| 68 | + year, |
| 69 | + month, |
| 70 | + day, |
| 71 | + hour, |
| 72 | + minute, |
| 73 | + second, |
| 74 | + calendarId: "iso8601", |
| 75 | + timeZone: "UTC", |
| 76 | + }) as InstanceType<TemporalClassType>; |
| 77 | + } |
| 78 | + return TemporalClass.from({ |
| 79 | + year, |
| 80 | + month, |
| 81 | + day, |
| 82 | + hour, |
| 83 | + minute, |
| 84 | + second, |
| 85 | + calendarId: "iso8601", |
| 86 | + }) as InstanceType<TemporalClassType>; |
| 87 | +} |
0 commit comments