diff --git a/.travis.yml b/.travis.yml index 2e763fb..39c71a6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,8 @@ install: - pip install -U coverage==4.3 - pip install codeclimate-test-reporter - pip install taskw + - pip install pytz + - pip install tzlocal script: - nosetests -v --with-coverage --cover-erase tests/ - CODECLIMATE_REPO_TOKEN=40cb00907f7a10e04868e856570bb997ab9c42fd3b63d980f2b2269433195fdf codeclimate-test-reporter diff --git a/PKGBUILD b/PKGBUILD index 15d0e43..4e6b57d 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -20,7 +20,9 @@ optdepends=('xorg-xbacklight: to display a displays brightness' 'pacman: display current status of pacman' 'iputils: display a ping' 'i3ipc-python: display titlebar' - 'fakeroot: dependency of the pacman module') + 'fakeroot: dependency of the pacman module' + 'pytz: timezone conversion for datetimetz module' + 'tzlocal: retrieve system timezone for datetimetz module') source=("$pkgname-$pkgver.tar.gz::$url/archive/v$pkgver.tar.gz") sha512sums=('3a66fc469dd3b081337c9e213a1b2262f25f30977ee6ef65b9fa5a8b6aa341637832d1a5dbb74e30d68e2824e0d19d7a911eb3390dc6062707a552f429b483e8') diff --git a/README.md b/README.md index 652f463..dffe0e3 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,8 @@ Modules and commandline utilities are only required for modules, the core itself * i3ipc (for the module 'title') * pacman-contrib (for module 'arch-update') * docker (for the module 'docker_ps') +* pytz (for the module 'datetimetz') +* localtz (for the module 'datetimetz') # Required commandline utilities diff --git a/bumblebee/modules/datetimetz.py b/bumblebee/modules/datetimetz.py new file mode 100644 index 0000000..e546ace --- /dev/null +++ b/bumblebee/modules/datetimetz.py @@ -0,0 +1,79 @@ +# pylint: disable=C0111,R0903 + +"""Displays the current date and time with timezone options. + +Parameters: + * datetimetz.format : strftime()-compatible formatting string + * datetimetz.timezone : IANA timezone name + * datetz.format : alias for datetimetz.format + * timetz.format : alias for datetimetz.format + * timetz.timezone : alias for datetimetz.timezone + * datetimetz.locale : locale to use rather than the system default + * datetz.locale : alias for datetimetz.locale + * timetz.locale : alias for datetimetz.locale + * timetz.timezone : alias for datetimetz.timezone +""" + +from __future__ import absolute_import +import datetime +import locale +import pytz +import tzlocal +import bumblebee.input +import bumblebee.output +import bumblebee.engine + +ALIASES = ["datetz", "timetz"] + +def default_format(module): + default = "%x %X %Z" + if module == "datetz": + default = "%x %Z" + if module == "timetz": + default = "%X %Z" + return default + +class Module(bumblebee.engine.Module): + def __init__(self, engine, config): + super(Module, self).__init__(engine, config, + bumblebee.output.Widget(full_text=self.get_time)) + engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd=self.next_tz) + engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE, cmd=self.prev_tz) + self._fmt = self.parameter("format", default_format(self.name)) + self._timezones = self.parameter("timezone", tzlocal.get_localzone().zone).split(",") + self._current_tz = 0 + + l = locale.getdefaultlocale() + if not l or l == (None, None): + l = ('en_US', 'UTF-8') + lcl = self.parameter("locale", ".".join(l)) + try: + locale.setlocale(locale.LC_TIME, lcl.split(".")) + except Exception: + locale.setlocale(locale.LC_TIME, ('en_US', 'UTF-8')) + + def get_time(self, widget): + try: + tz = pytz.timezone(self._timezones[self._current_tz].strip()) + retval = datetime.datetime.now(tz=tzlocal.get_localzone()).astimezone(tz).strftime(self._fmt) + except pytz.exceptions.UnknownTimeZoneError: + retval = "[Unknown timezone: {}]".format(self._timezones[self._current_tz].strip()) + + enc = locale.getpreferredencoding() + if hasattr(retval, "decode"): + return retval.decode(enc) + return retval + + def next_tz(self, event): + next_timezone = self._current_tz + 1 + if next_timezone >= len(self._timezones): + next_timezone = 0 # wraparound + self._current_tz = next_timezone + + def prev_tz(self, event): + previous_timezone = self._current_tz - 1 + if previous_timezone < 0: + previous_timezone = 0 # wraparound + self._current_tz = previous_timezone + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/screenshots/datetimetz.gif b/screenshots/datetimetz.gif new file mode 100644 index 0000000..ae926eb Binary files /dev/null and b/screenshots/datetimetz.gif differ diff --git a/themes/icons/awesome-fonts.json b/themes/icons/awesome-fonts.json index daa370c..eb4a623 100644 --- a/themes/icons/awesome-fonts.json +++ b/themes/icons/awesome-fonts.json @@ -6,6 +6,9 @@ "date": { "prefix": "" }, "time": { "prefix": "" }, "datetime": { "prefix": "" }, + "datetz": { "prefix": "" }, + "timetz": { "prefix": "" }, + "datetimetz": { "prefix": "" }, "memory": { "prefix": "" }, "cpu": { "prefix": "" }, "disk": { "prefix": "" },