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.

users: Canonicalize the timezone identifier.

Anders Kaseorg41f509170be9
diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.js
index 76baae4dba5..27b9d742750 100644
--- a/frontend_tests/node_tests/dispatch.js
+++ b/frontend_tests/node_tests/dispatch.js
@@ -496,7 +496,7 @@ run_test("realm_user", (override) => {
const added_person = people.get_by_user_id(event.person.user_id);
// sanity check a few individual fields
assert.equal(added_person.full_name, "Test User");
- assert.equal(added_person.timezone, "US/Eastern");
+ assert.equal(added_person.timezone, "America/New_York");
// ...but really the whole struct gets copied without any
// manipulation
diff --git a/frontend_tests/node_tests/lib/events.js b/frontend_tests/node_tests/lib/events.js
index 25526de8940..553c56dac8d 100644
--- a/frontend_tests/node_tests/lib/events.js
+++ b/frontend_tests/node_tests/lib/events.js
@@ -453,7 +453,7 @@ exports.fixtures = {
is_bot: false,
is_guest: false,
profile_data: {},
- timezone: "US/Eastern",
+ timezone: "America/New_York",
date_joined: "2020-01-01",
},
},
diff --git a/frontend_tests/node_tests/people.js b/frontend_tests/node_tests/people.js
index 5793bf006c5..9c8ad4935f4 100644
--- a/frontend_tests/node_tests/people.js
+++ b/frontend_tests/node_tests/people.js
@@ -35,7 +35,7 @@ const me = {
user_id: 30,
full_name: "Me Myself",
- timezone: "US/Pacific",
+ timezone: "America/Los_Angeles",
is_admin: false,
is_guest: false,
is_bot: false,
@@ -387,7 +387,7 @@ run_test("bot_custom_profile_data", () => {
run_test("user_timezone", () => {
const expected_pref = {
- timezone: "US/Pacific",
+ timezone: "America/Los_Angeles",
format: "H:mm",
};
@@ -630,7 +630,7 @@ run_test("message_methods", () => {
value: "Field value",
},
},
- timezone: "US/Pacific",
+ timezone: "America/Los_Angeles",
user_id: 30,
},
]);
diff --git a/frontend_tests/node_tests/people_errors.js b/frontend_tests/node_tests/people_errors.js
index a96122092da..b4e12019f32 100644
--- a/frontend_tests/node_tests/people_errors.js
+++ b/frontend_tests/node_tests/people_errors.js
@@ -16,7 +16,7 @@ const me = {
user_id: 30,
full_name: "Me Myself",
- timezone: "US/Pacific",
+ timezone: "America/Los_Angeles",
};
people.init();
diff --git a/frontend_tests/node_tests/popovers.js b/frontend_tests/node_tests/popovers.js
index f38a57666ee..34649b1036e 100644
--- a/frontend_tests/node_tests/popovers.js
+++ b/frontend_tests/node_tests/popovers.js
@@ -62,7 +62,7 @@ const me = {
user_id: 30,
full_name: "Me Myself",
- timezone: "US/Pacific",
+ timezone: "America/Los_Angeles",
};
const target = $.create("click target");
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index acd84ce4dd5..8258e800317 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -142,6 +142,7 @@ from zerver.lib.streams import (
subscribed_to_stream,
)
from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
+from zerver.lib.timezone import canonicalize_timezone
from zerver.lib.topic import (
LEGACY_PREV_TOPIC,
ORIG_TOPIC,
@@ -3868,7 +3869,7 @@ def do_set_user_display_setting(user_profile: UserProfile,
if setting_name == "timezone":
payload = dict(email=user_profile.email,
user_id=user_profile.id,
- timezone=user_profile.timezone)
+ timezone=canonicalize_timezone(user_profile.timezone))
send_event(user_profile.realm,
dict(type='realm_user', op='update', person=payload),
active_user_ids(user_profile.realm_id))
diff --git a/zerver/lib/timezone.py b/zerver/lib/timezone.py
index aa67f2e216b..51391a03d86 100644
--- a/zerver/lib/timezone.py
+++ b/zerver/lib/timezone.py
@@ -1,10 +1,26 @@
import datetime
from functools import lru_cache
+from io import TextIOWrapper
from typing import Any, Dict, Union
import pytz
+@lru_cache(maxsize=None)
+def get_canonical_timezone_map() -> Dict[str, str]:
+ canonical = {}
+ with TextIOWrapper(
+ pytz.open_resource("tzdata.zi") # type: ignore[attr-defined] # Unclear if this is part of the public pytz API
+ ) as f:
+ for line in f:
+ if line.startswith("L "):
+ l, name, alias = line.split()
+ canonical[alias] = name
+ return canonical
+
+def canonicalize_timezone(key: str) -> str:
+ return get_canonical_timezone_map().get(key, key)
+
# This method carefully trims a list of common timezones in the pytz
# database and handles duplicate abbreviations in favor of the most
# common/popular offset. The output of this can be directly passed as
diff --git a/zerver/lib/users.py b/zerver/lib/users.py
index f12e445cda7..3ca1dece293 100644
--- a/zerver/lib/users.py
+++ b/zerver/lib/users.py
@@ -19,6 +19,7 @@ from zerver.lib.cache import (
)
from zerver.lib.exceptions import OrganizationAdministratorRequired
from zerver.lib.request import JsonableError
+from zerver.lib.timezone import canonicalize_timezone
from zerver.models import (
CustomProfileField,
CustomProfileFieldValue,
@@ -329,7 +330,7 @@ def format_user_row(realm: Realm, acting_user: Optional[UserProfile], row: Dict[
is_guest=is_guest,
is_bot=is_bot,
full_name=row['full_name'],
- timezone=row['timezone'],
+ timezone=canonicalize_timezone(row['timezone']),
is_active = row['is_active'],
date_joined = row['date_joined'].isoformat(),
)
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index ddb7b94c434..db8b86293ea 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -1792,7 +1792,7 @@ class UserDisplayActionTest(BaseAction):
test_changes: Dict[str, Any] = dict(
emojiset = ['twitter'],
default_language = ['es', 'de', 'en'],
- timezone = ['US/Mountain', 'US/Samoa', 'Pacific/Galapogos', ''],
+ timezone = ['America/Denver', 'Pacific/Pago_Pago', 'Pacific/Galapagos', ''],
demote_inactive_streams = [2, 3, 1],
color_scheme = [2, 3, 1]
)
diff --git a/zerver/tests/test_timezone.py b/zerver/tests/test_timezone.py
new file mode 100644
index 00000000000..772e4fcdd44
--- /dev/null
+++ b/zerver/tests/test_timezone.py
@@ -0,0 +1,9 @@
+from zerver.lib.test_classes import ZulipTestCase
+from zerver.lib.timezone import canonicalize_timezone
+
+
+class TimeZoneTest(ZulipTestCase):
+ def test_canonicalize_timezone(self) -> None:
+ self.assertEqual(canonicalize_timezone("America/Los_Angeles"), "America/Los_Angeles")
+ self.assertEqual(canonicalize_timezone("US/Pacific"), "America/Los_Angeles")
+ self.assertEqual(canonicalize_timezone("Gondor/Minas_Tirith"), "Gondor/Minas_Tirith")