umami/src/components/hooks/useTimezone.ts
Mike Cao edb7022c55 Merge branch 'dev' into jajaja
# Conflicts:
#	package.json
#	src/components/hooks/useTimezone.ts
#	yarn.lock
2025-02-19 22:55:40 -08:00

38 lines
1.1 KiB
TypeScript

import { setItem } from '@/lib/storage';
import { TIMEZONE_CONFIG } from '@/lib/constants';
import { formatInTimeZone, zonedTimeToUtc, utcToZonedTime } from 'date-fns-tz';
import { useApp, setTimezone } from '@/store/app';
import { useLocale } from './useLocale';
const selector = (state: { timezone: string }) => state.timezone;
export function useTimezone() {
const timezone = useApp(selector);
const { dateLocale } = useLocale();
const saveTimezone = (value: string) => {
setItem(TIMEZONE_CONFIG, value);
setTimezone(value);
};
const formatTimezoneDate = (date: string, pattern: string) => {
return formatInTimeZone(
/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]{3})?Z$/.test(date)
? date
: date.split(' ').join('T') + 'Z',
timezone,
pattern,
{ locale: dateLocale },
);
};
const toUtc = (date: Date | string | number) => {
return zonedTimeToUtc(date, timezone);
};
const fromUtc = (date: Date | string | number) => {
return utcToZonedTime(date, timezone);
};
return { timezone, saveTimezone, formatTimezoneDate, toUtc, fromUtc };
}