Skip to content

Commit

Permalink
Merge pull request #918 from oddstr13/pr-iso-server-time-1
Browse files Browse the repository at this point in the history
Use email.utils for parsing server time
  • Loading branch information
mcarlton00 authored Dec 21, 2024
2 parents 87556da + 8d1ae2e commit 44d5ee6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion jellyfin_kodi/jellyfin/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def request(self, data, session=None):

else:
try:
self.config.data["server-time"] = r.headers["Date"]
self.config.data["server-time"] = r.headers.get("Date")
elapsed = int(r.elapsed.total_seconds() * 1000)
response = r.json()
LOG.debug("---<[ http ][%s ms]", elapsed)
Expand Down
18 changes: 11 additions & 7 deletions jellyfin_kodi/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,18 +525,22 @@ def fast_sync(self):
return True

def save_last_sync(self):
_raw_time = self.server.config.data["server-time"]

try:
time_now = datetime.strptime(
self.server.config.data["server-time"].split(", ", 1)[1],
"%d %b %Y %H:%M:%S GMT",
) - timedelta(minutes=2)
import email.utils

time_now = email.utils.parsedate_to_datetime(_raw_time)

except Exception as error:
LOG.warning(error)
LOG.warning("Failed to parse server time, falling back to client time.")
time_now = datetime.utcnow()

LOG.exception(error)
time_now = datetime.utcnow() - timedelta(minutes=2)
# Add some tolerance in case time is out of sync with server
time_now -= timedelta(minutes=2)

last_sync = time_now.strftime("%Y-%m-%dT%H:%M:%Sz")
last_sync = time_now.strftime("%Y-%m-%dT%H:%M:%SZ")
settings("LastIncrementalSync", value=last_sync)
LOG.info("--[ sync/%s ]", last_sync)

Expand Down

0 comments on commit 44d5ee6

Please sign in to comment.