Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

people: Verify `utcToZonedTime` returns a `Date` before formatting it.

Aman Agrawal69d26841268d
diff --git a/frontend_tests/node_tests/people.js b/frontend_tests/node_tests/people.js
index 1edb9d6fc9d..df4ab3784f1 100644
--- a/frontend_tests/node_tests/people.js
+++ b/frontend_tests/node_tests/people.js
@@ -3,6 +3,7 @@
const {strict: assert} = require("assert");
const {parseISO} = require("date-fns");
+const date_fns_tz = require("date-fns-tz");
const _ = require("lodash");
const MockDate = require("mockdate");
@@ -515,6 +516,21 @@ test_people("user_timezone", () => {
assert.equal(people.get_user_time(me.user_id), "12:09 AM");
});
+test_people("utcToZonedTime", ({override}) => {
+ MockDate.set(parseISO("20130208T080910").getTime());
+ user_settings.twenty_four_hour_time = true;
+
+ override(date_fns_tz, "utcToZonedTime", () => new Date("2022/10/10 03:24"));
+ assert.equal(people.get_user_time(me.user_id), "3:24");
+
+ override(date_fns_tz, "utcToZonedTime", () => new Date("foo"));
+ blueslip.expect("error", "Got invalid date for timezone: America/Los_Angeles", 2);
+ people.get_user_time(me.user_id);
+
+ override(date_fns_tz, "utcToZonedTime", () => undefined);
+ people.get_user_time(me.user_id);
+});
+
test_people("user_type", () => {
people.init();
diff --git a/static/js/people.js b/static/js/people.js
index 318a356265a..c09be6d64e3 100644
--- a/static/js/people.js
+++ b/static/js/people.js
@@ -287,6 +287,17 @@ export function get_user_time(user_id) {
const user_pref = get_user_time_preferences(user_id);
if (user_pref) {
const current_date = utcToZonedTime(new Date(), user_pref.timezone);
+ // In theory, utcToZonedTime should always return a Date
+ // object. However, we have reports from users in the wild
+ // seeing this returning `undefined`. To avoid throwing an
+ // exception, we check whether we got a Date object back,
+ // including a NaN check, which covers the case where we
+ // called `Date("foo")`.
+ // eslint-disable-next-line unicorn/prefer-number-properties
+ if (!(current_date instanceof Date) || isNaN(current_date)) {
+ blueslip.error(`Got invalid date for timezone: ${user_pref.timezone}`);
+ return undefined;
+ }
return format(current_date, user_pref.format, {timeZone: user_pref.timezone});
}
return undefined;