Merge pull request #115 from teto/scroll_time_formats

With this commit, it is possible to provide a list of formats for the
This commit is contained in:
enkore 2014-09-13 17:36:05 +02:00
commit 806ee99e93

View File

@ -4,6 +4,7 @@
import os import os
import locale import locale
import datetime import datetime
import pytz
from i3pystatus import IntervalModule from i3pystatus import IntervalModule
@ -14,12 +15,13 @@ class Clock(IntervalModule):
""" """
settings = ( settings = (
("format", "stftime format string, `None` means to use the default, locale-dependent format"), ("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"), ("color", "RGB hexadecimal code color specifier, default to #ffffff, set to `i3Bar` to use i3 bar default"),
) )
format = None format = None
color = "#ffffff" color = "#ffffff"
interval = 1 interval = 1
current_format_id = 0
def init(self): def init(self):
if self.format is None: if self.format is None:
@ -29,15 +31,45 @@ class Clock(IntervalModule):
lang = locale.getlocale()[0] lang = locale.getlocale()[0]
if lang == 'en_US': if lang == 'en_US':
# MDY format - United States of America # MDY format - United States of America
self.format = "%a %b %-d %X" self.format = ["%a %b %-d %X"]
else: else:
# DMY format - almost all other countries # 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]
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): 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 = { self.output = {
"full_text": datetime.datetime.now().strftime(self.format), "full_text": output,
"urgent": False, "urgent": False,
} }
if self.color != "i3Bar": if self.color != "i3Bar":
self.output["color"] = self.color self.output["color"] = self.color
def on_upscroll(self):
self.current_format_id = (self.current_format_id + 1) % len(self.format)
def on_downscroll(self):
self.current_format_id = (self.current_format_id - 1) % len(self.format)