Skip to content

Commit

Permalink
Merge pull request #273 from LinusT/master
Browse files Browse the repository at this point in the history
Residual fix for ValueError: (22, 'Invalid argument') issue
  • Loading branch information
razzeee committed Mar 3, 2016
2 parents 0df4d1c + 9c5ec32 commit 39e1662
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,25 +482,28 @@ 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

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

Expand Down

0 comments on commit 39e1662

Please sign in to comment.