1 import { format } from '@proton/shared/lib/date-fns-utc';
3 convertUTCDateTimeToZone,
4 convertZonedDateTimeToUTC,
7 } from '@proton/shared/lib/date/timezone';
8 import { dateLocale } from '@proton/shared/lib/i18n';
9 import { SETTINGS_TIME_FORMAT, type UserSettings } from '@proton/shared/lib/interfaces';
12 * Given a date, if we are to display a single time zone offset for the whole day,
13 * we pick the UTC offset at noon. DST changes usually happens at 2:00,
14 * so the offset at noon is more representative of the offset of the day.
16 export const getNoonDateForTimeZoneOffset = ({
25 // Extract year, month and day in the date time zone
26 const dateTimeInDateTzid = convertUTCDateTimeToZone(fromUTCDate(date), dateTzid);
27 // Pick noon of that date
28 const noonDateTimeInDateTzid = {
29 ...dateTimeInDateTzid,
35 // Return date corresponding to the noon above in the target time zone
36 return toUTCDate(convertZonedDateTimeToUTC(noonDateTimeInDateTzid, targetTzid));
39 export const formatShortTime = (utcDate: Date, userSettings: UserSettings) => {
40 const timeString = format(utcDate, 'p', { locale: dateLocale });
41 const is12HourFormat = timeString.includes('AM') || timeString.includes('PM');
44 userSettings.TimeFormat === SETTINGS_TIME_FORMAT.H12 ||
45 (is12HourFormat && userSettings.TimeFormat === SETTINGS_TIME_FORMAT.LOCALE_DEFAULT)
47 if (format(utcDate, 'mm') === '00') {
48 // If it's a full hour, display only the hour with AM/PM in lowercase
49 return format(utcDate, 'ha', { locale: dateLocale }).toLowerCase();
51 // Otherwise, display the hour with minutes and AM/PM in lowercase
52 return format(utcDate, 'h:mma', { locale: dateLocale }).toLowerCase().replace(/\s/g, ''); // strip spaces