Skip to content

Commit 3a651ce

Browse files
committed
Support formatting time zone ID in formatWithoutLocale
1 parent db604fe commit 3a651ce

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/datetime/formatWithoutLocale.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,25 @@ test("offset", () => {
255255
}
256256
});
257257

258+
test("time zone ID", () => {
259+
const dt = Temporal.ZonedDateTime.from(target);
260+
expect(formatWithoutLocale(dt, "VV")).toEqual("Europe/London");
261+
262+
const ng = [
263+
Temporal.PlainDate.from(target),
264+
Temporal.PlainTime.from(target),
265+
Temporal.PlainDateTime.from(target),
266+
Temporal.PlainMonthDay.from(target),
267+
Temporal.PlainYearMonth.from(target),
268+
];
269+
270+
for (const dt of ng) {
271+
expect(() => {
272+
formatWithoutLocale(dt, "VV");
273+
}).toThrowError();
274+
}
275+
});
276+
258277
test("ZonedDateTime with non-ISO calendar", () => {
259278
expect(() => {
260279
formatWithoutLocale(

src/datetime/formatWithoutLocale.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { getTypeName, isPlainMonthDay } from "../type-utils.js";
1+
import {
2+
getTypeName,
3+
isPlainMonthDay,
4+
isZonedDateTime,
5+
} from "../type-utils.js";
26
import type { Temporal } from "../types.js";
37
import { padLeadingZeros } from "./_padLeadingZeros.js";
48
import { secondsToHms } from "./_secondsToHms.js";
@@ -227,6 +231,15 @@ function offset(dateTime: DateTime, token: string) {
227231
throw new Error(`Invalid token: ${token}`);
228232
}
229233

234+
function timeZoneId(dateTime: DateTime) {
235+
if (!isZonedDateTime(dateTime)) {
236+
throw new Error(
237+
`${getTypeName(dateTime)} doesn't have timezone and offset info`,
238+
);
239+
}
240+
return dateTime.timeZoneId;
241+
}
242+
230243
function formatToken(
231244
dateTime: DateTime,
232245
token: string,
@@ -256,6 +269,9 @@ function formatToken(
256269
if (/^(x{1,5}|X{1,5})$/.test(token)) {
257270
return offset(dateTime, token);
258271
}
272+
if (token === "VV") {
273+
return timeZoneId(dateTime);
274+
}
259275
throw new Error(`Invalid token: ${token}`);
260276
}
261277

@@ -322,6 +338,7 @@ export interface FormatWithoutLocaleOptions {
322338
* | | XXX | -08:00, +05:30, Z |
323339
* | | XXXX | -0800, +0530, Z, +123456 |
324340
* | | XXXXX | -08:00, +05:30, Z, +12:34:56 |
341+
* | time zone ID | VV | Europe/London, Etc/GMT+1 |
325342
*
326343
* @param dateTime Temporal object
327344
* @param format pattern string
@@ -343,7 +360,7 @@ export function formatWithoutLocale(
343360
"Unbalanced single quotes. Use single quotes for escaping and two single quotes to represent actual single quote.",
344361
);
345362
}
346-
const regex = /''|'(''|[^'])+'|y+|M+|d+|h+|H+|m+|s+|S+|x+|X+/g;
363+
const regex = /''|'(''|[^'])+'|y+|M+|d+|h+|H+|m+|s+|S+|VV|x+|X+/g;
347364
return format.replace(regex, (match) => {
348365
if (match === `''`) {
349366
return `'`;

0 commit comments

Comments
 (0)