timezone: Remove get_timezone wrapper.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-10-26 17:04:07 -07:00 committed by Tim Abbott
parent 0134112b51
commit 0b288f92c9
5 changed files with 12 additions and 17 deletions

View File

@ -4,9 +4,6 @@ from typing import Any, Dict, Union
import pytz
def get_timezone(tz: str) -> pytz.BaseTzInfo:
return pytz.timezone(tz)
# 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

View File

@ -1,5 +1,6 @@
from typing import Any, Optional
import pytz
from django.conf import settings
from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.dispatch import receiver
@ -11,7 +12,6 @@ from confirmation.models import one_click_unsubscribe_link
from zerver.lib.actions import do_set_zoom_token
from zerver.lib.queue import queue_json_publish
from zerver.lib.send_email import FromAddress
from zerver.lib.timezone import get_timezone
from zerver.models import UserProfile
JUST_CREATED_THRESHOLD = 60
@ -81,7 +81,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(get_timezone(user_tz))
local_time = timezone_now().astimezone(pytz.timezone(user_tz))
if user.twenty_four_hour_time:
hhmm_string = local_time.strftime('%H:%M')
else:

View File

@ -3,6 +3,7 @@ from typing import Any, Optional, Set
from unittest import mock
import orjson
import pytz
from django.conf import settings
from django.db.models import Q
from django.http import HttpResponse
@ -44,7 +45,6 @@ from zerver.lib.test_helpers import (
reset_emails_in_zulip_realm,
)
from zerver.lib.timestamp import convert_to_UTC, datetime_to_timestamp
from zerver.lib.timezone import get_timezone
from zerver.models import (
MAX_MESSAGE_LENGTH,
MAX_TOPIC_NAME_LENGTH,
@ -1013,7 +1013,7 @@ class ScheduledMessageTest(ZulipTestCase):
message = self.last_scheduled_message()
self.assert_json_success(result)
self.assertEqual(message.content, 'Test message 6')
local_tz = get_timezone(tz_guess)
local_tz = pytz.timezone(tz_guess)
utz_defer_until = local_tz.normalize(local_tz.localize(defer_until))
self.assertEqual(message.scheduled_timestamp,
convert_to_UTC(utz_defer_until))
@ -1029,7 +1029,7 @@ class ScheduledMessageTest(ZulipTestCase):
message = self.last_scheduled_message()
self.assert_json_success(result)
self.assertEqual(message.content, 'Test message 7')
local_tz = get_timezone(user.timezone)
local_tz = pytz.timezone(user.timezone)
utz_defer_until = local_tz.normalize(local_tz.localize(defer_until))
self.assertEqual(message.scheduled_timestamp,
convert_to_UTC(utz_defer_until))

View File

@ -1,6 +1,7 @@
import datetime
from unittest import mock
import pytz
from django.conf import settings
from django.core import mail
from django.test import override_settings
@ -8,7 +9,6 @@ from django.test import override_settings
from zerver.lib.actions import do_change_notification_settings, notify_new_user
from zerver.lib.initial_password import initial_password
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.timezone import get_timezone
from zerver.models import Realm, Recipient, Stream
from zerver.signals import JUST_CREATED_THRESHOLD, get_device_browser, get_device_os
@ -28,8 +28,7 @@ class SendLoginEmailTest(ZulipTestCase):
with self.settings(SEND_LOGIN_EMAILS=True):
self.assertTrue(settings.SEND_LOGIN_EMAILS)
# we don't use the self.login method since we spoof the user-agent
utc = get_timezone('utc')
mock_time = datetime.datetime(year=2018, month=1, day=1, tzinfo=utc)
mock_time = datetime.datetime(year=2018, month=1, day=1, tzinfo=datetime.timezone.utc)
user = self.example_user('hamlet')
user.timezone = 'US/Pacific'
@ -42,8 +41,8 @@ class SendLoginEmailTest(ZulipTestCase):
password=password,
)
firefox_windows = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"
user_tz = get_timezone(user.timezone)
mock_time = datetime.datetime(year=2018, month=1, day=1, tzinfo=utc)
user_tz = pytz.timezone(user.timezone)
mock_time = datetime.datetime(year=2018, month=1, day=1, tzinfo=datetime.timezone.utc)
reference_time = mock_time.astimezone(user_tz).strftime('%A, %B %d, %Y at %I:%M%p %Z')
with mock.patch('zerver.signals.timezone_now', return_value=mock_time):
self.client_post("/accounts/login/",
@ -96,8 +95,7 @@ class SendLoginEmailTest(ZulipTestCase):
@override_settings(SEND_LOGIN_EMAILS=True)
def test_enable_login_emails_user_setting(self) -> None:
user = self.example_user('hamlet')
utc = get_timezone('utc')
mock_time = datetime.datetime(year=2018, month=1, day=1, tzinfo=utc)
mock_time = datetime.datetime(year=2018, month=1, day=1, tzinfo=datetime.timezone.utc)
user.timezone = 'US/Pacific'
user.date_joined = mock_time - datetime.timedelta(seconds=JUST_CREATED_THRESHOLD + 1)

View File

@ -1,5 +1,6 @@
from typing import Iterable, Optional, Sequence, Union, cast
import pytz
from dateutil.parser import parse as dateparser
from django.core import validators
from django.core.exceptions import ValidationError
@ -20,7 +21,6 @@ from zerver.lib.actions import (
from zerver.lib.message import render_markdown
from zerver.lib.response import json_error, json_success
from zerver.lib.timestamp import convert_to_UTC
from zerver.lib.timezone import get_timezone
from zerver.lib.topic import REQ_topic
from zerver.lib.zcommand import process_zcommands
from zerver.lib.zephyr import compute_mit_user_fullname
@ -143,7 +143,7 @@ def handle_deferred_message(sender: UserProfile, client: Client,
deliver_at_usertz = deliver_at
if deliver_at_usertz.tzinfo is None:
user_tz = get_timezone(local_tz)
user_tz = pytz.timezone(local_tz)
deliver_at_usertz = user_tz.normalize(user_tz.localize(deliver_at))
deliver_at = convert_to_UTC(deliver_at_usertz)