Skip to content

Commit

Permalink
fix timezone issue in icalUtils.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
muness committed Jan 21, 2024
1 parent 33eddfb commit 87d121a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions src/icalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ function adjustDateToOriginalTimezone(originalDate: Date, currentDate: Date, tzi
}

export function filterMatchingEvents(icsArray: any[], dayToMatch: string) {
const localTimeZone = tz.zone(tz.guess());

return icsArray.reduce((matchingEvents, event) => {
var hasRecurrenceOverride = false
Expand All @@ -56,13 +55,22 @@ export function filterMatchingEvents(icsArray: any[], dayToMatch: string) {
}
}
if (typeof event.rrule !== 'undefined' && !hasRecurrenceOverride) {
event.rrule.between(moment(dayToMatch).startOf('day').toDate(), moment(dayToMatch).endOf('day').toDate()).forEach(date => {
// We need to clone the event and override the date

// Per the rrule docs: Whether or not you use the `TZID` param, make sure to only use JS `Date` objects that are represented in UTC to avoid unexpected timezone offsets being applied
const utcStartOfDay = moment(dayToMatch).utc().startOf('day').toDate();
const utcEndOfDay = moment(dayToMatch).utc().endOf('day').toDate();

event.rrule.between(utcStartOfDay, utcEndOfDay).forEach(date => {

// now the date is in the local timezone, so we need to apply the offset to get it back to UTC
const offset = moment(date).utcOffset();
date = moment(date).subtract(offset, 'minutes').toDate();


// We need to clone the event and override the date
const clonedEvent = { ...event };

console.debug('Found a recurring event to clone: ', event.summary, ' on ', date, 'at ', event.start.toString());
console.debug("RRULE origOptions:", event.rrule.origOptions);

// But timezones...
if (event.rrule != undefined && event.rrule.origOptions.tzid) {
Expand Down

0 comments on commit 87d121a

Please sign in to comment.