FIX: ensures preview is correctly computing timezone for current user (#9758)

FIX: ensures preview is correctly computing timezone for current user (#9758)

This also fixes a related bug with timezones on displayed date when in calendar range.

diff --git a/plugins/discourse-local-dates/assets/javascripts/lib/local-date-builder.js.es6 b/plugins/discourse-local-dates/assets/javascripts/lib/local-date-builder.js.es6
index 96b48dd..7c4dd78 100644
--- a/plugins/discourse-local-dates/assets/javascripts/lib/local-date-builder.js.es6
+++ b/plugins/discourse-local-dates/assets/javascripts/lib/local-date-builder.js.es6
@@ -89,7 +89,14 @@ export default class LocalDateBuilder {
     previewedTimezones.push({
       timezone: this._zoneWithoutPrefix(this.localTimezone),
       current: true,
-      formated: this._createDateTimeRange(localDate, this.time)
+      formated: this._createDateTimeRange(
+        DateWithZoneHelper.fromDatetime(
+          localDate.datetime,
+          localDate.timezone,
+          this.localTimezone
+        ),
+        this.time
+      )
     });
 
     if (
@@ -180,10 +187,12 @@ export default class LocalDateBuilder {
         );
 
       if (inCalendarRange && sameTimezone) {
-        return localDate.datetime.calendar(
-          moment.tz(this.localTimezone),
-          this._calendarFormats(this.time ? this.time : null)
-        );
+        return localDate
+          .datetimeWithZone(this.localTimezone)
+          .calendar(
+            moment.tz(localDate.timezone),
+            this._calendarFormats(this.time ? this.time : null)
+          );
       }
     }
 
diff --git a/plugins/discourse-local-dates/test/javascripts/lib/local-date-builder-test.js.es6 b/plugins/discourse-local-dates/test/javascripts/lib/local-date-builder-test.js.es6
index 66a4938..d4864f7 100644
--- a/plugins/discourse-local-dates/test/javascripts/lib/local-date-builder-test.js.es6
+++ b/plugins/discourse-local-dates/test/javascripts/lib/local-date-builder-test.js.es6
@@ -298,6 +298,18 @@ QUnit.test("option[calendar]", assert => {
       "it stops formating out of calendar range"
     )
   );
+
+  freezeTime({ date: "2020-05-12", timezone: LOS_ANGELES }, () => {
+    assert.buildsCorrectDate(
+      {
+        date: "2020-05-13",
+        time: "18:00",
+        localTimezone: LOS_ANGELES
+      },
+      { formated: "Tomorrow 11:00 AM" },
+      "it correctly displays a different local timezone"
+    );
+  });
 });
 
 QUnit.test("previews", assert => {
@@ -467,4 +479,27 @@ QUnit.test("previews", assert => {
       }
     );
   });
+
+  freezeTime({ date: "2020-05-12", timezone: LOS_ANGELES }, () => {
+    assert.buildsCorrectDate(
+      {
+        date: "2020-05-13",
+        time: "18:00",
+        localTimezone: LOS_ANGELES
+      },
+      {
+        previews: [
+          {
+            current: true,
+            formated: "May 13, 2020 11:00 AM",
+            timezone: "Los Angeles"
+          },
+          {
+            formated: "May 13, 2020 6:00 PM",
+            timezone: "UTC"
+          }
+        ]
+      }
+    );
+  });
 });

GitHub sha: e990d8ad

This commit appears in #9758 which was merged by jjaffeux.