[module/datetime] Add locale parameter to override system default

This change mostly affects the %x and %X variables, for example to
change to 24 hour time you might set the locale to 'en_UK.UTF-8'.
This commit is contained in:
Naya Verdier 2017-08-06 11:29:41 -07:00
parent d15750008f
commit aa9941ceda
No known key found for this signature in database
GPG key ID: 1A59389D46A94A4C

View file

@ -6,6 +6,9 @@ Parameters:
* datetime.format: strftime()-compatible formatting string * datetime.format: strftime()-compatible formatting string
* date.format : alias for datetime.format * date.format : alias for datetime.format
* time.format : alias for datetime.format * time.format : alias for datetime.format
* datetime.locale: locale to use rather than the system default
* date.locale : alias for datetime.locale
* time.locale : alias for datetime.locale
""" """
from __future__ import absolute_import from __future__ import absolute_import
@ -13,8 +16,6 @@ import datetime
import locale import locale
import bumblebee.engine import bumblebee.engine
locale.setlocale(locale.LC_TIME, locale.getdefaultlocale())
ALIASES = [ "date", "time" ] ALIASES = [ "date", "time" ]
def default_format(module): def default_format(module):
@ -28,9 +29,15 @@ def default_format(module):
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
super(Module, self).__init__(engine, config, super(Module, self).__init__(engine, config,
bumblebee.output.Widget(full_text=self.get_time) bumblebee.output.Widget(full_text=self.get_time))
)
self._fmt = self.parameter("format", default_format(self.name)) self._fmt = self.parameter("format", default_format(self.name))
self._lcl = self.parameter("locale").split(".")
# can't use the default in "parameter" because we split the
# string, while 'getdefaultlocale' already returns a tuple
if self._lcl is None:
self._lcl = locale.getdefaultlocale()
locale.setlocale(locale.LC_TIME, self._lcl)
def get_time(self, widget): def get_time(self, widget):
return datetime.datetime.now().strftime(self._fmt) return datetime.datetime.now().strftime(self._fmt)