2020-04-25 10:40:50 +02:00
|
|
|
# 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 logging
|
2020-04-25 10:48:36 +02:00
|
|
|
import pytz
|
|
|
|
import tzlocal
|
|
|
|
|
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
|
|
|
|
|
|
|
import util.format
|
2020-04-25 10:40:50 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-25 10:40:50 +02:00
|
|
|
def default_format(module):
|
2020-05-03 11:15:52 +02:00
|
|
|
default = "%x %X %Z"
|
|
|
|
if module == "datetz":
|
|
|
|
default = "%x %Z"
|
|
|
|
if module == "timetz":
|
|
|
|
default = "%X %Z"
|
2020-04-25 10:40:50 +02:00
|
|
|
return default
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-25 10:48:36 +02:00
|
|
|
class Module(core.module.Module):
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.get_time))
|
2020-04-25 10:48:36 +02:00
|
|
|
|
|
|
|
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.next_tz)
|
|
|
|
core.input.register(self, button=core.input.RIGHT_MOUSE, cmd=self.prev_tz)
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__fmt = self.parameter("format", self.default_format())
|
2020-04-25 10:48:36 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
default_timezone = ""
|
2020-04-25 10:40:50 +02:00
|
|
|
try:
|
|
|
|
default_timezone = tzlocal.get_localzone().zone
|
|
|
|
except Exception as e:
|
2020-05-03 11:15:52 +02:00
|
|
|
logging.error("unable to get default timezone: {}".format(str(e)))
|
2020-04-25 10:40:50 +02:00
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
self._timezones = util.format.aslist(
|
|
|
|
self.parameter("timezone", default_timezone)
|
|
|
|
)
|
2020-04-25 10:40:50 +02:00
|
|
|
except:
|
|
|
|
self._timezones = [default_timezone]
|
|
|
|
self._current_tz = 0
|
|
|
|
|
|
|
|
l = locale.getdefaultlocale()
|
|
|
|
if not l or l == (None, None):
|
2020-05-03 11:15:52 +02:00
|
|
|
l = ("en_US", "UTF-8")
|
|
|
|
lcl = self.parameter("locale", ".".join(l))
|
2020-04-25 10:40:50 +02:00
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
locale.setlocale(locale.LC_TIME, lcl.split("."))
|
2020-04-25 10:40:50 +02:00
|
|
|
except Exception:
|
2020-05-03 11:15:52 +02:00
|
|
|
locale.setlocale(locale.LC_TIME, ("en_US", "UTF-8"))
|
2020-04-25 10:40:50 +02:00
|
|
|
|
2020-04-25 10:48:36 +02:00
|
|
|
def default_format(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
return "%x %X %Z"
|
2020-04-25 10:48:36 +02:00
|
|
|
|
2020-04-25 10:40:50 +02:00
|
|
|
def get_time(self, widget):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
tz = pytz.timezone(self._timezones[self._current_tz].strip())
|
2020-05-03 11:15:52 +02:00
|
|
|
retval = (
|
|
|
|
datetime.datetime.now(tz=tzlocal.get_localzone())
|
|
|
|
.astimezone(tz)
|
|
|
|
.strftime(self.__fmt)
|
|
|
|
)
|
2020-04-25 10:40:50 +02:00
|
|
|
except pytz.exceptions.UnknownTimeZoneError:
|
2020-05-03 11:15:52 +02:00
|
|
|
retval = "[Unknown timezone: {}]".format(
|
|
|
|
self._timezones[self._current_tz].strip()
|
|
|
|
)
|
2020-04-25 10:40:50 +02:00
|
|
|
except Exception as e:
|
2020-05-03 11:15:52 +02:00
|
|
|
logging.error("unable to get time: {}".format(str(e)))
|
|
|
|
retval = "[n/a]"
|
2020-04-25 10:40:50 +02:00
|
|
|
|
|
|
|
enc = locale.getpreferredencoding()
|
2020-05-03 11:15:52 +02:00
|
|
|
if hasattr(retval, "decode"):
|
2020-04-25 10:40:50 +02:00
|
|
|
return retval.decode(enc)
|
|
|
|
return retval
|
|
|
|
|
|
|
|
def next_tz(self, event):
|
|
|
|
next_timezone = self._current_tz + 1
|
|
|
|
if next_timezone >= len(self._timezones):
|
2020-05-03 11:15:52 +02:00
|
|
|
next_timezone = 0 # wraparound
|
2020-04-25 10:40:50 +02:00
|
|
|
self._current_tz = next_timezone
|
|
|
|
|
|
|
|
def prev_tz(self, event):
|
|
|
|
previous_timezone = self._current_tz - 1
|
|
|
|
if previous_timezone < 0:
|
2020-05-03 11:15:52 +02:00
|
|
|
previous_timezone = 0 # wraparound
|
2020-04-25 10:40:50 +02:00
|
|
|
self._current_tz = previous_timezone
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-25 10:40:50 +02:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|