Merge pull request #918 from oddstr13/pr-iso-server-time-1

Use email.utils for parsing server time
This commit is contained in:
mcarlton00 2024-12-21 14:11:51 -05:00 committed by GitHub
commit 44d5ee69d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 8 deletions

View file

@ -179,7 +179,7 @@ class HTTP(object):
else: else:
try: 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) elapsed = int(r.elapsed.total_seconds() * 1000)
response = r.json() response = r.json()
LOG.debug("---<[ http ][%s ms]", elapsed) LOG.debug("---<[ http ][%s ms]", elapsed)

View file

@ -525,18 +525,22 @@ class Library(threading.Thread):
return True return True
def save_last_sync(self): def save_last_sync(self):
_raw_time = self.server.config.data["server-time"]
try: try:
time_now = datetime.strptime( import email.utils
self.server.config.data["server-time"].split(", ", 1)[1],
"%d %b %Y %H:%M:%S GMT", time_now = email.utils.parsedate_to_datetime(_raw_time)
) - timedelta(minutes=2)
except Exception as error: 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) # Add some tolerance in case time is out of sync with server
time_now = datetime.utcnow() - timedelta(minutes=2) 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) settings("LastIncrementalSync", value=last_sync)
LOG.info("--[ sync/%s ]", last_sync) LOG.info("--[ sync/%s ]", last_sync)