A platform for community discussion. Free, open, simple.
FIX: Time shifting when editing events (#43761)
Martin Brennan89a984d2c494
diff --git a/plugins/discourse-events/assets/javascripts/discourse/components/modal/post-event-builder.gjs b/plugins/discourse-events/assets/javascripts/discourse/components/modal/post-event-builder.gjsindex c676acbd0d9..e4e8582e773 100644--- a/plugins/discourse-events/assets/javascripts/discourse/components/modal/post-event-builder.gjs+++ b/plugins/discourse-events/assets/javascripts/discourse/components/modal/post-event-builder.gjs@@ -30,6 +30,7 @@ import {customFieldFormName,defaultEventState,defaultReminderFor,+ eventDateInTimezone,getCustomFieldNames,isLivestreamUrl,livestreamSource,@@ -639,7 +640,7 @@ export default class PostEventBuilder extends Component {if (this.event.allDay) {return moment(this.event.startsAt, "YYYY-MM-DD");}- return moment(this.event.startsAt).tz(this.event.timezone || "UTC");+ return eventDateInTimezone(this.event.startsAt, this.event.timezone);}#initEndsAt() {@@ -649,7 +650,7 @@ export default class PostEventBuilder extends Component {if (this.event.allDay) {return moment(this.event.endsAt, "YYYY-MM-DD");}- return moment(this.event.endsAt).tz(this.event.timezone || "UTC");+ return eventDateInTimezone(this.event.endsAt, this.event.timezone);}#snapshotFormData() {diff --git a/plugins/discourse-events/assets/javascripts/discourse/lib/event-recurrence.js b/plugins/discourse-events/assets/javascripts/discourse/lib/event-recurrence.jsindex c585851914a..f90f16b126c 100644--- a/plugins/discourse-events/assets/javascripts/discourse/lib/event-recurrence.js+++ b/plugins/discourse-events/assets/javascripts/discourse/lib/event-recurrence.js@@ -1,4 +1,5 @@import { i18n } from "discourse-i18n";+import { eventDateInTimezone } from "./raw-event-helper";const ORDINALS = ["first", "second", "third", "fourth"];@@ -6,7 +7,7 @@ export function recurrenceRef(event) {if (event.allDay) {return moment(event.startsAt, "YYYY-MM-DD");}- return moment(event.startsAt).tz(event.timezone || "UTC");+ return eventDateInTimezone(event.startsAt, event.timezone);}export function recurrenceContext(ref) {diff --git a/plugins/discourse-events/assets/javascripts/discourse/lib/raw-event-helper.js b/plugins/discourse-events/assets/javascripts/discourse/lib/raw-event-helper.jsindex dd7f65b0784..73ce3587abf 100644--- a/plugins/discourse-events/assets/javascripts/discourse/lib/raw-event-helper.js+++ b/plugins/discourse-events/assets/javascripts/discourse/lib/raw-event-helper.js@@ -72,6 +72,17 @@ export function livestreamSource(location, url) {return location?.trim() ? location : url;}+export function eventDateInTimezone(value, timezone) {+ if (!value) {+ return null;+ }++ const eventTimezone = timezone || "UTC";+ return moment.isMoment(value)+ ? value.clone().tz(eventTimezone)+ : moment.tz(value, eventTimezone);+}+export function allDayTransition({ startsAt, endsAt, timezone, allDay }) {if (allDay) {const startDate = (startsAt ?? moment.tz(timezone)).format("YYYY-MM-DD");@@ -212,7 +223,7 @@ export function buildParams(startsAt, endsAt, event, siteSettings) {params.start = event.allDay? moment(startsAt).format("YYYY-MM-DD")- : moment(startsAt).tz(eventTz).format("YYYY-MM-DD HH:mm");+ : eventDateInTimezone(startsAt, eventTz).format("YYYY-MM-DD HH:mm");if (event.isClosed) {params.closed = "true";@@ -279,7 +290,7 @@ export function buildParams(startsAt, endsAt, event, siteSettings) {if (endsAt) {params.end = event.allDay? moment(endsAt).format("YYYY-MM-DD")- : moment(endsAt).tz(eventTz).format("YYYY-MM-DD HH:mm");+ : eventDateInTimezone(endsAt, eventTz).format("YYYY-MM-DD HH:mm");}if (event.status === "private") {diff --git a/plugins/discourse-events/test/javascripts/integration/components/post-event-builder-test.gjs b/plugins/discourse-events/test/javascripts/integration/components/post-event-builder-test.gjsindex fdc13f47a91..75f28b3d01c 100644--- a/plugins/discourse-events/test/javascripts/integration/components/post-event-builder-test.gjs+++ b/plugins/discourse-events/test/javascripts/integration/components/post-event-builder-test.gjs@@ -144,6 +144,36 @@ module("Integration | Component | Modal | PostEventBuilder", function (hooks) {);});+ test("editing a local-time event preserves its wall-clock time", async function (assert) {+ const event = eventWith({+ starts_at: "2026-09-30T20:15:00",+ ends_at: "2026-09-30T22:15:00",+ timezone: "Europe/Madrid",+ show_local_time: true,+ });++ let savedDates;+ await renderAdvanced(event, {+ onUpdate: (startsAt, endsAt) => {+ savedDates = { startsAt, endsAt };+ },+ });++ await fillIn(`[data-name="description"] textarea`, "Updated description");+ await click(".btn-primary");++ assert.strictEqual(+ savedDates.startsAt.format("YYYY-MM-DD HH:mm Z"),+ "2026-09-30 20:15 +02:00",+ "the start remains anchored to the event timezone"+ );+ assert.strictEqual(+ savedDates.endsAt.format("YYYY-MM-DD HH:mm Z"),+ "2026-09-30 22:15 +02:00",+ "the end remains anchored to the event timezone"+ );+ });+test("typed custom field survives a compact-screen edit in between", async function (assert) {this.siteSettings.discourse_post_event_allowed_custom_fields = "test1";diff --git a/plugins/discourse-events/test/javascripts/lib/raw-event-helper-test.js b/plugins/discourse-events/test/javascripts/lib/raw-event-helper-test.jsindex 4e56cedf469..35318c0ab63 100644--- a/plugins/discourse-events/test/javascripts/lib/raw-event-helper-test.js+++ b/plugins/discourse-events/test/javascripts/lib/raw-event-helper-test.js@@ -7,6 +7,7 @@ import {buildEventBlock,buildParams,defaultReminderFor,+ eventDateInTimezone,isLivestreamUrl,parseEventAttrs,parseEventBlock,@@ -44,6 +45,44 @@ const LONG_DEFAULT = {module("Unit | Lib | raw-event-helper", function (hooks) {setupTest(hooks);+ test("eventDateInTimezone preserves floating wall-clock times", function (assert) {+ const date = eventDateInTimezone("2026-09-30T20:15:00", "Europe/Madrid");++ assert.strictEqual(+ date.format("YYYY-MM-DD HH:mm Z"),+ "2026-09-30 20:15 +02:00",+ "an offset-free event date is parsed in the event timezone"+ );+ });++ test("eventDateInTimezone preserves the instant of offset dates", function (assert) {+ const date = eventDateInTimezone(+ "2026-09-30T11:15:00-07:00",+ "Europe/Madrid"+ );++ assert.strictEqual(+ date.format("YYYY-MM-DD HH:mm Z"),+ "2026-09-30 20:15 +02:00",+ "an offset-bearing event date is converted to the event timezone"+ );+ });++ test("buildParams preserves floating event times", function (assert) {+ const params = buildParams(+ "2026-09-30T20:15:00",+ "2026-09-30T22:15:00",+ { timezone: "Europe/Madrid", showLocalTime: true },+ { discourse_post_event_allowed_custom_fields: "" }+ );++ assert.deepEqual(+ { start: params.start, end: params.end },+ { start: "2026-09-30 20:15", end: "2026-09-30 22:15" },+ "direct BBCode rewrites keep the event's wall-clock range"+ );+ });+test("removeEvent", function (assert) {assert.strictEqual(removeEvent('[event start="2024-01-01"]\nDescription\n[/event]'),