Skip to content

Commit 6cfa5f2

Browse files
committed
Throw an error when the format can't represent the given date in formatRfc7231
1 parent 8d8e56d commit 6cfa5f2

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/datetime/formatRfc7231.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,39 @@ test("fractional seconds", () => {
3232
formatRfc7231(Temporal.Instant.from("1968-06-07T01:23:45.123456789Z")),
3333
).toEqual("Fri, 07 Jun 1968 01:23:45 GMT");
3434
});
35+
36+
test("valid range of year", () => {
37+
expect(formatRfc7231(Temporal.Instant.from("0000-01-01T00:00:00Z"))).toEqual(
38+
"Sat, 01 Jan 0000 00:00:00 GMT",
39+
);
40+
expect(
41+
formatRfc7231(Temporal.ZonedDateTime.from("0000-01-01T00:00:00Z[UTC]")),
42+
).toEqual("Sat, 01 Jan 0000 00:00:00 GMT");
43+
expect(formatRfc7231(Temporal.Instant.from("9999-12-31T00:00:00Z"))).toEqual(
44+
"Fri, 31 Dec 9999 00:00:00 GMT",
45+
);
46+
expect(
47+
formatRfc7231(Temporal.ZonedDateTime.from("9999-12-31T00:00:00Z[UTC]")),
48+
).toEqual("Fri, 31 Dec 9999 00:00:00 GMT");
49+
});
50+
51+
test("invalid range of year", () => {
52+
expect(() => {
53+
formatRfc7231(Temporal.Instant.from("-000001-12-31T23:59:59Z"));
54+
}).toThrow();
55+
expect(() => {
56+
formatRfc7231(Temporal.ZonedDateTime.from("-000001-12-31T23:59:59Z[UTC]"));
57+
}).toThrow();
58+
expect(() => {
59+
formatRfc7231(Temporal.Instant.from("-100000-01-01T00:00:00Z"));
60+
}).toThrow();
61+
expect(() => {
62+
formatRfc7231(Temporal.ZonedDateTime.from("-100000-01-01T00:00:00Z[UTC]"));
63+
}).toThrow();
64+
expect(() => {
65+
formatRfc7231(Temporal.Instant.from("+010000-01-01T00:00:00Z"));
66+
}).toThrow();
67+
expect(() => {
68+
formatRfc7231(Temporal.ZonedDateTime.from("+010000-01-01T00:00:00Z[UTC]"));
69+
}).toThrow();
70+
});

src/datetime/formatRfc7231.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export function formatRfc7231(
1818
// timeZone: 'UTC', calendar: 'iso8601'
1919
const zdt = (isInstant(dt) ? dt : dt.toInstant()).toZonedDateTimeISO("UTC");
2020
const dayOfWeek = getDayOfWeekAbbreviationFromNumber(zdt.dayOfWeek);
21+
if (zdt.year < 0 || zdt.year > 9999) {
22+
throw new Error(`RFC 7231 format can't represent year ${zdt.year}`);
23+
}
2124
const year = padLeadingZeros(zdt.year, 4);
2225
const day = padLeadingZeros(zdt.day, 2);
2326
const month = getMonthAbbreviationFromNumber(zdt.month);

0 commit comments

Comments
 (0)