/* eslint-disable eslint-comments/no-unlimited-disable */ /* eslint-disable */ // @ts-nocheck import { getDefaultOptions, type Locale } from '../../../date-fns/index.ts'; import type { FormatOptionsWithTZ } from '../../index.ts'; /** * Returns the formatted time zone name of the provided `timeZone` or the current * system time zone if omitted, accounting for DST according to the UTC value of * the date. */ export function tzIntlTimeZoneName( length: Intl.DateTimeFormatOptions['timeZoneName'], date: Date, options: FormatOptionsWithTZ, ): string | undefined { const defaultOptions = getDefaultOptions(); const dtf = getDTF(length, options.timeZone, options.locale ?? defaultOptions.locale); return 'formatToParts' in dtf ? partsTimeZone(dtf, date) : hackyTimeZone(dtf, date); } function partsTimeZone(dtf: Intl.DateTimeFormat, date: Date): string | undefined { const formatted = dtf.formatToParts(date); for (let i = formatted.length - 1; i >= 0; --i) { if (formatted[i].type === 'timeZoneName') { return formatted[i].value; } } return undefined; } function hackyTimeZone(dtf: Intl.DateTimeFormat, date: Date) { const formatted = dtf.format(date).replace(/\u200E/g, ''); const tzNameMatch = / [\w-+ ]+$/.exec(formatted); return tzNameMatch ? tzNameMatch[0].substr(1) : ''; } // If a locale has been provided `en-US` is used as a fallback in case it is an // invalid locale, otherwise the locale is left undefined to use the system locale. function getDTF( length: Intl.DateTimeFormatOptions['timeZoneName'], timeZone: string | undefined, locale: Pick | undefined, ) { return new Intl.DateTimeFormat(locale ? [locale.code, 'en-US'] : undefined, { timeZone: timeZone, timeZoneName: length, }); } /* eslint-enable */