A platform for community discussion. Free, open, simple.
FIX: calendar plugin event timezone mismatch (#37516)
Renato Atiliodaef88a0fb1f
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/event-date.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/event-date.gjsindex 16ed4958b1c..76cdc5be0fc 100644--- a/plugins/discourse-calendar/assets/javascripts/discourse/components/event-date.gjs+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/event-date.gjs@@ -99,7 +99,10 @@ export default class EventDate extends Component {}_parsedDate(date) {- return moment.utc(date).tz(moment.tz.guess());+ const timezone = this.args.topic.event_show_local_time+ ? this.args.topic.event_timezone+ : moment.tz.guess();+ return moment.utc(date).tz(timezone || moment.tz.guess());}_guessedDateFormat() {diff --git a/plugins/discourse-calendar/plugin.rb b/plugins/discourse-calendar/plugin.rbindex 7cf86b22347..2f9b3b7232d 100644--- a/plugins/discourse-calendar/plugin.rb+++ b/plugins/discourse-calendar/plugin.rb@@ -237,7 +237,10 @@ after_initialize doadd_to_class(:topic, :event_starts_at) do@event_starts_at ||=- Time.zone.parse(custom_fields[DiscoursePostEvent::TOPIC_POST_EVENT_STARTS_AT].to_s)+ begin+ value = custom_fields[DiscoursePostEvent::TOPIC_POST_EVENT_STARTS_AT].to_s+ Time.find_zone("UTC").parse(value) if value.present?+ endendadd_to_serializer(@@ -263,7 +266,10 @@ after_initialize doadd_to_class(:topic, :event_ends_at) do@event_ends_at ||=- Time.zone.parse(custom_fields[DiscoursePostEvent::TOPIC_POST_EVENT_ENDS_AT].to_s)+ begin+ value = custom_fields[DiscoursePostEvent::TOPIC_POST_EVENT_ENDS_AT].to_s+ Time.find_zone("UTC").parse(value) if value.present?+ endendadd_to_serializer(@@ -275,6 +281,26 @@ after_initialize doend,) { object.event_ends_at }+ add_to_serializer(+ :topic_view,+ :event_timezone,+ include_condition: -> do+ SiteSetting.discourse_post_event_enabled &&+ SiteSetting.display_post_event_date_on_topic_title &&+ object.topic.first_post&.event&.timezone.present?+ end,+ ) { object.topic.first_post.event.timezone }++ add_to_serializer(+ :topic_view,+ :event_show_local_time,+ include_condition: -> do+ SiteSetting.discourse_post_event_enabled &&+ SiteSetting.display_post_event_date_on_topic_title &&+ object.topic.first_post&.event.present?+ end,+ ) { object.topic.first_post.event.show_local_time }+# DISCOURSE CALENDARrequire_relative "jobs/scheduled/create_holiday_events"diff --git a/plugins/discourse-calendar/spec/serializers/topic_list_item_serializer_spec.rb b/plugins/discourse-calendar/spec/serializers/topic_list_item_serializer_spec.rbindex 62909cc07ce..485335bac3d 100644--- a/plugins/discourse-calendar/spec/serializers/topic_list_item_serializer_spec.rb+++ b/plugins/discourse-calendar/spec/serializers/topic_list_item_serializer_spec.rb@@ -23,6 +23,22 @@ RSpec.describe TopicListItemSerializer doit "returns the start time of the event in proper format" doexpect(parsed_json["event_starts_at"]).to eq("2020-04-24T15:10:00.000Z")end++ it "returns correct UTC time regardless of server timezone" do+ # Event at Feb 2, 04:30 UTC - when server is in Sydney (UTC+11),+ # this should still serialize as Feb 2 04:30 UTC, not Feb 2 15:30 UTC+ Time.use_zone("Australia/Sydney") do+ event_time = Time.utc(2026, 2, 2, 4, 30)+ topic.first_post.event.update!(+ original_starts_at: event_time,+ original_ends_at: event_time + 1.hour,+ )+ # Force reload to clear memoization+ topic.instance_variable_set(:@event_starts_at, nil)++ expect(parsed_json["event_starts_at"]).to eq("2026-02-02T04:30:00.000Z")+ end+ endenddescribe "#event_ends_at" dodiff --git a/plugins/discourse-calendar/spec/serializers/topic_view_serializer_spec.rb b/plugins/discourse-calendar/spec/serializers/topic_view_serializer_spec.rbindex 00dad6ab444..2e555046c10 100644--- a/plugins/discourse-calendar/spec/serializers/topic_view_serializer_spec.rb+++ b/plugins/discourse-calendar/spec/serializers/topic_view_serializer_spec.rb@@ -14,22 +14,87 @@ RSpec.describe TopicViewSerializer doJobs.run_immediately!SiteSetting.calendar_enabled = trueSiteSetting.discourse_post_event_enabled = true- DiscoursePostEvent::Event.create!(- id: first_post.id,- original_starts_at: 1.hour.from_now,- original_ends_at: 2.hours.from_now,- )end- describe "#event_starts_at" do- it "returns the start time of the event in proper format" do- expect(parsed_json["event_starts_at"]).to eq("2020-04-24T15:10:00.000Z")+ context "without timezone" do+ before do+ DiscoursePostEvent::Event.create!(+ id: first_post.id,+ original_starts_at: 1.hour.from_now,+ original_ends_at: 2.hours.from_now,+ )+ end++ describe "#event_starts_at" do+ it "returns the start time of the event in proper format" do+ expect(parsed_json["event_starts_at"]).to eq("2020-04-24T15:10:00.000Z")+ end+ end++ describe "#event_ends_at" do+ it "returns the end time of the event in proper format" do+ expect(parsed_json["event_ends_at"]).to eq("2020-04-24T16:10:00.000Z")+ end+ end++ describe "#event_timezone" do+ it "is not included when event has no timezone" do+ expect(parsed_json).not_to have_key("event_timezone")+ end+ end++ describe "#event_show_local_time" do+ it "returns false when show_local_time is not set" do+ expect(parsed_json["event_show_local_time"]).to eq(false)+ endendend- describe "#event_ends_at" do- it "returns the end time of the event in proper format" do- expect(parsed_json["event_ends_at"]).to eq("2020-04-24T16:10:00.000Z")+ context "with timezone and show_local_time true" do+ before do+ DiscoursePostEvent::Event.create!(+ id: first_post.id,+ original_starts_at: 1.hour.from_now,+ original_ends_at: 2.hours.from_now,+ timezone: "Australia/Sydney",+ show_local_time: true,+ )+ end++ describe "#event_timezone" do+ it "returns the timezone of the event" do+ expect(parsed_json["event_timezone"]).to eq("Australia/Sydney")+ end+ end++ describe "#event_show_local_time" do+ it "returns true when show_local_time is set" do+ expect(parsed_json["event_show_local_time"]).to eq(true)+ end+ end+ end++ context "with timezone and show_local_time false" do+ before do+ DiscoursePostEvent::Event.create!(+ id: first_post.id,+ original_starts_at: 1.hour.from_now,+ original_ends_at: 2.hours.from_now,+ timezone: "Australia/Sydney",+ show_local_time: false,+ )+ end++ describe "#event_timezone" do+ it "returns the timezone of the event" do+ expect(parsed_json["event_timezone"]).to eq("Australia/Sydney")+ end+ end++ describe "#event_show_local_time" do+ it "returns false when show_local_time is explicitly false" do+ expect(parsed_json["event_show_local_time"]).to eq(false)+ endendendenddiff --git a/plugins/discourse-calendar/test/javascripts/integration/components/event-date-test.gjs b/plugins/discourse-calendar/test/javascripts/integration/components/event-date-test.gjsnew file mode 100644index 00000000000..61f085d3857--- /dev/null+++ b/plugins/discourse-calendar/test/javascripts/integration/components/event-date-test.gjs@@ -0,0 +1,88 @@+import { render } from "@ember/test-helpers";+import { module, test } from "qunit";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";+import { fakeTime } from "discourse/tests/helpers/qunit-helpers";+import EventDate from "../../discourse/components/event-date";++module("Integration | Component | EventDate", function (hooks) {+ setupRenderingTest(hooks);++ hooks.beforeEach(function () {+ this.siteSettings.discourse_post_event_enabled = true;+ this.siteSettings.use_local_event_date = true;+ });++ hooks.afterEach(function () {+ this.clock?.restore();+ });++ test("uses event timezone when show_local_time is true", async function (assert) {+ moment.tz.guess = () => "Africa/Harare";+ this.clock = fakeTime("2026-02-02T12:00:00Z", "Africa/Harare", true);++ // 15:00 UTC on Feb 2 is:+ // - Feb 3, 02:00 in Sydney (UTC+11)+ // - Feb 2, 17:00 in Harare (UTC+2)+ // So the date should show Feb 3 (Sydney) not Feb 2 (Harare)+ const topic = {+ event_starts_at: "2026-02-02T15:00:00.000Z",+ event_timezone: "Australia/Sydney",+ event_show_local_time: true,+ };++ await render(<template><EventDate @topic={{topic}} /></template>);++ assert+ .dom(".event-date")+ .hasAttribute(+ "title",+ /February 3, 2026/,+ "displays Feb 3 (Sydney timezone) not Feb 2 (browser Harare timezone)"+ );+ });++ test("uses browser timezone when show_local_time is false", async function (assert) {+ moment.tz.guess = () => "Africa/Harare";+ this.clock = fakeTime("2026-02-02T12:00:00Z", "Africa/Harare", true);++ // 15:00 UTC on Feb 2 is:+ // - Feb 3, 02:00 in Sydney (UTC+11)+ // - Feb 2, 17:00 in Harare (UTC+2)+ // When show_local_time is false, should use browser timezone (Harare)+ const topic = {+ event_starts_at: "2026-02-02T15:00:00.000Z",+ event_timezone: "Australia/Sydney",+ event_show_local_time: false,+ };++ await render(<template><EventDate @topic={{topic}} /></template>);++ assert+ .dom(".event-date")+ .hasAttribute(+ "title",+ /February 2, 2026.*5:00 PM/,+ "displays Feb 2 (browser Harare timezone) not Feb 3 (event Sydney timezone)"+ );+ });++ test("falls back to browser timezone when event has no timezone", async function (assert) {+ moment.tz.guess = () => "Africa/Harare";+ this.clock = fakeTime("2026-02-02T12:00:00Z", "Africa/Harare", true);++ // 15:00 UTC on Feb 2 is Feb 2, 17:00 in Harare (UTC+2)+ const topic = {+ event_starts_at: "2026-02-02T15:00:00.000Z",+ };++ await render(<template><EventDate @topic={{topic}} /></template>);++ assert+ .dom(".event-date")+ .hasAttribute(+ "title",+ /February 2, 2026.*5:00 PM/,+ "displays date in browser timezone (Harare) when no event timezone"+ );+ });+});