Skip to content

Commit

Permalink
account for daylight savings time (#34)
Browse files Browse the repository at this point in the history
* very wip

* account for daylight savings time

* less is more
  • Loading branch information
jgravois authored Nov 9, 2024
1 parent 9ae4c61 commit 05133a7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
5 changes: 3 additions & 2 deletions app/routes/reservations.new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
requireValidStytchToken,
sessionStorage,
} from "~/session.server";
import { anotherTimeFormattingFunc } from "~/utils";
import { anotherTimeFormattingFunc, getPacificOffset } from "~/utils";

// if an anonymous or stale user gets here, fail fast
export const loader = async ({ request }: LoaderFunctionArgs) => {
Expand Down Expand Up @@ -65,7 +65,8 @@ export const action = async ({ request }: ActionFunctionArgs) => {
return json({ errors: { start: "court is required" } }, { status: 400 });
}

const start = new Date(`${startDate}T${startTime}:00-07:00`);
const offset = getPacificOffset(startDate);
const start = new Date(`${startDate}T${startTime}:00-0${offset}:00`);

try {
await createReservation({
Expand Down
12 changes: 9 additions & 3 deletions app/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { tz, TZDate } from "@date-fns/tz";
import { useMatches } from "@remix-run/react";
import { contains } from "@terraformer/spatial";
import { addDays } from "date-fns";
import { formatInTimeZone } from "date-fns-tz";
import { addDays, format as dateFnsFormat } from "date-fns";
import type { GeoJSON } from "geojson";
import { useMemo } from "react";

Expand Down Expand Up @@ -32,6 +32,12 @@ export const anotherTimeFormattingFunc = (val: string | null) => {
return formatTime(Number(h), m == "30");
};

// either GMT -7 or -8 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;
}

export const getTimezoneOffsetMs = (date: Date) => {
const invdate = new Date(
date.toLocaleString("en-US", {
Expand All @@ -49,7 +55,7 @@ export const getTimezoneOffset = (date: Date) => {
}

export const format = (date: Date | string, format: string) =>
formatInTimeZone(date, "America/Los_Angeles", format);
dateFnsFormat(date, format, { in: tz("America/Los_Angeles") });

export const formatTime = (rawHour: number, isHalf = false): string => {
const hour = rawHour - (rawHour > 12 ? 12 : 0)
Expand Down
14 changes: 10 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"/public/build"
],
"dependencies": {
"@date-fns/tz": "^1.2.0",
"@googlemaps/js-api-loader": "^1.16.6",
"@prisma/client": "^4.16.2",
"@remix-run/css-bundle": "^2.8.0",
Expand All @@ -33,8 +34,7 @@
"@remix-run/serve": "^2.8.0",
"@terraformer/spatial": "^2.1.2",
"classnames": "^2.5.1",
"date-fns": "^3.4.0",
"date-fns-tz": "^3.1.3",
"date-fns": "^4.1.0",
"isbot": "^3.7.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down

0 comments on commit 05133a7

Please sign in to comment.