Files
twenty_crm/packages/twenty-front/src/modules/localization/utils/formatTimeZoneLabel.ts
Lucas Bordeau ccf4d1eeec Date formatting per workspace member settings (#6408)
Implement date formatting per workspace member settings

We'll need another round to maybe initialize all workspaces on the
default settings.

For now the default behavior is to take system settings if nothing is
found in DB.

---------

Co-authored-by: Weiko <corentin@twenty.com>
2024-07-30 14:52:10 +02:00

30 lines
884 B
TypeScript

import { formatInTimeZone } from 'date-fns-tz';
import defaultLocale from 'date-fns/locale/en-US';
/**
* Formats a IANA time zone to a select option label.
* @param ianaTimeZone IANA time zone
* @returns Formatted label
* @example 'Europe/Paris' => '(GMT+01:00) Central European Time - Paris'
*/
export const formatTimeZoneLabel = (ianaTimeZone: string) => {
const timeZoneWithGmtOffset = formatInTimeZone(
Date.now(),
ianaTimeZone,
`(OOOO) zzzz`,
{ locale: defaultLocale },
);
const ianaTimeZoneParts = ianaTimeZone.split('/');
const location =
ianaTimeZoneParts.length > 1
? ianaTimeZoneParts.slice(-1)[0].replaceAll('_', ' ')
: undefined;
const timeZoneLabel =
!location || timeZoneWithGmtOffset.includes(location)
? timeZoneWithGmtOffset
: [timeZoneWithGmtOffset, location].join(' - ');
return timeZoneLabel;
};