Skip to content

Commit 9fda3a1

Browse files
committed
Fix days query (filter by EST instead of UTC)
1 parent 10b6384 commit 9fda3a1

2 files changed

Lines changed: 17 additions & 27 deletions

File tree

src/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export const NOTIFICATION_LOOKAHEAD_HOURS = 7;
99
export const ITUNES_LOOKUP_URL =
1010
'https://itunes.apple.com/lookup?bundleId=org.cuappdev.eatery';
1111

12+
export enum TimeZone {
13+
EASTERN = 'America/New_York',
14+
}
15+
1216
export enum Weekday {
1317
SUNDAY = 'Sunday',
1418
MONDAY = 'Monday',

src/eateries/eateryService.ts

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { endOfDay, startOfDay } from 'date-fns';
2+
import { fromZonedTime, toZonedTime } from 'date-fns-tz';
3+
14
import type { Event } from '@prisma/client';
25

6+
import { TimeZone } from '../constants.js';
37
import { NotFoundError } from '../utils/AppError.js';
48
import { getAllEateriesData, refreshCacheFromDB } from '../utils/cache.js';
59
import 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

Comments
 (0)