From 403dc578537192239336e53643a8b3bf722fa3ee Mon Sep 17 00:00:00 2001 From: Linus Date: Thu, 3 Mar 2016 15:33:41 +0800 Subject: [PATCH 1/2] Update utilities.py Addressed ValueError / date outside of unix timespan conversion by wrapping in try-except block. Fixes test case when toConvert is on the very edge of unix timespan and ends up outside after conversion to utc/local --- utilities.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/utilities.py b/utilities.py index d25771ac..ca460f6b 100644 --- a/utilities.py +++ b/utilities.py @@ -482,11 +482,13 @@ def convertDateTimeToUTC(toConvert): dateFormat = "%Y-%m-%d %H:%M:%S" try: naive = datetime.strptime(toConvert, dateFormat) except TypeError: naive = datetime(*(time.strptime(toConvert, dateFormat)[0:6])) - if naive.year < 1970 or naive.year > 2038: - logger.debug('convertDateTimeToUTC() Movie/show was collected/watched outside of the unix timespan. Fallback to datetime now') - naive = datetime.now() - local = naive.replace(tzinfo=tzlocal()) - utc = local.astimezone(tzutc()) + + try: + local = naive.replace(tzinfo=tzlocal()) + utc = local.astimezone(tzutc()) + except ValueError: + logger.debug('convertDateTimeToUTC() ValueError: movie/show was collected/watched outside of the unix timespan. Fallback to datetime utcnow') + utc = datetime.utcnow() return unicode(utc) else: return toConvert @@ -494,13 +496,14 @@ def convertDateTimeToUTC(toConvert): def convertUtcToDateTime(toConvert): if toConvert: dateFormat = "%Y-%m-%d %H:%M:%S" - naive = dateutil.parser.parse(toConvert) - if naive.year < 1970 or naive.year > 2038: - logger.debug('convertUtcToDateTime() Movie/show was collected/watched outside of the unix timespan. Fallback to datetime now') - naive = datetime.now() - utc = naive.replace(tzinfo=tzutc()) - local = utc.astimezone(tzlocal()) - return local.strftime(dateFormat) + try: + naive = dateutil.parser.parse(toConvert) + utc = naive.replace(tzinfo=tzutc()) + local = utc.astimezone(tzlocal()) + except ValueError: + logger.debug('convertUtcToDateTime() ValueError: movie/show was collected/watched outside of the unix timespan. Fallback to datetime now') + local = datetime.now() + return local.strftime(dateFormat) else: return toConvert From 9c5ec32450dd36968b8d398f14a240ea7477196a Mon Sep 17 00:00:00 2001 From: Linus Date: Thu, 3 Mar 2016 15:40:50 +0800 Subject: [PATCH 2/2] Update utilities.py Indent fix --- utilities.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utilities.py b/utilities.py index ca460f6b..2fb33869 100644 --- a/utilities.py +++ b/utilities.py @@ -501,8 +501,8 @@ def convertUtcToDateTime(toConvert): utc = naive.replace(tzinfo=tzutc()) local = utc.astimezone(tzlocal()) except ValueError: - logger.debug('convertUtcToDateTime() ValueError: movie/show was collected/watched outside of the unix timespan. Fallback to datetime now') - local = datetime.now() + logger.debug('convertUtcToDateTime() ValueError: movie/show was collected/watched outside of the unix timespan. Fallback to datetime now') + local = datetime.now() return local.strftime(dateFormat) else: return toConvert