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>
30 lines
884 B
TypeScript
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;
|
|
};
|