From d2670e18e35eb4b512d676ed1b73733de00a9beb Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 14 Feb 2018 02:35:47 +0530 Subject: [PATCH] Fix errors when using strftime with unicode strings on non-utf-8 windows systems. Fixes #1749219 [conversion error - failure to fetch news from nyt](https://bugs.launchpad.net/calibre/+bug/1749219) --- src/calibre/__init__.py | 2 +- src/calibre/web/feeds/templates.py | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/calibre/__init__.py b/src/calibre/__init__.py index d96f48f9a5..f105db8879 100644 --- a/src/calibre/__init__.py +++ b/src/calibre/__init__.py @@ -544,7 +544,7 @@ def strftime(fmt, t=None): ans = None if iswindows: if isinstance(fmt, unicode): - fmt = fmt.encode('mbcs') + fmt = fmt.encode('mbcs', 'replace') fmt = fmt.replace(b'%e', b'%#d') ans = plugins['winutil'][0].strftime(fmt, t) else: diff --git a/src/calibre/web/feeds/templates.py b/src/calibre/web/feeds/templates.py index 3ed689fe59..a3fe823333 100644 --- a/src/calibre/web/feeds/templates.py +++ b/src/calibre/web/feeds/templates.py @@ -11,7 +11,7 @@ from lxml.html.builder import HTML, HEAD, TITLE, STYLE, DIV, BODY, \ STRONG, BR, SPAN, A, HR, UL, LI, H2, H3, IMG, P as PT, \ TABLE, TD, TR -from calibre import preferred_encoding, strftime, isbytestring +from calibre import strftime, isbytestring def CLASS(*args, **kwargs): # class is a reserved word in Python @@ -88,8 +88,6 @@ class IndexTemplate(Template): def _generate(self, title, masthead, datefmt, feeds, extra_css=None, style=None): self.IS_HTML = False - if isinstance(datefmt, unicode): - datefmt = datefmt.encode(preferred_encoding) date = strftime(datefmt) head = HEAD(TITLE(title)) if style: @@ -245,11 +243,7 @@ class NavBarTemplate(Template): class TouchscreenIndexTemplate(Template): def _generate(self, title, masthead, datefmt, feeds, extra_css=None, style=None): - self.IS_HTML = False - - if isinstance(datefmt, unicode): - datefmt = datefmt.encode(preferred_encoding) date = '%s, %s %s, %s' % (strftime('%A'), strftime('%B'), strftime('%d').lstrip('0'), strftime('%Y')) masthead_p = etree.Element("p") masthead_p.set("style","text-align:center")