Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix datetime returning UTC #1262

Merged
merged 3 commits into from Oct 4, 2023
Merged

Fix datetime returning UTC #1262

merged 3 commits into from Oct 4, 2023

Conversation

cdce8p
Copy link
Contributor

@cdce8p cdce8p commented Oct 2, 2023

Description

datetime.utcfromtimestamp will emit a DeprecationWarning in Python 3.12.

The "official" replacement would be to use fromtimestamp(tz=timezone.utc).replace(tzinfo=None). However in this particular case datetime(1970, 1, 1) seems to be the easier solution.

https://docs.python.org/3.12/library/datetime.html#datetime.datetime.utcfromtimestamp

plexapi/utils.py Outdated Show resolved Hide resolved
Co-authored-by: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com>
@simonc56
Copy link
Contributor

simonc56 commented Oct 3, 2023

Doesn't seem to be the same either.

>>> datetime.datetime.utcfromtimestamp(0) + datetime.timedelta(seconds=1593721093)
datetime.datetime(2020, 7, 2, 20, 18, 13)
>>> datetime.datetime.fromtimestamp(1593721093)
datetime.datetime(2020, 7, 2, 22, 18, 13)
>>> datetime.datetime.fromtimestamp(0) + datetime.timedelta(seconds=1593721093)
datetime.datetime(2020, 7, 2, 21, 18, 13)

plexapi/utils.py Outdated Show resolved Hide resolved
@JonnyWong16
Copy link
Collaborator

I think that should work now.

@JonnyWong16 JonnyWong16 changed the title Replace datetime.utcfromtimestamp Fix datetime returning UTC Oct 4, 2023
@JonnyWong16 JonnyWong16 merged commit dcedde1 into pkkid:master Oct 4, 2023
5 checks passed
@cdce8p cdce8p deleted the replace-utcfromtimestamp branch October 4, 2023 03:32
@meisnate12
Copy link
Contributor

I've got a user getting an issue that i think may be related to this PR. The plex instance seems to have some weird addedAt dates and causing ValueError: year 30828 is out of range to be thrown. I'm trying to get the user to fix the metadata in their DB, but they mentioned that it was working fine on the latest PMM which uses PlexAPI v4.15.2 but when switched to develop using v4.15.6 he would see this issue. I can open a new issue if needed but thought id post on this PR first.

LibrarySection.findItems(data, initpath=key)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/plexapi/base.py", line 329, in findItems
  item = self._buildItemOrNone(elem, cls, initpath)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/plexapi/base.py", line 109, in _buildItemOrNone
  return self._buildItem(elem, cls, initpath)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/plexapi/base.py", line 101, in _buildItem
  return ecls(self._server, elem, initpath)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/plexapi/base.py", line 62, in __init__
  self._loadData(data)
File "/usr/local/lib/python3.11/site-packages/plexapi/video.py", line 363, in _loadData
  Video._loadData(self, data)
File "/usr/local/lib/python3.11/site-packages/plexapi/video.py", line 51, in _loadData
  self.addedAt = utils.toDatetime(data.attrib.get('addedAt'))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/plexapi/utils.py", line 331, in toDatetime
  return datetime.fromtimestamp(value)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: year 30828 is out of range

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants