From 59b05484338c6ac132fe953e3daccd8778d9613d Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 27 Feb 2024 16:36:17 -0800 Subject: [PATCH] timezone: Only look up canonical time zones from the system. Legacy time zone aliases were removed from the Debian tzdata package in tzdata 2023c-8. https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1040997 Signed-off-by: Anders Kaseorg --- zerver/lib/email_notifications.py | 5 ++++- zerver/lib/validator.py | 5 +++-- zerver/signals.py | 3 ++- zerver/tests/test_new_users.py | 3 ++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/zerver/lib/email_notifications.py b/zerver/lib/email_notifications.py index 4222a49855..7b8b229779 100644 --- a/zerver/lib/email_notifications.py +++ b/zerver/lib/email_notifications.py @@ -29,6 +29,7 @@ from zerver.lib.queue import queue_json_publish from zerver.lib.send_email import FromAddress, send_future_email from zerver.lib.soft_deactivation import soft_reactivate_if_personal_notification from zerver.lib.tex import change_katex_to_raw_latex +from zerver.lib.timezone import canonicalize_timezone from zerver.lib.topic import get_topic_resolution_and_bare_name from zerver.lib.url_encoding import ( huddle_narrow_url, @@ -686,7 +687,9 @@ def get_onboarding_email_schedule(user: UserProfile) -> Dict[str, timedelta]: user_tz = user.timezone if user_tz == "": user_tz = "UTC" - signup_day = user.date_joined.astimezone(zoneinfo.ZoneInfo(user_tz)).isoweekday() + signup_day = user.date_joined.astimezone( + zoneinfo.ZoneInfo(canonicalize_timezone(user_tz)) + ).isoweekday() # General rules for scheduling welcome emails flow: # -Do not send emails on Saturday or Sunday diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py index 5c69bc3e25..16200df654 100644 --- a/zerver/lib/validator.py +++ b/zerver/lib/validator.py @@ -140,7 +140,7 @@ def check_long_string(var_name: str, val: object) -> str: def check_timezone(var_name: str, val: object) -> str: s = check_string(var_name, val) try: - zoneinfo.ZoneInfo(s) + zoneinfo.ZoneInfo(canonicalize_timezone(s)) except (ValueError, zoneinfo.ZoneInfoNotFoundError): raise ValidationError( _("{var_name} is not a recognized time zone").format(var_name=var_name) @@ -603,11 +603,12 @@ def to_decimal(var_name: str, s: str) -> Decimal: def to_timezone_or_empty(var_name: str, s: str) -> str: try: + s = canonicalize_timezone(s) zoneinfo.ZoneInfo(s) except (ValueError, zoneinfo.ZoneInfoNotFoundError): return "" else: - return canonicalize_timezone(s) + return s def to_converted_or_fallback( diff --git a/zerver/signals.py b/zerver/signals.py index ac345d58b1..7418bc4b76 100644 --- a/zerver/signals.py +++ b/zerver/signals.py @@ -11,6 +11,7 @@ from django.utils.translation import gettext as _ from confirmation.models import one_click_unsubscribe_link from zerver.lib.queue import queue_json_publish from zerver.lib.send_email import FromAddress +from zerver.lib.timezone import canonicalize_timezone from zerver.models import UserProfile if sys.version_info < (3, 9): # nocoverage @@ -96,7 +97,7 @@ def email_on_new_login(sender: Any, user: UserProfile, request: Any, **kwargs: A user_tz = user.timezone if user_tz == "": user_tz = timezone_get_current_timezone_name() - local_time = timezone_now().astimezone(zoneinfo.ZoneInfo(user_tz)) + local_time = timezone_now().astimezone(zoneinfo.ZoneInfo(canonicalize_timezone(user_tz))) if user.twenty_four_hour_time: hhmm_string = local_time.strftime("%H:%M") else: diff --git a/zerver/tests/test_new_users.py b/zerver/tests/test_new_users.py index d1f031233f..afc337f5cb 100644 --- a/zerver/tests/test_new_users.py +++ b/zerver/tests/test_new_users.py @@ -13,6 +13,7 @@ from zerver.actions.create_user import notify_new_user from zerver.actions.user_settings import do_change_user_setting from zerver.lib.initial_password import initial_password from zerver.lib.test_classes import ZulipTestCase +from zerver.lib.timezone import canonicalize_timezone from zerver.models import Message, Realm, Recipient, Stream, UserProfile from zerver.models.realms import get_realm from zerver.signals import JUST_CREATED_THRESHOLD, get_device_browser, get_device_os @@ -53,7 +54,7 @@ class SendLoginEmailTest(ZulipTestCase): firefox_windows = ( "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0" ) - user_tz = zoneinfo.ZoneInfo(user.timezone) + user_tz = zoneinfo.ZoneInfo(canonicalize_timezone(user.timezone)) mock_time = datetime(year=2018, month=1, day=1, tzinfo=timezone.utc) reference_time = mock_time.astimezone(user_tz).strftime("%A, %B %d, %Y at %I:%M %p %Z") with time_machine.travel(mock_time, tick=False):