diff --git a/i3pystatus/clock.py b/i3pystatus/clock.py index 61c9de4..33df881 100644 --- a/i3pystatus/clock.py +++ b/i3pystatus/clock.py @@ -3,7 +3,7 @@ import os import locale -import datetime +import time from i3pystatus import IntervalModule @@ -45,7 +45,7 @@ class Clock(IntervalModule): lang = (None, None) if lang != locale.getlocale(locale.LC_TIME): - # affects datetime.time.strftime() in whole program + # affects time.strftime() in whole program locale.setlocale(locale.LC_TIME, lang) if self.format is None: @@ -65,32 +65,26 @@ class Clock(IntervalModule): def expand_formats(formats): def expand_format(format_): if isinstance(format_, tuple): - return (format_[0], format_[1] if len(format_) > 1 else None) - return (format_, None) + # check if timezone exists (man tzset) + if len(format_) > 1 and os.path.isfile('/usr/share/zoneinfo/' + format_[1]): + return (format_[0], format_[1]) + else: + return (format_[0], time.tzname[0]) + return (format_, time.tzname[0]) return [expand_format(format_) for format_ in formats] def run(self): - # Safest way is to work from utc and localize afterwards - if self.format[self.current_format_id][1]: - try: - import pytz - except ImportError as e: - raise RuntimeError("Need pytz for timezones") from e - utc_dt = pytz.utc.localize(datetime.datetime.utcnow()) - tz = pytz.timezone(self.format[self.current_format_id][1]) - dt = tz.normalize(utc_dt.astimezone(tz)) - else: - dt = datetime.datetime.now() - - output = dt.strftime(self.format[self.current_format_id][0]) + # set correct timezone + if time.tzname[0] is not self.format[self.current_format_id][1]: + os.environ.putenv('TZ', self.format[self.current_format_id][1]) + time.tzset() self.output = { - "full_text": output, + "full_text": time.strftime(self.format[self.current_format_id][0]), + "color": self.color, "urgent": False, } - if self.color != "i3Bar": - self.output["color"] = self.color def scroll_format(self, step=1): self.current_format_id = (self.current_format_id + step) % len(self.format)