1
- import { getTypeName , isPlainMonthDay } from "../type-utils.js" ;
1
+ import {
2
+ getTypeName ,
3
+ isPlainMonthDay ,
4
+ isZonedDateTime ,
5
+ } from "../type-utils.js" ;
2
6
import type { Temporal } from "../types.js" ;
3
7
import { padLeadingZeros } from "./_padLeadingZeros.js" ;
4
8
import { secondsToHms } from "./_secondsToHms.js" ;
@@ -227,6 +231,15 @@ function offset(dateTime: DateTime, token: string) {
227
231
throw new Error ( `Invalid token: ${ token } ` ) ;
228
232
}
229
233
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
+
230
243
function formatToken (
231
244
dateTime : DateTime ,
232
245
token : string ,
@@ -256,6 +269,9 @@ function formatToken(
256
269
if ( / ^ ( x { 1 , 5 } | X { 1 , 5 } ) $ / . test ( token ) ) {
257
270
return offset ( dateTime , token ) ;
258
271
}
272
+ if ( token === "VV" ) {
273
+ return timeZoneId ( dateTime ) ;
274
+ }
259
275
throw new Error ( `Invalid token: ${ token } ` ) ;
260
276
}
261
277
@@ -322,6 +338,7 @@ export interface FormatWithoutLocaleOptions {
322
338
* | | XXX | -08:00, +05:30, Z |
323
339
* | | XXXX | -0800, +0530, Z, +123456 |
324
340
* | | XXXXX | -08:00, +05:30, Z, +12:34:56 |
341
+ * | time zone ID | VV | Europe/London, Etc/GMT+1 |
325
342
*
326
343
* @param dateTime Temporal object
327
344
* @param format pattern string
@@ -343,7 +360,7 @@ export function formatWithoutLocale(
343
360
"Unbalanced single quotes. Use single quotes for escaping and two single quotes to represent actual single quote." ,
344
361
) ;
345
362
}
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 + | V V | x + | X + / g;
347
364
return format . replace ( regex , ( match ) => {
348
365
if ( match === `''` ) {
349
366
return `'` ;
0 commit comments