import { formatInTimeZone } from "date-fns-tz";

export const utcDateToLocal = (datetime: string | Date) => {
    const userTZ = Intl.DateTimeFormat().resolvedOptions().timeZone;
    const localTime = formatInTimeZone(datetime, userTZ, "yyyy-MM-dd HH:mm:ss");
    return new Date(localTime);
};

export const localDateTimeToString = (datetime: string | Date) => {
    const date = new Date(datetime);
    return date.toLocaleString("ru").substring(0, 17);
};

export const utcDateTimeToLocalString = (
    datetime: string | Date,
    withSeconds: boolean = false
) => {
    const res = localDateTimeToString(utcDateToLocal(datetime));
    return withSeconds ? res : res.substring(0, 17);
};
