From c6a6041b0df0e19e6dd77e39a1a2fdd30d2ba627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5ken=20Lid?= Date: Sun, 4 Oct 2015 22:03:06 +0200 Subject: [PATCH] Allow unicode characters in time segment in Python-2 --- powerline/segments/common/time.py | 7 ++++++- tests/test_segments.py | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/powerline/segments/common/time.py b/powerline/segments/common/time.py index 84518c77..1e2207b3 100644 --- a/powerline/segments/common/time.py +++ b/powerline/segments/common/time.py @@ -16,8 +16,13 @@ def date(pl, format='%Y-%m-%d', istime=False): Highlight groups used: ``time`` or ``date``. ''' + try: + contents = datetime.now().strftime(format) + except UnicodeEncodeError: + contents = datetime.now().strftime(format.encode('utf-8')).decode('utf-8') + return [{ - 'contents': datetime.now().strftime(format), + 'contents': contents, 'highlight_groups': (['time'] if istime else []) + ['date'], 'divider_highlight_group': 'time:divider' if istime else None, }] diff --git a/tests/test_segments.py b/tests/test_segments.py index 58292909..3e24b927 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -709,6 +709,7 @@ class TestTime(TestCommon): with replace_attr(self.module, 'datetime', Args(now=lambda: Args(strftime=lambda fmt: fmt))): self.assertEqual(self.module.date(pl=pl), [{'contents': '%Y-%m-%d', 'highlight_groups': ['date'], 'divider_highlight_group': None}]) self.assertEqual(self.module.date(pl=pl, format='%H:%M', istime=True), [{'contents': '%H:%M', 'highlight_groups': ['time', 'date'], 'divider_highlight_group': 'time:divider'}]) + self.assertEqual(self.module.date(pl=pl, format='\u231a', istime=True), [{'contents': '\u231a', 'highlight_groups': ['time', 'date'], 'divider_highlight_group': 'time:divider'}]) def test_fuzzy_time(self): time = Args(hour=0, minute=45)