Skip to content

Commit 07859f0

Browse files
committed
Add formatRfc7231 function
1 parent fda9394 commit 07859f0

6 files changed

+90
-1
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
- `startOfYear`, `startOfMonth`, `startOfDay`, `startOfHour`, `startOfMinute`, `startOfSecond`, `endOfYear`, `endOfMonth`, `endOfDay`, `endOfHour`, `endOfMinute`, `endOfSecond`, `fromRfc2822`, `fromRfc7231` functions
12+
- `startOfYear`, `startOfMonth`, `startOfDay`, `startOfHour`, `startOfMinute`, `startOfSecond`, `endOfYear`, `endOfMonth`, `endOfDay`, `endOfHour`, `endOfMinute`, `endOfSecond`, `fromRfc2822`, `fromRfc7231`, `formatRfc7231` functions
1313

1414
### Changed
1515

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { expect, test } from "vitest";
2+
3+
import { getMonthAbbreviationFromNumber } from "./_getMonthAbbreviationFromNumber.js";
4+
5+
test("getMonthAbbreviationFromNumber", () => {
6+
expect(getMonthAbbreviationFromNumber(1)).toEqual("Jan");
7+
expect(getMonthAbbreviationFromNumber(2)).toEqual("Feb");
8+
expect(getMonthAbbreviationFromNumber(3)).toEqual("Mar");
9+
expect(getMonthAbbreviationFromNumber(4)).toEqual("Apr");
10+
expect(getMonthAbbreviationFromNumber(5)).toEqual("May");
11+
expect(getMonthAbbreviationFromNumber(6)).toEqual("Jun");
12+
expect(getMonthAbbreviationFromNumber(7)).toEqual("Jul");
13+
expect(getMonthAbbreviationFromNumber(8)).toEqual("Aug");
14+
expect(getMonthAbbreviationFromNumber(9)).toEqual("Sep");
15+
expect(getMonthAbbreviationFromNumber(10)).toEqual("Oct");
16+
expect(getMonthAbbreviationFromNumber(11)).toEqual("Nov");
17+
expect(getMonthAbbreviationFromNumber(12)).toEqual("Dec");
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export function getMonthAbbreviationFromNumber(num: number): string {
2+
const name = [
3+
"Jan",
4+
"Feb",
5+
"Mar",
6+
"Apr",
7+
"May",
8+
"Jun",
9+
"Jul",
10+
"Aug",
11+
"Sep",
12+
"Oct",
13+
"Nov",
14+
"Dec",
15+
][num - 1];
16+
if (name === undefined) {
17+
throw new Error(`Invalid month number: ${num}`);
18+
}
19+
return name;
20+
}

src/datetime/formatRfc7231.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Temporal } from "@js-temporal/polyfill";
2+
import { expect, test } from "vitest";
3+
4+
import { formatRfc7231 } from "./formatRfc7231.js";
5+
6+
test("ZonedDateTime", () => {
7+
expect(
8+
formatRfc7231(
9+
Temporal.ZonedDateTime.from("2024-06-07T10:23:45+09:00[Asia/Tokyo]"),
10+
),
11+
).toEqual("Fri, 07 Jun 2024 01:23:45 GMT");
12+
expect(
13+
formatRfc7231(
14+
Temporal.ZonedDateTime.from(
15+
"2024-06-07T10:23:45+09:00[Asia/Tokyo][u-ca=hebrew]",
16+
),
17+
),
18+
).toEqual("Fri, 07 Jun 2024 01:23:45 GMT");
19+
});
20+
21+
test("Instant", () => {
22+
expect(formatRfc7231(Temporal.Instant.from("2024-06-07T01:23:45Z"))).toEqual(
23+
"Fri, 07 Jun 2024 01:23:45 GMT",
24+
);
25+
});

src/datetime/formatRfc7231.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { isInstant } from "../type-utils.js";
2+
import type { Temporal } from "../types.js";
3+
import { getDayOfWeekAbbreviationFromNumber } from "./_getDayOfWeekAbbreviationFromNumber.js";
4+
import { getMonthAbbreviationFromNumber } from "./_getMonthAbbreviationFromNumber.js";
5+
6+
/**
7+
* Returns a string in RFC 7231's format which represents an exact time of given temporal object.
8+
*
9+
* @param dt temporal object which includes exact time info (`Instant` and `ZonedDateTime`)
10+
* @returns a string formatted according to RFC 7231
11+
*/
12+
export function formatRfc7231(
13+
dt: Temporal.Instant | Temporal.ZonedDateTime,
14+
): string {
15+
// timeZone: 'UTC', calendar: 'iso8601'
16+
const zdt = (isInstant(dt) ? dt : dt.toInstant()).toZonedDateTimeISO("UTC");
17+
const dayOfWeek = getDayOfWeekAbbreviationFromNumber(zdt.dayOfWeek);
18+
const year = zdt.year.toString().padStart(4, "0");
19+
const day = zdt.day.toString().padStart(2, "0");
20+
const month = getMonthAbbreviationFromNumber(zdt.month);
21+
const hour = zdt.hour.toString().padStart(2, "0");
22+
const minute = zdt.minute.toString().padStart(2, "0");
23+
const second = zdt.second.toString().padStart(2, "0");
24+
return `${dayOfWeek}, ${day} ${month} ${year} ${hour}:${minute}:${second} GMT`;
25+
}

src/datetime/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export { endOfMinute } from "./endOfMinute.js";
1515
export { endOfMonth } from "./endOfMonth.js";
1616
export { endOfSecond } from "./endOfSecond.js";
1717
export { endOfYear } from "./endOfYear.js";
18+
export { formatRfc7231 } from "./formatRfc7231.js";
1819
export {
1920
formatWithoutLocale,
2021
type FormatWithoutLocaleOptions,

0 commit comments

Comments
 (0)