From 8dd9dbb947b5fd285bf2b5d332e12302366be074 Mon Sep 17 00:00:00 2001 From: Guillaume Duveau Date: Tue, 11 Nov 2025 21:18:56 +0100 Subject: [PATCH] Fix: Handle timedelta objects in convertDateTimeToUTC() to prevent AttributeError - Add type checking for datetime and timedelta objects - Handle timedelta gracefully with error logging and fallback - Prevent crash and memory leak when timedelta is passed instead of datetime/string - Fixes AttributeError: 'datetime.timedelta' object has no attribute 'tzinfo' --- resources/lib/utilities.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/resources/lib/utilities.py b/resources/lib/utilities.py index f89ee570..d858d42d 100644 --- a/resources/lib/utilities.py +++ b/resources/lib/utilities.py @@ -8,7 +8,7 @@ import traceback from typing import Tuple import dateutil.parser -from datetime import datetime +from datetime import datetime, timedelta from dateutil.tz import tzutc, tzlocal # make strptime call prior to doing anything, to try and prevent threading @@ -215,6 +215,31 @@ def findEpisodeMatchInList(id, seasonNumber, episodeNumber, list, idType): def convertDateTimeToUTC(toConvert): if toConvert: + # Check if already a datetime object + if isinstance(toConvert, datetime): + try: + if toConvert.tzinfo is None: + local = toConvert.replace(tzinfo=tzlocal()) + else: + local = toConvert + utc = local.astimezone(tzutc()) + return str(utc) + except (ValueError, AttributeError) as e: + logger.debug( + "convertDateTimeToUTC() ValueError/AttributeError with datetime object: %s. Fallback to datetime utcnow" + % str(e) + ) + return str(datetime.utcnow()) + + # Check if it's a timedelta (should not happen, but handle gracefully) + if isinstance(toConvert, timedelta): + logger.error( + "convertDateTimeToUTC() received timedelta object instead of datetime/string: %s. This is a bug. Fallback to datetime utcnow" + % str(toConvert) + ) + return str(datetime.utcnow()) + + # Otherwise, treat as string and parse dateFormat = "%Y-%m-%d %H:%M:%S" try: naive = datetime.strptime(toConvert, dateFormat)