Fix datetime returning UTC (#1262)

* Replace datetime.utcfromtimestamp

* Update plexapi/utils.py

Co-authored-by: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com>

* Default using `fromtimestamp` and fallback to using `timedelta` from 0

---------

Co-authored-by: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com>
This commit is contained in:
Marc Mueller 2023-10-04 05:30:32 +02:00 committed by GitHub
parent d5cad1fe1c
commit dcedde1947
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -323,13 +323,18 @@ def toDatetime(value, format=None):
return None
else:
try:
return datetime.utcfromtimestamp(0) + timedelta(seconds=int(value))
value = int(value)
except ValueError:
log.info('Failed to parse "%s" to datetime as timestamp, defaulting to None', value)
return None
except OverflowError:
log.info('Failed to parse "%s" to datetime as timestamp (out-of-bounds), defaulting to None', value)
return None
try:
return datetime.fromtimestamp(value)
except (OSError, OverflowError):
try:
return datetime.fromtimestamp(0) + timedelta(seconds=value)
except OverflowError:
log.info('Failed to parse "%s" to datetime as timestamp (out-of-bounds), defaulting to None', value)
return None
return value