2020-02-04 21:10:05 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
"""Displays the current date and time.
|
2020-02-04 21:10:05 +01:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* datetime.format: strftime()-compatible formatting string
|
|
|
|
* datetime.locale: locale to use rather than the system default
|
2020-05-03 11:15:52 +02:00
|
|
|
"""
|
2020-02-04 21:10:05 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import datetime
|
|
|
|
import locale
|
|
|
|
|
|
|
|
import core.module
|
|
|
|
import core.widget
|
2020-02-07 21:28:29 +01:00
|
|
|
import core.input
|
2020-02-04 21:10:05 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-04 21:10:05 +01:00
|
|
|
class Module(core.module.Module):
|
2022-03-10 09:22:08 +01:00
|
|
|
def __init__(self, config, theme, dtlibrary=None):
|
2020-04-26 16:39:24 +02:00
|
|
|
super().__init__(config, theme, core.widget.Widget(self.full_text))
|
2020-02-07 21:28:29 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(self, button=core.input.LEFT_MOUSE, cmd="calendar")
|
2022-03-10 09:22:08 +01:00
|
|
|
self.dtlibrary = dtlibrary or datetime
|
|
|
|
|
|
|
|
def set_locale(self):
|
|
|
|
l = self.default_locale()
|
2020-02-04 21:10:05 +01:00
|
|
|
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-02-04 21:10:05 +01:00
|
|
|
try:
|
2022-03-10 09:22:08 +01:00
|
|
|
locale.setlocale(locale.LC_ALL, lcl.split("."))
|
2020-02-04 21:10:05 +01:00
|
|
|
except Exception as e:
|
2022-03-10 09:22:08 +01:00
|
|
|
locale.setlocale(locale.LC_ALL, ("en_US", "UTF-8"))
|
2020-02-04 21:10:05 +01:00
|
|
|
|
|
|
|
def default_format(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
return "%x %X"
|
2020-02-04 21:10:05 +01:00
|
|
|
|
2022-03-10 09:22:08 +01:00
|
|
|
def default_locale(self):
|
|
|
|
return locale.getdefaultlocale()
|
|
|
|
|
2020-02-23 14:47:47 +01:00
|
|
|
def full_text(self, widget):
|
2022-03-10 09:22:08 +01:00
|
|
|
self.set_locale()
|
2020-02-04 21:10:05 +01:00
|
|
|
enc = locale.getpreferredencoding()
|
2021-05-24 12:56:02 +02:00
|
|
|
fmt = self.parameter("format", self.default_format())
|
2022-03-10 09:22:08 +01:00
|
|
|
retval = self.dtlibrary.datetime.now().strftime(fmt)
|
2020-05-03 11:15:52 +02:00
|
|
|
if hasattr(retval, "decode"):
|
2020-02-04 21:10:05 +01:00
|
|
|
return retval.decode(enc)
|
|
|
|
return retval
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-04 21:10:05 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|