Skip to content

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

FIX: Respect user timezone in chat timestamps (#44157)

duckse6c69afcab1d
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs
index 31439879f23..b1eef126f9e 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs
@@ -341,7 +341,10 @@ export default class ChatChannel extends Component {
return;
}
- const message = this.messagesManager.findFirstMessageOfDay(new Date(date));
+ const message = this.messagesManager.findFirstMessageOfDay(
+ new Date(date),
+ this.currentUser.user_option.timezone
+ );
if (message.firstOfResults && this.messagesLoader.canLoadMorePast) {
this.fetchMessages({ target_date: date, direction: FUTURE });
} else {
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-message-separator.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-message-separator.gjs
index 7aac16cdc32..b8afcce70bd 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-message-separator.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-message-separator.gjs
@@ -2,6 +2,7 @@ import Component from "@glimmer/component";
import { cached } from "@glimmer/tracking";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
+import { service } from "@ember/service";
import { modifier } from "ember-modifier";
import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";
import { i18n } from "discourse-i18n";
@@ -9,6 +10,8 @@ import { i18n } from "discourse-i18n";
const IS_PINNED_CLASS = "is-pinned";
export default class ChatMessageSeparator extends Component {
+ @service currentUser;
+
track = modifier((element) => {
const intersectionObserver = new IntersectionObserver(
([entry]) => {
@@ -61,30 +64,34 @@ export default class ChatMessageSeparator extends Component {
return this.args.message.id === this.args.message.channel.newestMessage?.id;
}
+ get #timezone() {
+ return this.currentUser?.user_option?.timezone || moment.tz.guess();
+ }
+
@action
onDateClick() {
return this.args.fetchMessagesByDate?.(this.firstMessageOfTheDayAt);
}
#areDatesOnSameDay(a, b) {
- return (
- a.getFullYear() === b.getFullYear() &&
- a.getMonth() === b.getMonth() &&
- a.getDate() === b.getDate()
- );
+ return moment(a)
+ .tz(this.#timezone)
+ .isSame(moment(b).tz(this.#timezone), "day");
}
#startOfDay(date) {
- return moment(date).startOf("day").format();
+ return moment(date).tz(this.#timezone).startOf("day").format();
}
#calendarDate(date) {
- return moment(date).calendar(moment(), {
- sameDay: `[${i18n("chat.chat_message_separator.today")}]`,
- lastDay: `[${i18n("chat.chat_message_separator.yesterday")}]`,
- lastWeek: "LL",
- sameElse: "LL",
- });
+ return moment(date)
+ .tz(this.#timezone)
+ .calendar(moment().tz(this.#timezone), {
+ sameDay: `[${i18n("chat.chat_message_separator.today")}]`,
+ lastDay: `[${i18n("chat.chat_message_separator.yesterday")}]`,
+ lastWeek: "LL",
+ sameElse: "LL",
+ });
}
<template>
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs
index 8fdd2c8f8f9..50bef95292b 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs
@@ -425,7 +425,10 @@ export default class ChatThread extends Component {
return;
}
- const message = this.messagesManager.findFirstMessageOfDay(new Date(date));
+ const message = this.messagesManager.findFirstMessageOfDay(
+ new Date(date),
+ this.currentUser.user_option.timezone
+ );
if (message.firstOfResults && this.messagesLoader.canLoadMorePast) {
this.fetchMessages({ target_date: date, direction: FUTURE });
} else {
diff --git a/plugins/chat/assets/javascripts/discourse/helpers/format-chat-date.js b/plugins/chat/assets/javascripts/discourse/helpers/format-chat-date.js
index d5704e9f6aa..e2503433a04 100644
--- a/plugins/chat/assets/javascripts/discourse/helpers/format-chat-date.js
+++ b/plugins/chat/assets/javascripts/discourse/helpers/format-chat-date.js
@@ -5,8 +5,8 @@ import { i18n } from "discourse-i18n";
export default function formatChatDate(message, options = {}) {
const currentUser = User.current();
- const tz = currentUser ? currentUser.user_option.timezone : moment.tz.guess();
- const date = moment(new Date(message.createdAt), tz);
+ const timezone = currentUser?.user_option?.timezone || moment.tz.guess();
+ const date = moment(message.createdAt).tz(timezone);
const title = date.format(i18n("dates.long_with_year"));
let display;
diff --git a/plugins/chat/assets/javascripts/discourse/lib/chat-messages-manager.js b/plugins/chat/assets/javascripts/discourse/lib/chat-messages-manager.js
index 74001b1fc98..5907bf4d6c1 100644
--- a/plugins/chat/assets/javascripts/discourse/lib/chat-messages-manager.js
+++ b/plugins/chat/assets/javascripts/discourse/lib/chat-messages-manager.js
@@ -44,12 +44,12 @@ export default class ChatMessagesManager {
);
}
- findFirstMessageOfDay(a) {
- return this.messages.find(
- (b) =>
- a.getFullYear() === b.createdAt.getFullYear() &&
- a.getMonth() === b.createdAt.getMonth() &&
- a.getDate() === b.createdAt.getDate()
+ findFirstMessageOfDay(date, timezone) {
+ const resolvedTimezone = timezone || moment.tz.guess();
+ const targetDate = moment(date).tz(resolvedTimezone);
+
+ return this.messages.find((message) =>
+ targetDate.isSame(moment(message.createdAt).tz(resolvedTimezone), "day")
);
}
diff --git a/plugins/chat/test/javascripts/components/chat-message-info-test.gjs b/plugins/chat/test/javascripts/components/chat-message-info-test.gjs
index ffbdded482c..41fd2052700 100644
--- a/plugins/chat/test/javascripts/components/chat-message-info-test.gjs
+++ b/plugins/chat/test/javascripts/components/chat-message-info-test.gjs
@@ -2,6 +2,7 @@ import { getOwner } from "@ember/owner";
import { render } from "@ember/test-helpers";
import { module, test } from "qunit";
import Bookmark from "discourse/models/bookmark";
+import User from "discourse/models/user";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { i18n } from "discourse-i18n";
import Info from "discourse/plugins/chat/discourse/components/chat/message/info";
@@ -105,6 +106,31 @@ module("Component | ChatMessageInfo", function (hooks) {
assert.dom(".chat-message-info__date").exists();
});
+ test("date uses the user's configured timezone", async function (assert) {
+ moment.tz.setDefault("UTC");
+ this.currentUser.user_option.timezone = "Europe/Madrid";
+ User.resetCurrent(this.currentUser);
+ this.message = new ChatFabricators(getOwner(this)).message({
+ user: { username: "discobot" },
+ created_at: "2026-09-27T12:00:00Z",
+ });
+
+ await render(
+ <template><Info @message={{this.message}} @show={{true}} /></template>
+ );
+
+ assert
+ .dom(".chat-time")
+ .hasText(
+ moment
+ .tz("2026-09-27T12:00:00Z", "Europe/Madrid")
+ .format(i18n("dates.time"))
+ );
+
+ User.resetCurrent();
+ moment.tz.setDefault();
+ });
+
test("bookmark (with reminder)", async function (assert) {
this.message = new ChatFabricators(getOwner(this)).message({
user: { username: "discobot" },
diff --git a/plugins/chat/test/javascripts/components/chat-message-separator-test.gjs b/plugins/chat/test/javascripts/components/chat-message-separator-test.gjs
new file mode 100644
index 00000000000..b1141e105da
--- /dev/null
+++ b/plugins/chat/test/javascripts/components/chat-message-separator-test.gjs
@@ -0,0 +1,36 @@
+import { render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import stubIntersectionObserver from "discourse/tests/helpers/stub-intersection-observer";
+import ChatMessageSeparator from "discourse/plugins/chat/discourse/components/chat-message-separator";
+
+module("Component | ChatMessageSeparator", function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ stubIntersectionObserver();
+ moment.tz.setDefault("UTC");
+ this.currentUser.user_option.timezone = "Europe/Madrid";
+ });
+
+ hooks.afterEach(function () {
+ moment.tz.setDefault();
+ });
+
+ test("separates messages on different days in the user's timezone", async function (assert) {
+ this.message = {
+ id: 2,
+ createdAt: new Date("2026-09-27T22:30:00Z"),
+ previousMessage: {
+ createdAt: new Date("2026-09-27T21:30:00Z"),
+ },
+ channel: { newestMessage: null },
+ };
+
+ await render(
+ <template><ChatMessageSeparator @message={{this.message}} /></template>
+ );
+
+ assert.dom(".chat-message-separator-date").exists();
+ });
+});
diff --git a/plugins/chat/test/javascripts/unit/lib/chat-messages-manager-test.js b/plugins/chat/test/javascripts/unit/lib/chat-messages-manager-test.js
new file mode 100644
index 00000000000..590704718ba
--- /dev/null
+++ b/plugins/chat/test/javascripts/unit/lib/chat-messages-manager-test.js
@@ -0,0 +1,23 @@
+import { getOwner } from "@ember/owner";
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import ChatMessagesManager from "discourse/plugins/chat/discourse/lib/chat-messages-manager";
+
+module("Unit | Lib | chat-messages-manager", function (hooks) {
+ setupTest(hooks);
+
+ test("finds the first message using the user's timezone", function (assert) {
+ const manager = new ChatMessagesManager(getOwner(this));
+ manager.messages = [
+ { id: 1, createdAt: new Date("2026-09-27T21:30:00Z") },
+ { id: 2, createdAt: new Date("2026-09-27T22:30:00Z") },
+ ];
+
+ const message = manager.findFirstMessageOfDay(
+ new Date("2026-09-27T22:00:00Z"),
+ "Europe/Madrid"
+ );
+
+ assert.strictEqual(message.id, 2);
+ });
+});