Skip to content

A platform for community discussion. Free, open, simple.

FIX: use event timezone for Google Calendar export (#36727)

Régis Hanolb9e6c298715d
diff --git a/frontend/discourse/app/lib/download-calendar.js b/frontend/discourse/app/lib/download-calendar.js
index 516f54ee945..3856fcac88d 100644
--- a/frontend/discourse/app/lib/download-calendar.js
+++ b/frontend/discourse/app/lib/download-calendar.js
@@ -45,8 +45,9 @@ export function downloadGoogle(title, dates, options = {}) {
link.searchParams.append("text", title);
link.searchParams.append(
"dates",
- `${_formatDateForGoogleApi(date.startsAt)}/${_formatDateForGoogleApi(
- date.endsAt
+ `${_formatDateForGoogleApi(date.startsAt, date.timezone)}/${_formatDateForGoogleApi(
+ date.endsAt,
+ date.timezone
)}`
);
@@ -243,8 +244,7 @@ function _displayModal(title, dates, options = {}) {
});
}
-function _formatDateForGoogleApi(date) {
- return moment(date)
- .toISOString()
- .replace(/-|:|\.\d\d\d/g, "");
+function _formatDateForGoogleApi(date, timezone) {
+ const momentDate = timezone ? moment.tz(date, timezone) : moment(date);
+ return momentDate.utc().format("YYYYMMDD[T]HHmmss[Z]");
}
diff --git a/frontend/discourse/tests/unit/lib/download-calendar-test.js b/frontend/discourse/tests/unit/lib/download-calendar-test.js
index 1908ae521f6..712361b78bc 100644
--- a/frontend/discourse/tests/unit/lib/download-calendar-test.js
+++ b/frontend/discourse/tests/unit/lib/download-calendar-test.js
@@ -150,6 +150,25 @@ module("Unit | Utility | download-calendar", function (hooks) {
);
});
+ test("correct url for Google with timezone", function (assert) {
+ // America/New_York is UTC-4 in October, so 18:30 NY = 22:30 UTC
+ downloadGoogle("event", [
+ {
+ startsAt: "2021-10-12T18:30:00",
+ endsAt: "2021-10-12T21:00:00",
+ timezone: "America/New_York",
+ },
+ ]);
+ assert.true(
+ window.open.calledWith(
+ "https://www.google.com/calendar/event?action=TEMPLATE&text=event&dates=20211012T223000Z%2F20211013T010000Z",
+ "_blank",
+ "noopener",
+ "noreferrer"
+ )
+ );
+ });
+
test("correct url for Google when recurring event", function (assert) {
downloadGoogle(
"event",