From 7f371420f3fb7b55193cb271e1b85bc8992aff91 Mon Sep 17 00:00:00 2001 From: Dennis Lustre Date: Tue, 12 Nov 2024 17:52:55 -0800 Subject: [PATCH] add utcToPacific() --- apps/expo/src/app/index.tsx | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/apps/expo/src/app/index.tsx b/apps/expo/src/app/index.tsx index e1c74fd2..333a444d 100644 --- a/apps/expo/src/app/index.tsx +++ b/apps/expo/src/app/index.tsx @@ -1,7 +1,8 @@ import React from "react"; import { Platform, RefreshControl } from "react-native"; import { AlertTriangle } from "@tamagui/lucide-icons"; -import { isWithinInterval } from "date-fns"; +import { addDays, isWithinInterval } from "date-fns"; +import { toZonedTime } from "date-fns-tz"; import { ScrollView, Spinner, @@ -55,17 +56,38 @@ export default function Home() { const brandywinePeriods = brandywineInfo?.menus.map((menu) => menu.period) ?? []; + /** + * Convert postgres `time` column to PST. + * + * e.g. "00:30:00" -> "4:30 PM" + */ + function utcToPacific(timeString: string) { + const today = new Date(); + const [hours, minutes, seconds] = timeString.split(":"); + const utcDate = new Date( + Date.UTC( + today.getFullYear(), + today.getMonth(), + today.getDate(), + parseInt(hours!), + parseInt(minutes!), + parseInt(seconds!), + ), + ); + return addDays(toZonedTime(utcDate, "America/Los_Angeles"), 1); + } + const currentAnteateryPeriod = anteateryPeriods.find((period) => isWithinInterval(new Date(), { - start: period.startTime, - end: period.endTime, + start: utcToPacific(period.startTime), + end: utcToPacific(period.endTime), }), ); const currentBrandywinePeriod = brandywinePeriods.find((period) => isWithinInterval(new Date(), { - start: period.startTime, - end: period.endTime, + start: utcToPacific(period.startTime), + end: utcToPacific(period.endTime), }), );