Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion resources/lib/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I want this to not fail hard. This would cause watched timestamps and added at timestamps to be now instead of the correct date.


# Otherwise, treat as string and parse
dateFormat = "%Y-%m-%d %H:%M:%S"
try:
naive = datetime.strptime(toConvert, dateFormat)
Expand Down