From cd89efa1e3041ed62661f1df5a366635a48e9ab3 Mon Sep 17 00:00:00 2001
From: Thomas Citharel <tcit@tcit.fr>
Date: Tue, 12 Oct 2021 11:53:54 +0200
Subject: [PATCH] Export timezone in ICS files

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
---
 lib/service/export/icalendar.ex | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/lib/service/export/icalendar.ex b/lib/service/export/icalendar.ex
index 83125bdeb..40e5e2a90 100644
--- a/lib/service/export/icalendar.ex
+++ b/lib/service/export/icalendar.ex
@@ -5,7 +5,7 @@ defmodule Mobilizon.Service.Export.ICalendar do
 
   alias Mobilizon.Addresses.Address
   alias Mobilizon.{Config, Events}
-  alias Mobilizon.Events.Event
+  alias Mobilizon.Events.{Event, EventOptions}
   alias Mobilizon.Service.Export.Common
   alias Mobilizon.Service.Formatter.HTML
 
@@ -114,9 +114,9 @@ defmodule Mobilizon.Service.Export.ICalendar do
   defp do_export_event(%Event{} = event) do
     %ICalendar.Event{
       summary: event.title,
-      dtstart: event.begins_on,
+      dtstart: begins_on(event),
       dtstamp: event.publish_at || DateTime.utc_now(),
-      dtend: event.ends_on,
+      dtend: ends_on(event),
       description: HTML.strip_tags(event.description),
       uid: event.uuid,
       url: event.url,
@@ -130,4 +130,18 @@ defmodule Mobilizon.Service.Export.ICalendar do
   defp vendor do
     "Mobilizon #{Config.instance_version()}"
   end
+
+  defp begins_on(%Event{begins_on: begins_on, options: %EventOptions{timezone: timezone}}) do
+    shift_tz(begins_on, timezone)
+  end
+
+  defp ends_on(%Event{ends_on: ends_on, options: %EventOptions{timezone: timezone}}) do
+    shift_tz(ends_on, timezone)
+  end
+
+  defp shift_tz(%DateTime{} = date, timezone) when is_binary(timezone) do
+    DateTime.shift_zone!(date, timezone)
+  end
+
+  defp shift_tz(%DateTime{} = date, _), do: date
 end