/** * 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 default function tzIntlTimeZoneName(length, date, options) { var dtf = getDTF(length, options.timeZone, options.locale) return dtf.formatToParts ? partsTimeZone(dtf, date) : hackyTimeZone(dtf, date) } function partsTimeZone(dtf, date) { var formatted = dtf.formatToParts(date) for (var i = formatted.length - 1; i >= 0; --i) { if (formatted[i].type === 'timeZoneName') { return formatted[i].value } } } function hackyTimeZone(dtf, date) { var formatted = dtf.format(date).replace(/\u200E/g, '') var 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, timeZone, locale) { if (locale && !locale.code) { throw new Error( "date-fns-tz error: Please set a language code on the locale object imported from date-fns, e.g. `locale.code = 'en-US'`" ) } return new Intl.DateTimeFormat(locale ? [locale.code, 'en-US'] : undefined, { timeZone: timeZone, timeZoneName: length, }) }