Skip to content

Commit c65b549

Browse files
committed
fix: Incorrect Times/Timezones when fetching events from the Community calendar
1 parent 5c0ef1b commit c65b549

File tree

1 file changed

+16
-38
lines changed

1 file changed

+16
-38
lines changed

pages/index.page.tsx

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ function printEventsForNextWeeks(icalData: { [x: string]: any }) {
8383
}
8484

8585
// Calculate the range of dates for the next 12 weeks from today
86-
const today = moment().startOf('day');
87-
const nextFourWeeksEnd = moment().add(12, 'weeks').endOf('day');
86+
const today = moment.utc().startOf('day'); // Use UTC for today
87+
const nextFourWeeksEnd = moment.utc().add(12, 'weeks').endOf('day'); // Use UTC for the end date
8888

8989
// Loop through the events in the iCal data
9090
for (const k in icalData) {
@@ -93,39 +93,20 @@ function printEventsForNextWeeks(icalData: { [x: string]: any }) {
9393
if (event.type === 'VEVENT') {
9494
const title = event.summary;
9595

96-
const timezoneL = moment.tz.guess(); // Default to UTC if timezone information is not provided
97-
const startDate = moment.tz(event.start, timezoneL);
96+
// Parse the start date in UTC
97+
const startDate = moment.utc(event.start);
9898

99-
// Complicated case - if an RRULE exists, handle multiple recurrences of the event.
100-
if (event.rrule !== undefined) {
101-
// For recurring events, get the set of event start dates that fall within the range
102-
// of dates we're looking for.
103-
const dates = event.rrule.between(
104-
today.toDate(),
105-
nextFourWeeksEnd.toDate(),
106-
true,
107-
);
99+
// Complicated case - if an RRULE exists, handle multiple recurrences of the event
100+
if (event.rrule) {
101+
const dates = event.rrule.between(today.toDate(), nextFourWeeksEnd.toDate(), true);
108102

109-
// Loop through the set of date entries to see which recurrences should be printed.
103+
// Loop through the set of date entries
110104
for (const date of dates) {
111-
const startDate = moment.tz(date, timezoneL);
105+
const startDate = moment.utc(date); // Use UTC directly
112106

113-
// Check if the event falls within the next 4 weeks from today
107+
// Check if the event falls within the next 12 weeks
114108
if (startDate.isBetween(today, nextFourWeeksEnd, undefined, '[]')) {
115-
const dateTimezone = moment.tz.zone(event.start.tz);
116-
const localTimezone = moment.tz.guess();
117-
const tz =
118-
event.rrule.origOptions.tzid === localTimezone
119-
? event.rrule.origOptions.tzid
120-
: localTimezone;
121-
const timezone = moment.tz.zone(tz);
122-
let offset;
123-
if (timezone && dateTimezone)
124-
offset = timezone.utcOffset(date) - dateTimezone.utcOffset(date);
125-
const newDate = moment(date).add(offset, 'minutes').toDate();
126-
127-
const start = moment(newDate);
128-
const utcDate = start.utc();
109+
const utcDate = startDate.subtract(5.5,'hours'); // Already in UTC
129110

130111
const time = utcDate.format('MMMM Do YYYY, h:mm a');
131112
const day = utcDate.format('D');
@@ -140,13 +121,13 @@ function printEventsForNextWeeks(icalData: { [x: string]: any }) {
140121
}
141122
}
142123
} else {
143-
// Simple case - no recurrences, just print out the calendar event.
124+
// Simple case - no recurrences
144125
if (startDate.isBetween(today, nextFourWeeksEnd, undefined, '[]')) {
145-
const utcDate = startDate.utc();
126+
const utcDate = startDate.subtract(5.5,'hours'); // Already in UTC
146127

147128
const time = utcDate.format('MMMM Do YYYY, h:mm a');
148129
const day = utcDate.format('D');
149-
const parsedStartDate = startDate.format('YYYY-MM-DD HH:mm:ss');
130+
const parsedStartDate = utcDate.format('YYYY-MM-DD HH:mm:ss');
150131
arrayDates.push({
151132
title,
152133
time,
@@ -159,11 +140,8 @@ function printEventsForNextWeeks(icalData: { [x: string]: any }) {
159140
}
160141
}
161142

162-
arrayDates.sort(
163-
(x, y) =>
164-
new Date(x.parsedStartDate).getTime() -
165-
new Date(y.parsedStartDate).getTime(),
166-
);
143+
// Sort the array based on parsedStartDate
144+
arrayDates.sort((x, y) => new Date(x.parsedStartDate).getTime() - new Date(y.parsedStartDate).getTime());
167145

168146
return arrayDates;
169147
}

0 commit comments

Comments
 (0)