From 0c8e1e20586c8321e237c797494a6a1840eaf414 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sat, 3 Jul 2010 19:19:12 +0100 Subject: [PATCH] Implement timezone heuristic hack for sony devices --- src/calibre/devices/prs505/sony_cache.py | 33 ++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/calibre/devices/prs505/sony_cache.py b/src/calibre/devices/prs505/sony_cache.py index 181a18bcf9..43832bca2b 100644 --- a/src/calibre/devices/prs505/sony_cache.py +++ b/src/calibre/devices/prs505/sony_cache.py @@ -345,6 +345,7 @@ class XMLCache(object): debug_print('Updating XML Cache:', i) root = self.record_roots[i] lpath_map = self.build_lpath_map(root) + self.timezone_for_dates = None for book in booklist: path = os.path.join(self.prefixes[i], *(book.lpath.split('/'))) record = lpath_map.get(book.lpath, None) @@ -458,10 +459,38 @@ class XMLCache(object): Update the Sony database from the book. This is done if the timestamp in the db differs from the timestamp on the file. ''' + + # It seems that a Sony device can sometimes know what timezone it is in. + # In this case it appears to convert the dates to GMT when it writes + # them to the db. Unfortunately, we can't tell when it is doing this, so + # we use a horrible heuristic to tell. As we check dates, check both + # localtime and gmtime. If neither match, set the date using localtime + # and hope it is right. If one of them matches, then assume that the + # rest of the dates in that db use that timezone. timestamp = os.path.getmtime(path) - date = strftime(timestamp) - if date != record.get('date', None): + if self.timezone_for_dates is None: + tz = time.localtime + else: + tz = self.timezone_for_dates + date = strftime(timestamp, zone=tz) + rec_date = record.get('date', None) + if date != rec_date: + if self.timezone_for_dates is None: + # We haven't yet identified a timezone. See if gmtime() matches + date = strftime(timestamp, zone=time.gmtime) + if date == rec_date: + # It did. Use gmtime for the rest of the dates. + debug_print("Using GMT TZ for dates") + self.timezone_for_dates = time.gmtime + # We now may or may not have identified a timezone. In either event, + # save the date. If gmtime matched, we are modifying it to itself, + # but that is OK for the one time it happens. record.set('date', date) + elif self.timezone_for_dates is None: + # Dates matched. Use localtime from here on. + debug_print("Using localtime TZ for dates") + self.timezone_for_dates = tz + record.set('size', str(os.stat(path).st_size)) title = book.title if book.title else _('Unknown') record.set('title', title)