Unit test for time zone

merge-requests/30/head
Bob Mottram 2022-02-25 21:00:53 +00:00
parent bacf8dece4
commit 7556ee9273
2 changed files with 29 additions and 4 deletions

View File

@ -54,6 +54,7 @@ from follow import clear_followers
from follow import send_follow_requestViaServer from follow import send_follow_requestViaServer
from follow import send_unfollow_request_via_server from follow import send_unfollow_request_via_server
from siteactive import site_is_active from siteactive import site_is_active
from utils import convert_published_to_local_timezone
from utils import convert_to_snake_case from utils import convert_to_snake_case
from utils import get_sha_256 from utils import get_sha_256
from utils import dangerous_svg from utils import dangerous_svg
@ -6628,6 +6629,26 @@ def _test_safe_webtext() -> None:
assert expected_text == safe_text 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(): def run_all_tests():
base_dir = os.getcwd() base_dir = os.getcwd()
print('Running tests...') print('Running tests...')
@ -6644,6 +6665,7 @@ def run_all_tests():
'message_json', 'liked_post_json']) 'message_json', 'liked_post_json'])
_test_checkbox_names() _test_checkbox_names()
_test_functions() _test_functions()
_test_published_to_local_timezone()
_test_safe_webtext() _test_safe_webtext()
_test_get_link_from_rss_item() _test_get_link_from_rss_item()
_test_xml_podcast_dict(base_dir) _test_xml_podcast_dict(base_dir)

View File

@ -15,7 +15,7 @@ import datetime
import json import json
import idna import idna
import locale import locale
from dateutil import tz from dateutil.tz import tz
from pprint import pprint from pprint import pprint
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes 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') from_zone = tz.gettz('UTC')
if timezone: if timezone:
to_zone = tz.gettz(timezone) try:
else: to_zone = tz.gettz(timezone)
to_zone = tz.tzlocal() except BaseException:
pass
if not timezone:
return published
utc = published.replace(tzinfo=from_zone) utc = published.replace(tzinfo=from_zone)
local_time = utc.astimezone(to_zone) local_time = utc.astimezone(to_zone)