1+ import { endOfDay , startOfDay } from 'date-fns' ;
2+ import { fromZonedTime , toZonedTime } from 'date-fns-tz' ;
3+
14import type { Event } from '@prisma/client' ;
25
6+ import { TimeZone } from '../constants.js' ;
37import { NotFoundError } from '../utils/AppError.js' ;
48import { getAllEateriesData , refreshCacheFromDB } from '../utils/cache.js' ;
59import type { EateryWithEvents } from '../utils/cache.js' ;
@@ -22,7 +26,7 @@ export const getAllEateries = async (days?: number) => {
2226 return cachedEateries ;
2327 }
2428
25- // Calculate date range for filtering events using UTC to match event timestamps
29+ // Calculate date range in EST
2630 // days=0 means today, days=1 means tomorrow, days=2 means day after tomorrow, etc.
2731 const now = new Date ( ) ;
2832
@@ -31,31 +35,13 @@ export const getAllEateries = async (days?: number) => {
3135 const targetTimestamp = now . getTime ( ) + days * msPerDay ;
3236 const targetDate = new Date ( targetTimestamp ) ;
3337
34- // Set to start of day in UTC
35- const startOfDay = new Date (
36- Date . UTC (
37- targetDate . getUTCFullYear ( ) ,
38- targetDate . getUTCMonth ( ) ,
39- targetDate . getUTCDate ( ) ,
40- 0 ,
41- 0 ,
42- 0 ,
43- 0 ,
44- ) ,
45- ) ;
38+ const targetDateEST = toZonedTime ( targetDate , TimeZone . EASTERN ) ;
39+ const startOfDayEST = startOfDay ( targetDateEST ) ;
40+ const endOfDayEST = endOfDay ( targetDateEST ) ;
4641
47- // Set to end of day in UTC
48- const endOfDay = new Date (
49- Date . UTC (
50- targetDate . getUTCFullYear ( ) ,
51- targetDate . getUTCMonth ( ) ,
52- targetDate . getUTCDate ( ) ,
53- 23 ,
54- 59 ,
55- 59 ,
56- 999 ,
57- ) ,
58- ) ;
42+ // Get the EST times in UTC to compare with event timestamps
43+ const startOfDayUTC = fromZonedTime ( startOfDayEST , TimeZone . EASTERN ) ;
44+ const endOfDayUTC = fromZonedTime ( endOfDayEST , TimeZone . EASTERN ) ;
5945
6046 // Filter events to only include those on the specified day
6147 const filteredEateries = cachedEateries . map ( ( eatery ) => ( {
@@ -64,8 +50,8 @@ export const getAllEateries = async (days?: number) => {
6450 const eventStart = new Date ( event . startTimestamp ) ;
6551 const eventEnd = new Date ( event . endTimestamp ) ;
6652
67- // Include event if it overlaps with the target day
68- return eventStart <= endOfDay && eventEnd >= startOfDay ;
53+ // Include event if it overlaps with the target day in EST
54+ return eventStart <= endOfDayUTC && eventEnd >= startOfDayUTC ;
6955 } ) ,
7056 } ) ) ;
7157
0 commit comments