Skip to content

Commit 5bda161

Browse files
authored
Merge pull request #276 from MarcelRobitaille/fix_256
Keep timezone in calendar event if available
2 parents 2c9ac76 + b904139 commit 5bda161

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

lib/Service/GoogleCalendarAPIService.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function importCalendar(string $userId, string $calId, string $calName, ?
194194
$events = $this->getCalendarEvents($userId, $calId, $allEvents);
195195
$nbAdded = 0;
196196
$nbUpdated = 0;
197-
/** @var array{id: string, start?: array{date?: string, dateTime?: string}, end?: array{date?: string, dateTime?: string}, colorId?: string, summary?: string, visibility?: string, sequence?: string, location?: string, description?: string, status?: string, created?: string, updated?: string, reminders?: array{useDefault?: bool, overrides?: list{array{minutes?: string, hours?: string, days?: string, weeks?: string}}}, recurrence?: list<string>} $e */
197+
/** @var array{id: string, start?: array{date?: string, dateTime?: string, timeZone?: string}, end?: array{date?: string, dateTime?: string, timeZone?: string}, colorId?: string, summary?: string, visibility?: string, sequence?: string, location?: string, description?: string, status?: string, created?: string, updated?: string, reminders?: array{useDefault?: bool, overrides?: list{array{minutes?: string, hours?: string, days?: string, weeks?: string}}}, recurrence?: list<string>} $e */
198198
foreach ($events as $e) {
199199
$objectUri = $e['id'];
200200

@@ -294,19 +294,29 @@ public function importCalendar(string $userId, string $calId, string $calName, ?
294294
}
295295
}
296296

297-
if (isset($e['start'], $e['start']['date'], $e['end'], $e['end']['date'])) {
297+
if (isset($e['start'], $e['start']['dateTime'], $e['end'], $e['end']['dateTime'])) {
298+
$start = new DateTime($e['start']['dateTime']);
299+
$end = new DateTime($e['end']['dateTime']);
300+
301+
if (isset($e['start']['timeZone'], $e['end']['timeZone'])) {
302+
$timezoneStart = $e['start']['timeZone'];
303+
$start->setTimezone(new DateTimeZone($timezoneStart));
304+
$calData .= "DTSTART;TZID=$timezoneStart:" . $start->format('Ymd\THis') . "\n";
305+
$timezoneEnd = $e['end']['timeZone'];
306+
$end->setTimezone(new DateTimeZone($timezoneEnd));
307+
$calData .= "DTEND;TZID=$timezoneEnd:" . $end->format('Ymd\THis') . "\n";
308+
} else {
309+
$start->setTimezone($utcTimezone);
310+
$calData .= 'DTSTART;VALUE=DATE-TIME:' . $start->format('Ymd\THis\Z') . "\n";
311+
$end->setTimezone($utcTimezone);
312+
$calData .= 'DTEND;VALUE=DATE-TIME:' . $end->format('Ymd\THis\Z') . "\n";
313+
}
314+
} elseif (isset($e['start'], $e['start']['date'], $e['end'], $e['end']['date'])) {
298315
// whole days
299316
$start = new DateTime($e['start']['date']);
300317
$calData .= 'DTSTART;VALUE=DATE:' . $start->format('Ymd') . "\n";
301318
$end = new DateTime($e['end']['date']);
302319
$calData .= 'DTEND;VALUE=DATE:' . $end->format('Ymd') . "\n";
303-
} elseif (isset($e['start']['dateTime']) && isset($e['end']['dateTime'])) {
304-
$start = new DateTime($e['start']['dateTime']);
305-
$start->setTimezone($utcTimezone);
306-
$calData .= 'DTSTART;VALUE=DATE-TIME:' . $start->format('Ymd\THis\Z') . "\n";
307-
$end = new DateTime($e['end']['dateTime']);
308-
$end->setTimezone($utcTimezone);
309-
$calData .= 'DTEND;VALUE=DATE-TIME:' . $end->format('Ymd\THis\Z') . "\n";
310320
} else {
311321
// skip entries without any date
312322
continue;

0 commit comments

Comments
 (0)