From cc44f3e0c6dc2e20abb06084aac1d37ce1fdc3b7 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Wed, 9 Dec 2020 14:07:14 +0200 Subject: [PATCH] Corrected timezone support when syncing events with Google Calendar --- application/libraries/Google_sync.php | 34 ++++++++++++++++----------- application/libraries/Timezones.php | 2 +- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/application/libraries/Google_sync.php b/application/libraries/Google_sync.php index f6319247..a648d704 100644 --- a/application/libraries/Google_sync.php +++ b/application/libraries/Google_sync.php @@ -151,12 +151,14 @@ class Google_Sync { $event->setDescription($appointment['notes']); $event->setLocation(isset($appointment['location']) ? $appointment['location'] : $settings['company_name']); + $timezone = new DateTimeZone($provider['timezone']); + $start = new Google_Service_Calendar_EventDateTime(); - $start->setDateTime(date3339(strtotime($appointment['start_datetime']))); + $start->setDateTime((new DateTime($appointment['start_datetime'], $timezone))->format(DateTime::RFC3339)); $event->setStart($start); $end = new Google_Service_Calendar_EventDateTime(); - $end->setDateTime(date3339(strtotime($appointment['end_datetime']))); + $end->setDateTime((new DateTime($appointment['end_datetime'], $timezone))->format(DateTime::RFC3339)); $event->setEnd($end); $event->attendees = []; @@ -170,8 +172,7 @@ class Google_Sync { if ($customer != NULL) { $event_customer = new Google_Service_Calendar_EventAttendee(); - $event_customer->setDisplayName($customer['first_name'] . ' ' - . $customer['last_name']); + $event_customer->setDisplayName($customer['first_name'] . ' ' . $customer['last_name']); $event_customer->setEmail($customer['email']); $event->attendees[] = $event_customer; } @@ -207,19 +208,20 @@ class Google_Sync { $event->setDescription($appointment['notes']); $event->setLocation(isset($appointment['location']) ? $appointment['location'] : $settings['company_name']); + $timezone = new DateTimeZone($provider['timezone']); + $start = new Google_Service_Calendar_EventDateTime(); - $start->setDateTime(date3339(strtotime($appointment['start_datetime']))); + $start->setDateTime((new DateTime($appointment['start_datetime'], $timezone))->format(DateTime::RFC3339)); $event->setStart($start); $end = new Google_Service_Calendar_EventDateTime(); - $end->setDateTime(date3339(strtotime($appointment['end_datetime']))); + $end->setDateTime((new DateTime($appointment['end_datetime'], $timezone))->format(DateTime::RFC3339)); $event->setEnd($end); $event->attendees = []; $event_provider = new Google_Service_Calendar_EventAttendee(); - $event_provider->setDisplayName($provider['first_name'] . ' ' - . $provider['last_name']); + $event_provider->setDisplayName($provider['first_name'] . ' ' . $provider['last_name']); $event_provider->setEmail($provider['email']); $event->attendees[] = $event_provider; @@ -273,12 +275,14 @@ class Google_Sync { $event->setSummary('Unavailable'); $event->setDescription($unavailable['notes']); + $timezone = new DateTimeZone($provider['timezone']); + $start = new Google_Service_Calendar_EventDateTime(); - $start->setDateTime(date3339(strtotime($unavailable['start_datetime']))); + $start->setDateTime((new DateTime($unavailable['start_datetime'], $timezone))->format(DateTime::RFC3339)); $event->setStart($start); $end = new Google_Service_Calendar_EventDateTime(); - $end->setDateTime(date3339(strtotime($unavailable['end_datetime']))); + $end->setDateTime((new DateTime($unavailable['end_datetime'], $timezone))->format(DateTime::RFC3339)); $event->setEnd($end); // Add the new event to the google calendar. @@ -304,12 +308,14 @@ class Google_Sync { $unavailable['id_google_calendar']); $event->setDescription($unavailable['notes']); + $timezone = new DateTimeZone($provider['timezone']); + $start = new Google_Service_Calendar_EventDateTime(); - $start->setDateTime(date3339(strtotime($unavailable['start_datetime']))); + $start->setDateTime((new DateTime($unavailable['start_datetime'], $timezone))->format(DateTime::RFC3339)); $event->setStart($start); $end = new Google_Service_Calendar_EventDateTime(); - $end->setDateTime(date3339(strtotime($unavailable['end_datetime']))); + $end->setDateTime((new DateTime($unavailable['end_datetime'], $timezone))->format(DateTime::RFC3339)); $event->setEnd($end); $updated_event = $this->service->events->update($provider['settings']['google_calendar'], @@ -364,8 +370,8 @@ class Google_Sync { $this->CI->load->helper('general'); $params = [ - 'timeMin' => date3339($start), - 'timeMax' => date3339($end), + 'timeMin' => date(DateTime::RFC3339, $start), + 'timeMax' => date(DateTime::RFC3339, $end), 'singleEvents' => TRUE, ]; diff --git a/application/libraries/Timezones.php b/application/libraries/Timezones.php index f8e55ba0..1c2af09c 100644 --- a/application/libraries/Timezones.php +++ b/application/libraries/Timezones.php @@ -551,7 +551,7 @@ class Timezones { */ public function convert($value, $from_timezone, $to_timezone) { - if ( ! $to_timezone) + if ( ! $to_timezone || $from_timezone === $to_timezone) { return $value; }