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
5 changed files with 23 additions and 27 deletions

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) {
const date = new Date(timestamp * 1000);
date.setMinutes(0, 0, 0);

View File

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