From 05133a78ac9d389bf3e2f48b8a2db587e14f048a Mon Sep 17 00:00:00 2001 From: john gravois Date: Sat, 9 Nov 2024 12:57:26 -0800 Subject: [PATCH] account for daylight savings time (#34) * very wip * account for daylight savings time * less is more --- app/routes/reservations.new.tsx | 5 +++-- app/utils.ts | 12 +++++++++--- package-lock.json | 14 ++++++++++---- package.json | 4 ++-- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/app/routes/reservations.new.tsx b/app/routes/reservations.new.tsx index 75a8bd9..a99e1c1 100644 --- a/app/routes/reservations.new.tsx +++ b/app/routes/reservations.new.tsx @@ -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) => { @@ -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({ diff --git a/app/utils.ts b/app/utils.ts index 7cfbe86..53af0d7 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -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"; @@ -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", { @@ -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) diff --git a/package-lock.json b/package-lock.json index 8dd23aa..28aaca6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,6 +6,7 @@ "": { "name": "court-dibs", "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", @@ -14,7 +15,7 @@ "@remix-run/serve": "^2.8.0", "@terraformer/spatial": "^2.1.2", "classnames": "^2.5.1", - "date-fns": "^3.4.0", + "date-fns": "^4.1.0", "date-fns-tz": "^3.1.3", "isbot": "^3.7.0", "react": "^18.2.0", @@ -912,6 +913,11 @@ "ms": "^2.1.1" } }, + "node_modules/@date-fns/tz": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@date-fns/tz/-/tz-1.2.0.tgz", + "integrity": "sha512-LBrd7MiJZ9McsOgxqWX7AaxrDjcFVjWH/tIKJd7pnR7McaslGYOP1QmmiBXdJH/H/yLCT+rcQ7FaPBUxRGUtrg==" + }, "node_modules/@emotion/hash": { "version": "0.9.1", "resolved": "https://registry.npmjs.org/@emotion/hash/-/hash-0.9.1.tgz", @@ -5828,9 +5834,9 @@ } }, "node_modules/date-fns": { - "version": "3.4.0", - "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-3.4.0.tgz", - "integrity": "sha512-Akz4R8J9MXBsOgF1QeWeCsbv6pntT5KCPjU0Q9prBxVmWJYPLhwAIsNg3b0QAdr0ttiozYLD3L/af7Ra0jqYXw==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-4.1.0.tgz", + "integrity": "sha512-Ukq0owbQXxa/U3EGtsdVBkR1w7KOQ5gIBqdH2hkvknzZPYvBxb/aa6E8L7tmjFtkwZBu3UXBbjIgPo/Ez4xaNg==", "funding": { "type": "github", "url": "https://github.com/sponsors/kossnocorp" diff --git a/package.json b/package.json index fb4b0ad..641ca99 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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",