diff --git a/src/calibre/devices/prs505/sony_cache.py b/src/calibre/devices/prs505/sony_cache.py index 5f3e9118dc..b5a8f08541 100644 --- a/src/calibre/devices/prs505/sony_cache.py +++ b/src/calibre/devices/prs505/sony_cache.py @@ -354,19 +354,22 @@ class XMLCache(object): root = self.record_roots[i] lpath_map = self.build_lpath_map(root) gtz_count = ltz_count = 0 + use_tz_var = False for book in booklist: path = os.path.join(self.prefixes[i], *(book.lpath.split('/'))) record = lpath_map.get(book.lpath, None) if record is None: record = self.create_text_record(root, i, book.lpath) - (gtz_count, ltz_count) = self.update_text_record(record, book, - path, i, gtz_count, ltz_count) + (gtz_count, ltz_count, use_tz_var) = \ + self.update_text_record(record, book, path, i, + gtz_count, ltz_count, use_tz_var) # Ensure the collections in the XML database are recorded for # this book if book.device_collections is None: book.device_collections = [] book.device_collections = playlist_map.get(book.lpath, []) - debug_print('Timezone votes: %d GMT, %d LTZ'%(gtz_count, ltz_count)) + debug_print('Timezone votes: %d GMT, %d LTZ, use_tz_var='% + (gtz_count, ltz_count, use_tz_var)) self.update_playlists(i, root, booklist, collections_attributes) # Update the device collections because update playlist could have added # some new ones. @@ -464,21 +467,27 @@ class XMLCache(object): root.append(ans) return ans - def update_text_record(self, record, book, path, bl_index, gtz_count, ltz_count): + def update_text_record(self, record, book, path, bl_index, + gtz_count, ltz_count, use_tz_var): ''' 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, - # and apparently converts the dates to GMT when it writes them to the - # db. Unfortunately, we can't tell when it does this, so we use a - # horrible heuristic. First, set dates only for new books, trying to - # avoid upsetting the sony. Use the timezone determined through the - # voting described next. Second, voting: if a book is not new, compare - # its Sony DB date against localtime and gmtime. Count the matches. When - # we must set a date, use the one with the most matches. Use localtime - # if the case of a tie, and hope it is right. + # and apparently converts the dates to GMT when it writes them to its + # DB. We can detect that a device is timezone-aware because there is a + # 'tz' variable in the Sony DB, which we can set to "0" to tell the + # device to ignore its own timezone when comparing mtime to the date in + # the DB. + + # Unfortunately, if there is no tz variable in the DB, then we can't + # tell when the device applies a timezone conversion. We use a horrible + # heuristic to work around this problem. First, set dates only for new + # books, trying to avoid upsetting the sony. Second, voting: if a book + # is not new, compare its Sony DB date against localtime and gmtime. + # Count the matches. When we must set a date, use the one with the most + # matches. Use localtime if the case of a tie, and hope it is right. timestamp = os.path.getmtime(path) rec_date = record.get('date', None) @@ -489,20 +498,25 @@ class XMLCache(object): return x if not getattr(book, '_new_book', False): # book is not new - if strftime(timestamp, zone=time.gmtime) == rec_date: - gtz_count += 1 - elif strftime(timestamp, zone=time.localtime) == rec_date: - ltz_count += 1 + if record.get('tz', None) is not None: + use_tz_var = True + if strftime(timestamp, zone=time.gmtime) == rec_date: + gtz_count += 1 + elif strftime(timestamp, zone=time.localtime) == rec_date: + ltz_count += 1 else: # book is new. Set the time using the current votes - if ltz_count >= gtz_count: + if use_tz_var: tz = time.localtime - debug_print("Using localtime TZ for new book", book.lpath) + record.set('tz', '0') + debug_print("Use localtime TZ and tz='0' for new book", book.lpath) + elif ltz_count >= gtz_count: + tz = time.localtime + debug_print("Use localtime TZ for new book", book.lpath) else: tz = time.gmtime - debug_print("Using GMT TZ for new book", book.lpath) + debug_print("Use GMT TZ for new book", book.lpath) date = strftime(timestamp, zone=tz) record.set('date', clean(date)) - record.set('size', clean(str(os.stat(path).st_size))) title = book.title if book.title else _('Unknown') record.set('title', clean(title)) @@ -532,7 +546,7 @@ class XMLCache(object): if 'id' not in record.attrib: num = self.max_id(record.getroottree().getroot()) record.set('id', str(num+1)) - return (gtz_count, ltz_count) + return (gtz_count, ltz_count, use_tz_var) # }}} # Writing the XML files {{{