Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support timezones #11434

Merged
merged 4 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
Handle offset timezones
  • Loading branch information
NickM-27 committed May 19, 2024
commit 22d977661adeb1f26072382e271b1fb512b7f353
23 changes: 18 additions & 5 deletions web/src/components/player/PreviewPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import useSWR from "swr";
import { FrigateConfig } from "@/types/frigateConfig";
import { Preview } from "@/types/preview";
import { PreviewPlayback } from "@/types/playback";
import { isCurrentHour } from "@/utils/dateUtil";
import { getUTCOffset, isCurrentHour } from "@/utils/dateUtil";
import { baseUrl } from "@/api/baseUrl";
import { isAndroid, isChrome, isMobile } from "react-device-detect";
import { TimeRange } from "@/types/timeline";
Expand Down Expand Up @@ -41,11 +41,21 @@ export default function PreviewPlayer({
const [currentHourFrame, setCurrentHourFrame] = useState<string>();

const currentPreview = useMemo(() => {
const timeRangeOffset =
(getUTCOffset(new Date(timeRange.before * 1000)) % 60) * 60;

console.log(`the offset is ${timeRangeOffset}`);
cameraPreviews.forEach((preview) =>
console.log(
`check ${new Date(Math.round(preview.start) * 1000)} >= ${new Date((timeRange.after + timeRangeOffset) * 1000)}`,
),
);

return cameraPreviews.find(
(preview) =>
preview.camera == camera &&
Math.round(preview.start) >= timeRange.after &&
Math.floor(preview.end) <= timeRange.before,
Math.round(preview.start) >= timeRange.after + timeRangeOffset &&
Math.floor(preview.end) <= timeRange.before + timeRangeOffset,
);
}, [cameraPreviews, camera, timeRange]);

Expand Down Expand Up @@ -225,11 +235,14 @@ function PreviewVideoPlayer({
return;
}

const timeRangeOffset =
getUTCOffset(new Date(timeRange.before * 1000)) % 60;

const preview = cameraPreviews.find(
(preview) =>
preview.camera == camera &&
Math.round(preview.start) >= timeRange.after &&
Math.floor(preview.end) <= timeRange.before,
Math.round(preview.start) >= timeRange.after + timeRangeOffset &&
Math.floor(preview.end) <= timeRange.before + timeRangeOffset,
);

if (preview != currentPreview) {
Expand Down
16 changes: 13 additions & 3 deletions web/src/components/player/dynamic/DynamicVideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ActivityIndicator from "@/components/indicators/activity-indicator";
import { VideoResolutionType } from "@/types/live";
import axios from "axios";
import { cn } from "@/lib/utils";
import { getUTCOffset } from "@/utils/dateUtil";

/**
* Dynamically switches between video playback and scrubbing preview player.
Expand Down Expand Up @@ -49,6 +50,12 @@ export default function DynamicVideoPlayer({
const apiHost = useApiHost();
const { data: config } = useSWR<FrigateConfig>("config");

useEffect(() => {
console.log(
`the time range is ${new Date(timeRange.after * 1000)} -> ${new Date(timeRange.before * 1000)}`,
);
}, [timeRange]);

// controlling playback

const playerRef = useRef<HTMLVideoElement | null>(null);
Expand Down Expand Up @@ -148,9 +155,12 @@ export default function DynamicVideoPlayer({
// state of playback player

const recordingParams = useMemo(() => {
const timeRangeOffset =
(getUTCOffset(new Date(timeRange.before * 1000)) % 60) * 60;

return {
before: timeRange.before,
after: timeRange.after,
before: timeRange.before + timeRangeOffset,
after: timeRange.after + timeRangeOffset,
};
}, [timeRange]);
const { data: recordings } = useSWR<Recording[]>(
Expand All @@ -168,7 +178,7 @@ export default function DynamicVideoPlayer({
}

setSource(
`${apiHost}vod/${camera}/start/${timeRange.after}/end/${timeRange.before}/master.m3u8`,
`${apiHost}vod/${camera}/start/${recordingParams.after}/end/${recordingParams.before}/master.m3u8`,
);
setLoadingTimeout(setTimeout(() => setIsLoading(true), 1000));

Expand Down
12 changes: 8 additions & 4 deletions web/src/utils/dateUtil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import strftime from "strftime";
import { fromUnixTime, intervalToDuration, formatDuration } from "date-fns";
import { useMemo } from "react";
export const longToDate = (long: number): Date => new Date(long * 1000);
export const epochToLong = (date: number): number => date / 1000;
export const dateToLong = (date: Date): number => epochToLong(date.getTime());
Expand Down Expand Up @@ -235,7 +236,10 @@ export const getDurationFromTimestamps = (
* @param timezone string representation of the timezone the user is requesting
* @returns number of minutes offset from UTC
*/
export const getUTCOffset = (date: Date, timezone: string): number => {
export const getUTCOffset = (
date: Date,
timezone: string = getResolvedTimeZone(),
): number => {
// If timezone is in UTC±HH:MM format, parse it to get offset
const utcOffsetMatch = timezone.match(/^UTC([+-])(\d{2}):(\d{2})$/);
if (utcOffsetMatch) {
Expand All @@ -259,10 +263,10 @@ export const getUTCOffset = (date: Date, timezone: string): number => {
target = new Date(`${iso}+000`);
}

return (
return Math.round(
(target.getTime() - utcDate.getTime() - date.getTimezoneOffset()) /
60 /
1000
60 /
1000,
);
};

Expand Down
19 changes: 16 additions & 3 deletions web/src/utils/timelineUtil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
MdOutlinePictureInPictureAlt,
} from "react-icons/md";
import { FaBicycle } from "react-icons/fa";
import { endOfHourOrCurrentTime } from "./dateUtil";
import { endOfHourOrCurrentTime, getUTCOffset } from "./dateUtil";
import { TimeRange, Timeline } from "@/types/timeline";

export function getTimelineIcon(timelineItem: Timeline) {
Expand Down Expand Up @@ -131,12 +131,25 @@ export function getChunkedTimeDay(timeRange: TimeRange): TimeRange[] {
endOfThisHour.setSeconds(0, 0);
const data: TimeRange[] = [];
const startDay = new Date(timeRange.after * 1000);
startDay.setMinutes(0, 0, 0);
const timezoneMinuteOffset =
getUTCOffset(new Date(timeRange.before * 1000)) % 60;

if (timezoneMinuteOffset == 0) {
startDay.setMinutes(0, 0, 0);
} else {
startDay.setSeconds(0, 0);
}

let start = startDay.getTime() / 1000;
let end = 0;

for (let i = 0; i < 24; i++) {
startDay.setHours(startDay.getHours() + 1, 0, 0, 0);
startDay.setHours(
startDay.getHours() + 1,
Math.abs(timezoneMinuteOffset),
0,
0,
);

if (startDay > endOfThisHour) {
break;
Expand Down
7 changes: 7 additions & 0 deletions web/src/views/events/RecordingView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ export function RecordingView({
() => getChunkedTimeDay(timeRange),
[timeRange],
);
useEffect(() => {
chunkedTimeRange.forEach((c) =>
console.log(
`the chunk is ${new Date(c.after * 1000)} -> ${new Date(c.before * 1000)}`,
),
);
}, [chunkedTimeRange]);
const [selectedRangeIdx, setSelectedRangeIdx] = useState(
chunkedTimeRange.findIndex((chunk) => {
return chunk.after <= startTime && chunk.before >= startTime;
Expand Down