Fix timezone conversion logic (#11444)

* Fix timezone conversion logic

* Use function for consistency

* Cleanup
This commit is contained in:
Nicolas Mowen 2024-05-20 07:02:19 -06:00 committed by GitHub
parent 82d4bf8ab5
commit 2a16d5593a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 23 additions and 27 deletions

View File

@ -10,7 +10,7 @@ import useSWR from "swr";
import { FrigateConfig } from "@/types/frigateConfig"; import { FrigateConfig } from "@/types/frigateConfig";
import { Preview } from "@/types/preview"; import { Preview } from "@/types/preview";
import { PreviewPlayback } from "@/types/playback"; import { PreviewPlayback } from "@/types/playback";
import { getUTCOffset, isCurrentHour } from "@/utils/dateUtil"; import { getTimestampOffset, isCurrentHour } from "@/utils/dateUtil";
import { baseUrl } from "@/api/baseUrl"; import { baseUrl } from "@/api/baseUrl";
import { isAndroid, isChrome, isMobile } from "react-device-detect"; import { isAndroid, isChrome, isMobile } from "react-device-detect";
import { TimeRange } from "@/types/timeline"; import { TimeRange } from "@/types/timeline";
@ -41,8 +41,7 @@ export default function PreviewPlayer({
const [currentHourFrame, setCurrentHourFrame] = useState<string>(); const [currentHourFrame, setCurrentHourFrame] = useState<string>();
const currentPreview = useMemo(() => { const currentPreview = useMemo(() => {
const timeRangeOffset = const timeRangeOffset = getTimestampOffset(timeRange.before);
(getUTCOffset(new Date(timeRange.before * 1000)) % 60) * 60;
return cameraPreviews.find( return cameraPreviews.find(
(preview) => (preview) =>
@ -233,8 +232,8 @@ function PreviewVideoPlayer({
return; return;
} }
const timeRangeOffset = // account for minutes offset in timezone
getUTCOffset(new Date(timeRange.before * 1000)) % 60; const timeRangeOffset = getTimestampOffset(timeRange.before);
const preview = cameraPreviews.find( const preview = cameraPreviews.find(
(preview) => (preview) =>

View File

@ -12,7 +12,7 @@ import ActivityIndicator from "@/components/indicators/activity-indicator";
import { VideoResolutionType } from "@/types/live"; import { VideoResolutionType } from "@/types/live";
import axios from "axios"; import axios from "axios";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
import { getUTCOffset } from "@/utils/dateUtil"; import { getTimestampOffset } from "@/utils/dateUtil";
/** /**
* Dynamically switches between video playback and scrubbing preview player. * Dynamically switches between video playback and scrubbing preview player.
@ -149,8 +149,7 @@ export default function DynamicVideoPlayer({
// state of playback player // state of playback player
const recordingParams = useMemo(() => { const recordingParams = useMemo(() => {
const timeRangeOffset = const timeRangeOffset = getTimestampOffset(timeRange.before);
(getUTCOffset(new Date(timeRange.before * 1000)) % 60) * 60;
return { return {
before: timeRange.before + timeRangeOffset, before: timeRange.before + timeRangeOffset,

View File

@ -11,7 +11,7 @@ import {
ReviewSeverity, ReviewSeverity,
ReviewSummary, ReviewSummary,
} from "@/types/review"; } from "@/types/review";
import { getUTCOffset } from "@/utils/dateUtil"; import { getTimestampOffset } from "@/utils/dateUtil";
import EventView from "@/views/events/EventView"; import EventView from "@/views/events/EventView";
import { RecordingView } from "@/views/events/RecordingView"; import { RecordingView } from "@/views/events/RecordingView";
import axios from "axios"; import axios from "axios";
@ -167,7 +167,8 @@ export default function Events() {
return undefined; return undefined;
} }
const timezoneMinuteOffset = (getUTCOffset(new Date()) % 60) * 60; // offset by timezone minutes
const timestampOffset = getTimestampOffset(Date.now() / 1000);
const startDate = new Date(); const startDate = new Date();
startDate.setMinutes(0, 0, 0); startDate.setMinutes(0, 0, 0);
@ -176,8 +177,8 @@ export default function Events() {
endDate.setHours(0, 0, 0, 0); endDate.setHours(0, 0, 0, 0);
return { return {
after: startDate.getTime() / 1000 + timezoneMinuteOffset, after: startDate.getTime() / 1000 + timestampOffset,
before: endDate.getTime() / 1000 + timezoneMinuteOffset, before: endDate.getTime() / 1000 + timestampOffset,
}; };
}, [reviews]); }, [reviews]);

View File

@ -269,6 +269,15 @@ export const getUTCOffset = (
); );
}; };
/**
* Gets the minute offset in seconds of the current timezone from UTC.
* Any timezones with an offset in hours will return 0,
* any timezones with an offset of 30 or 45 minutes will return that amount in seconds.
*/
export function getTimestampOffset(timestamp: number) {
return (getUTCOffset(new Date(timestamp * 1000)) % 60) * 60;
}
export function getRangeForTimestamp(timestamp: number) { export function getRangeForTimestamp(timestamp: number) {
const date = new Date(timestamp * 1000); const date = new Date(timestamp * 1000);
date.setMinutes(0, 0, 0); date.setMinutes(0, 0, 0);

View File

@ -20,7 +20,7 @@ import {
MdOutlinePictureInPictureAlt, MdOutlinePictureInPictureAlt,
} from "react-icons/md"; } from "react-icons/md";
import { FaBicycle } from "react-icons/fa"; import { FaBicycle } from "react-icons/fa";
import { endOfHourOrCurrentTime, getUTCOffset } from "./dateUtil"; import { endOfHourOrCurrentTime } from "./dateUtil";
import { TimeRange, Timeline } from "@/types/timeline"; import { TimeRange, Timeline } from "@/types/timeline";
export function getTimelineIcon(timelineItem: Timeline) { export function getTimelineIcon(timelineItem: Timeline) {
@ -131,25 +131,13 @@ export function getChunkedTimeDay(timeRange: TimeRange): TimeRange[] {
endOfThisHour.setSeconds(0, 0); endOfThisHour.setSeconds(0, 0);
const data: TimeRange[] = []; const data: TimeRange[] = [];
const startDay = new Date(timeRange.after * 1000); const startDay = new Date(timeRange.after * 1000);
const timezoneMinuteOffset =
getUTCOffset(new Date(timeRange.before * 1000)) % 60;
if (timezoneMinuteOffset == 0) {
startDay.setMinutes(0, 0, 0); startDay.setMinutes(0, 0, 0);
} else {
startDay.setSeconds(0, 0);
}
let start = startDay.getTime() / 1000; let start = startDay.getTime() / 1000;
let end = 0; let end = 0;
for (let i = 0; i < 24; i++) { for (let i = 0; i < 24; i++) {
startDay.setHours( startDay.setHours(startDay.getHours() + 1, 0, 0, 0);
startDay.getHours() + 1,
Math.abs(timezoneMinuteOffset),
0,
0,
);
if (startDay > endOfThisHour) { if (startDay > endOfThisHour) {
break; break;