From 43f576d1c58a2f78f302153899a3d40fca8a4aad Mon Sep 17 00:00:00 2001 From: mcarlton00 Date: Sat, 30 Apr 2022 10:34:47 -0400 Subject: [PATCH] Fix browsing the Live TV programs menu (#145) * Convert server times to local timezone * Fix naming format of programs * Simplify string formatting * Remove unneeded space --- resources/lib/item_functions.py | 14 +++++++++----- resources/lib/utils.py | 17 +++++++++++++++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/resources/lib/item_functions.py b/resources/lib/item_functions.py index 7561962..f6b217c 100644 --- a/resources/lib/item_functions.py +++ b/resources/lib/item_functions.py @@ -404,7 +404,7 @@ def add_gui_item(url, item_details, display_options, folder=True, default_sort=F end_time = datetime_from_string(item_details.program_end_date) duration = (end_time - start_time).total_seconds() - time_done = (datetime.now() - start_time).total_seconds() + time_done = (datetime.now().astimezone() - start_time).total_seconds() percentage_done = (float(time_done) / float(duration)) * 100.0 capped_percentage = int(percentage_done) @@ -414,10 +414,14 @@ def add_gui_item(url, item_details, display_options, folder=True, default_sort=F item_details.duration = int(duration) item_details.resume_time = int(time_done) - list_item_name = (item_details.program_channel_name + - " - " + list_item_name + - " - " + start_time_string + " to " + end_time_string + - " (" + str(int(percentage_done)) + "%)") + if item_details.program_channel_name: + list_item_name = '{} - {} - {} to {} ({}%)'.format( + item_details.program_channel_name, list_item_name, + start_time_string, end_time_string, capped_percentage) + else: + list_item_name = '{} - {} to {} ({}%)'.format( + list_item_name, start_time_string, end_time_string, + capped_percentage) time_info = "Start : " + start_time_string + "\n" time_info += "End : " + end_time_string + "\n" diff --git a/resources/lib/utils.py b/resources/lib/utils.py index 86a5a79..ac021ab 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -16,6 +16,8 @@ import math import os import hashlib from datetime import datetime +from dateutil import tz +import time import re from uuid import uuid4 from six import ensure_text, ensure_binary, text_type @@ -97,9 +99,20 @@ def datetime_from_string(time_string): elif time_string[-6:] == "+00:00": time_string = re.sub("[0-9]{1}\+00:00", " UTC", time_string) - dt = datetime.strptime(time_string, "%Y-%m-%dT%H:%M:%S.%f %Z") + try: + dt = datetime.strptime(time_string, "%Y-%m-%dT%H:%M:%S.%f %Z") + except TypeError: + # https://bugs.python.org/issue27400 + dt = datetime(*(time.strptime(time_string, "%Y-%m-%dT%H:%M:%S.%f %Z")[0:6])) - return dt + # Convert server dates from UTC to local time + utc = tz.tzutc() + local = tz.tzlocal() + + utc_dt = dt.replace(tzinfo=utc) + local_dt = utc_dt.astimezone(local) + + return local_dt def convert_size(size_bytes):