Merge pull request #469 from cphyc/bugfix/468-local-timezone-fix

Use pytz to figure out local timezone dynamically
This commit is contained in:
tobi-wan-kenobi 2019-11-24 21:04:23 +01:00 committed by GitHub
commit dddf37f22d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,6 +15,11 @@ from __future__ import absolute_import
import datetime import datetime
import locale import locale
import bumblebee.engine import bumblebee.engine
import os
try:
import pytz
except ModuleNotFoundError:
pytz = None
def default_format(module): def default_format(module):
default = "%x %X" default = "%x %X"
@ -41,8 +46,15 @@ class Module(bumblebee.engine.Module):
locale.setlocale(locale.LC_TIME, ('en_US', 'UTF-8')) locale.setlocale(locale.LC_TIME, ('en_US', 'UTF-8'))
def get_time(self, widget): def get_time(self, widget):
if pytz:
# Get local timezone (see https://github.com/tobi-wan-kenobi/bumblebee-status/issues/468)
my_tz_name = '/'.join(os.path.realpath('/etc/localtime').split('/')[-2:])
my_tz = pytz.timezone(my_tz_name)
else:
my_tz = None
enc = locale.getpreferredencoding() enc = locale.getpreferredencoding()
retval = datetime.datetime.now().strftime(self._fmt) retval = datetime.datetime.now(my_tz).strftime(self._fmt)
if hasattr(retval, "decode"): if hasattr(retval, "decode"):
return retval.decode(enc) return retval.decode(enc)
return retval return retval