iCalendar timezone fix in CalDAV (#5091)

* adds support to timezone change using tzid

* code improvement with math absolute

Co-authored-by: Alex van Andel <me@alexvanandel.com>

Co-authored-by: Alex van Andel <me@alexvanandel.com>
pull/5072/head
Syed Ali Shahbaz 2022-10-19 16:48:08 +05:30 committed by GitHub
parent 373c255733
commit 51e08a7642
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 0 deletions

View File

@ -47,6 +47,17 @@ const getDuration = (start: string, end: string): DurationObject => ({
minutes: dayjs(end).diff(dayjs(start), "minute"), minutes: dayjs(end).diff(dayjs(start), "minute"),
}); });
const buildUtcOffset = (minutes: number): string => {
const h =
minutes > 0
? "+" + (Math.floor(minutes / 60) < 10 ? "0" + Math.floor(minutes / 60) : Math.floor(minutes / 60))
: "-" +
(Math.ceil(minutes / 60) > -10 ? "0" + Math.ceil(minutes / 60) * -1 : Math.ceil(minutes / 60) * -1);
const m = Math.abs(minutes % 60);
const offset = `${h}:${m}`;
return offset;
};
const getAttendees = (attendees: Person[]): Attendee[] => const getAttendees = (attendees: Person[]): Attendee[] =>
attendees.map(({ email, name }) => ({ name, email, partstat: "NEEDS-ACTION" })); attendees.map(({ email, name }) => ({ name, email, partstat: "NEEDS-ACTION" }));
@ -274,6 +285,24 @@ export default abstract class BaseCalendarService implements Calendar {
if (vevent?.getFirstPropertyValue("transp") === "TRANSPARENT") return; if (vevent?.getFirstPropertyValue("transp") === "TRANSPARENT") return;
const event = new ICAL.Event(vevent); const event = new ICAL.Event(vevent);
const tzid: string | undefined = vevent?.getFirstPropertyValue("tzid");
// In case of icalendar, when only tzid is available without vtimezone, we need to add vtimezone explicitly to take care of timezone diff
if (!vcalendar.getFirstSubcomponent("vtimezone") && tzid) {
const timezoneComp = new ICAL.Component("vtimezone");
timezoneComp.addPropertyWithValue("tzid", tzid);
const standard = new ICAL.Component("standard");
// get timezone offset
const tzoffsetfrom = buildUtcOffset(dayjs(event.startDate.toJSDate()).tz(tzid, true).utcOffset());
const tzoffsetto = buildUtcOffset(dayjs(event.endDate.toJSDate()).tz(tzid, true).utcOffset());
// set timezone offset
standard.addPropertyWithValue("tzoffsetfrom", tzoffsetfrom);
standard.addPropertyWithValue("tzoffsetto", tzoffsetto);
// provide a standard dtstart
standard.addPropertyWithValue("dtstart", "1601-01-01T00:00:00");
timezoneComp.addSubcomponent(standard);
vcalendar.addSubcomponent(timezoneComp);
}
const vtimezone = vcalendar.getFirstSubcomponent("vtimezone"); const vtimezone = vcalendar.getFirstSubcomponent("vtimezone");
if (event.isRecurring()) { if (event.isRecurring()) {