Skip to content

Commit

Permalink
Added check for daylight saving time in ical parser
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Feb 11, 2022
1 parent 60fea4f commit 61f1dcd
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/lib/ical/common.ts
Expand Up @@ -10,6 +10,18 @@ function cleanIcalKey(key: string): string {
return key;
}

/**
* Checks to see if the given date is observing daylight savings time.
* @param date The date to be checked
* @returns true if the date observes daylight savings time, false otherwise.
* @see https://stackoverflow.com/a/30280636
*/
function checkDateObservesDST(date: Date): boolean {
const jan = new Date(date.getFullYear(), 0, 1);
const jul = new Date(date.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset()) > date.getTimezoneOffset();
}

/**
* The code in this function is derived from
* https://github.com/adrianlee44/ical2json.
Expand Down Expand Up @@ -84,7 +96,11 @@ function parseRawIcalDatetime(datetime: string): Date {

const date = new Date();
date.setFullYear(Number(fullYear), Number(month) - 1, Number(day));
date.setHours(Number(hours) - 7, Number(minutes), Number(seconds));

// The timezone determines how many hours the date is relative of UTC.
const observingDST = checkDateObservesDST(date);
const tzHoursOffset = observingDST ? -7 : -8; // PST
date.setHours(Number(hours) + tzHoursOffset, Number(minutes), Number(seconds));

return date;
}
Expand Down

0 comments on commit 61f1dcd

Please sign in to comment.