Files
frigate/web/src/hooks/use-date-utils.ts
T

119 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-03-03 17:19:02 -07:00
import { FrigateConfig } from "@/types/frigateConfig";
2024-02-25 12:04:44 -07:00
import { formatUnixTimestampToDateTime } from "@/utils/dateUtil";
import { useMemo } from "react";
2025-04-22 16:50:21 -05:00
import { useDateLocale } from "@/hooks/use-date-locale";
2024-02-25 12:04:44 -07:00
2024-09-12 13:28:15 -06:00
export function useFormattedTimestamp(
timestamp: number,
format: string,
timezone?: string,
) {
2025-04-22 16:50:21 -05:00
const locale = useDateLocale();
2024-02-25 12:04:44 -07:00
const formattedTimestamp = useMemo(() => {
return formatUnixTimestampToDateTime(timestamp, {
2024-09-12 13:28:15 -06:00
timezone,
2025-04-22 16:50:21 -05:00
date_format: format,
locale,
2024-02-25 12:04:44 -07:00
});
2025-04-22 16:50:21 -05:00
}, [format, timestamp, timezone, locale]);
2024-02-25 12:04:44 -07:00
return formattedTimestamp;
}
2024-03-03 17:19:02 -07:00
2025-05-15 17:13:32 -05:00
export function useFormattedRange(
start: number,
end: number,
format: string,
timezone?: string,
) {
2025-04-22 16:50:21 -05:00
const locale = useDateLocale();
2024-06-23 14:58:00 -06:00
const formattedStart = useMemo(() => {
return formatUnixTimestampToDateTime(start, {
2025-05-15 17:13:32 -05:00
timezone,
2025-04-22 16:50:21 -05:00
date_format: format,
locale,
2024-06-23 14:58:00 -06:00
});
2025-05-15 17:13:32 -05:00
}, [format, start, timezone, locale]);
2025-04-22 16:50:21 -05:00
2024-06-23 14:58:00 -06:00
const formattedEnd = useMemo(() => {
return formatUnixTimestampToDateTime(end, {
2025-05-15 17:13:32 -05:00
timezone,
2025-04-22 16:50:21 -05:00
date_format: format,
locale,
2024-06-23 14:58:00 -06:00
});
2025-05-15 17:13:32 -05:00
}, [format, end, timezone, locale]);
2024-06-23 14:58:00 -06:00
return `${formattedStart} - ${formattedEnd}`;
}
2024-03-03 17:19:02 -07:00
export function useTimezone(config: FrigateConfig | undefined) {
return useMemo(() => {
if (!config) {
return undefined;
}
return (
config.ui?.timezone || Intl.DateTimeFormat().resolvedOptions().timeZone
);
}, [config]);
}
2025-04-22 16:50:21 -05:00
export function use24HourTime(config: FrigateConfig | undefined) {
const localeUses24HourTime = useMemo(
() =>
new Intl.DateTimeFormat(undefined, {
hour: "numeric",
})
?.formatToParts(new Date(2020, 0, 1, 13))
?.find((part) => part.type === "hour")?.value?.length === 2,
[],
);
return useMemo(() => {
if (!config) {
return false;
}
2025-04-22 16:50:21 -05:00
if (config.ui.time_format !== "browser") {
return config.ui.time_format === "24hour";
}
return localeUses24HourTime;
}, [config, localeUses24HourTime]);
}
2026-03-29 14:03:07 -05:00
/**
* Returns the resolved time format key ("24hour" | "12hour") based on config
* and browser locale. Use this instead of checking config.ui.time_format directly
* to correctly handle the "browser" setting.
*/
export function useTimeFormat(
config: FrigateConfig | undefined,
): "24hour" | "12hour" {
const is24Hour = use24HourTime(config);
return is24Hour ? "24hour" : "12hour";
}
export function useFormattedHour(
config: FrigateConfig | undefined,
time: string, // hour is assumed to be in 24 hour format per the Date object
) {
const hour24 = use24HourTime(config);
return useMemo(() => {
if (hour24) {
return time;
}
const [hour, minute] = time.includes(":") ? time.split(":") : [time, "00"];
const hourNum = parseInt(hour);
2024-09-26 15:30:56 -05:00
const adjustedHour = hourNum % 12 || 12;
const period = hourNum < 12 ? "AM" : "PM";
2024-09-26 15:30:56 -05:00
return `${adjustedHour}:${minute} ${period}`;
}, [hour24, time]);
}