diff --git a/tests.py b/tests.py index b0a21d116..1236e1429 100644 --- a/tests.py +++ b/tests.py @@ -54,6 +54,7 @@ from follow import clear_followers from follow import send_follow_requestViaServer from follow import send_unfollow_request_via_server from siteactive import site_is_active +from utils import convert_published_to_local_timezone from utils import convert_to_snake_case from utils import get_sha_256 from utils import dangerous_svg @@ -6628,6 +6629,26 @@ def _test_safe_webtext() -> None: assert expected_text == safe_text +def _test_published_to_local_timezone() -> None: + print('published_to_local_timezone') + published_str = '2022-02-25T20:15:00Z' + timezone = 'Europe/Berlin' + published = \ + datetime.datetime.strptime(published_str, "%Y-%m-%dT%H:%M:%SZ") + datetime_object = \ + convert_published_to_local_timezone(published, timezone) + local_time_str = datetime_object.strftime("%a %b %d, %H:%M") + assert local_time_str == 'Fri Feb 25, 21:15' + + timezone = 'Asia/Seoul' + published = \ + datetime.datetime.strptime(published_str, "%Y-%m-%dT%H:%M:%SZ") + datetime_object = \ + convert_published_to_local_timezone(published, timezone) + local_time_str = datetime_object.strftime("%a %b %d, %H:%M") + assert local_time_str == 'Sat Feb 26, 05:15' + + def run_all_tests(): base_dir = os.getcwd() print('Running tests...') @@ -6644,6 +6665,7 @@ def run_all_tests(): 'message_json', 'liked_post_json']) _test_checkbox_names() _test_functions() + _test_published_to_local_timezone() _test_safe_webtext() _test_get_link_from_rss_item() _test_xml_podcast_dict(base_dir) diff --git a/utils.py b/utils.py index 0f53ee811..968790d1a 100644 --- a/utils.py +++ b/utils.py @@ -15,7 +15,7 @@ import datetime import json import idna import locale -from dateutil import tz +from dateutil.tz import tz from pprint import pprint from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes @@ -3358,9 +3358,12 @@ def convert_published_to_local_timezone(published, timezone: str) -> str: """ from_zone = tz.gettz('UTC') if timezone: - to_zone = tz.gettz(timezone) - else: - to_zone = tz.tzlocal() + try: + to_zone = tz.gettz(timezone) + except BaseException: + pass + if not timezone: + return published utc = published.replace(tzinfo=from_zone) local_time = utc.astimezone(to_zone)