From f3c549bb248c486519e66c27285853a341146868 Mon Sep 17 00:00:00 2001 From: Matthieu Coudron Date: Thu, 4 Sep 2014 23:57:43 +0200 Subject: [PATCH 1/2] With this commit, it is possible to provide a list of formats for the clock module. You can cycle through these different formats with the mousewheel. It is backward compatible. --- i3pystatus/clock.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/i3pystatus/clock.py b/i3pystatus/clock.py index 18f05f3..bc7081e 100644 --- a/i3pystatus/clock.py +++ b/i3pystatus/clock.py @@ -14,12 +14,13 @@ class Clock(IntervalModule): """ settings = ( - ("format", "stftime format string, `None` means to use the default, locale-dependent format"), + ("format", "list of stftime format string, `None` means to use the default, locale-dependent format. Can cycle between formats with mousewheel"), ("color", "RGB hexadecimal code color specifier, default to #ffffff, set to `i3Bar` to use i3 bar default"), ) format = None color = "#ffffff" interval = 1 + currentFormatId = 0 def init(self): if self.format is None: @@ -29,15 +30,26 @@ class Clock(IntervalModule): lang = locale.getlocale()[0] if lang == 'en_US': # MDY format - United States of America - self.format = "%a %b %-d %X" + self.format = ["%a %b %-d %X"] else: # DMY format - almost all other countries - self.format = "%a %-d %b %X" + self.format = ["%a %-d %b %X"] + + elif isinstance(self.format,str): + self.format=[self.format] def run(self): self.output = { - "full_text": datetime.datetime.now().strftime(self.format), + # todo find local + "full_text": datetime.datetime.now().strftime(self.format[self.currentFormatId]), "urgent": False, } if self.color != "i3Bar": self.output["color"] = self.color + + + def on_upscroll(self): + self.currentFormatId = (self.currentFormatId + 1)% len(self.format) + + def on_downscroll(self): + self.currentFormatId = (self.currentFormatId - 1)% len(self.format) From 389124e5c4db65f3b0d26ef15e9e468f88169763 Mon Sep 17 00:00:00 2001 From: Matthieu Coudron Date: Sat, 6 Sep 2014 19:18:34 +0200 Subject: [PATCH 2/2] This commit introduces the ability for i3pystatus clock to display the time depending on different timezones. You can set the timezone as described on the data linked on http://www.iana.org/time-zones. For instance: status.register('clock', format=[("%a %X",'Europe/Dublin'),("%X",'Europe/Paris')] ) --- i3pystatus/clock.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/i3pystatus/clock.py b/i3pystatus/clock.py index bc7081e..dde491a 100644 --- a/i3pystatus/clock.py +++ b/i3pystatus/clock.py @@ -4,6 +4,7 @@ import os import locale import datetime +import pytz from i3pystatus import IntervalModule @@ -14,13 +15,13 @@ class Clock(IntervalModule): """ settings = ( - ("format", "list of stftime format string, `None` means to use the default, locale-dependent format. Can cycle between formats with mousewheel"), + ("format", "list of tuple (stftime format string, optional timezone), `None` means to use the default, locale-dependent format. Can cycle between formats with mousewheel"), ("color", "RGB hexadecimal code color specifier, default to #ffffff, set to `i3Bar` to use i3 bar default"), ) format = None color = "#ffffff" interval = 1 - currentFormatId = 0 + current_format_id = 0 def init(self): if self.format is None: @@ -35,21 +36,40 @@ class Clock(IntervalModule): # DMY format - almost all other countries self.format = ["%a %-d %b %X"] - elif isinstance(self.format,str): - self.format=[self.format] + elif isinstance(self.format, str): + self.format = [self.format] + + self.format = self.expand_formats(self.format) + + @staticmethod + def expand_formats(formats): + def expand_format(format_): + if isinstance(format_, tuple): + return (format_[0], format_[1] if len(format_) > 1 else None) + return (format_, None) + + return [expand_format(format_) for format_ in formats] def run(self): + # Safest way is to work from utc and localize afterwards + if self.format[self.current_format_id][1]: + utc_dt = pytz.utc.localize(datetime.datetime.utcnow()) + tz = pytz.timezone(self.format[self.current_format_id][1]) + dt = tz.normalize(utc_dt.astimezone(tz)) + else: + dt = datetime.datetime.now() + + output = dt.strftime(self.format[self.current_format_id][0]), + self.output = { - # todo find local - "full_text": datetime.datetime.now().strftime(self.format[self.currentFormatId]), + "full_text": output, "urgent": False, } if self.color != "i3Bar": self.output["color"] = self.color - def on_upscroll(self): - self.currentFormatId = (self.currentFormatId + 1)% len(self.format) + self.current_format_id = (self.current_format_id + 1) % len(self.format) def on_downscroll(self): - self.currentFormatId = (self.currentFormatId - 1)% len(self.format) + self.current_format_id = (self.current_format_id - 1) % len(self.format)