Skip to content

Commit

Permalink
account for daylight savings in dusk calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
jgravois committed Jan 3, 2025
1 parent 05133a7 commit 0f31066
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion app/components/DuskCalculator.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { addMinutes } from "date-fns";
import * as SunCalc from "suncalc";

import { getPacificOffset } from "~/utils";

const LAT_LONG = [-117.7, 33.5].reverse() as [number, number];

// determine whether a America/Los_Angeles reservation
Expand All @@ -15,7 +17,8 @@ export const DuskCalculator = ({
duration: string;
startTime: string;
}) => {
const start = new Date(`${date}T${startTime}:00-07:00`);
const offset = getPacificOffset(date);
const start = new Date(`${date}T${startTime}:00-0${offset}:00`);
const end = addMinutes(start, Number(duration));
const { dusk } = SunCalc.getTimes(start, ...LAT_LONG);

Expand Down
2 changes: 1 addition & 1 deletion app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const anotherTimeFormattingFunc = (val: string | null) => {
return formatTime(Number(h), m == "30");
};

// either GMT -7 or -8 depending on the season
// 7 or 8 (from GMT) depending on the season
export const getPacificOffset = (rawDate: string) => {
const [year, month, day] = rawDate.split("-").map((val) => Number(val));
return new TZDate(year, month, day, "America/Los_Angeles").getTimezoneOffset() / 60;
Expand Down

0 comments on commit 0f31066

Please sign in to comment.