Remove dependency on pytz and datetime modules.

This commit is contained in:
Lukáš Mandák 2015-04-13 12:25:52 +02:00
parent 23ecdeed88
commit e31c58f1ad

View File

@ -3,7 +3,7 @@
import os import os
import locale import locale
import datetime import time
from i3pystatus import IntervalModule from i3pystatus import IntervalModule
@ -45,7 +45,7 @@ class Clock(IntervalModule):
lang = (None, None) lang = (None, None)
if lang != locale.getlocale(locale.LC_TIME): 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) locale.setlocale(locale.LC_TIME, lang)
if self.format is None: if self.format is None:
@ -65,32 +65,26 @@ class Clock(IntervalModule):
def expand_formats(formats): def expand_formats(formats):
def expand_format(format_): def expand_format(format_):
if isinstance(format_, tuple): if isinstance(format_, tuple):
return (format_[0], format_[1] if len(format_) > 1 else None) # check if timezone exists (man tzset)
return (format_, None) 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] return [expand_format(format_) for format_ in formats]
def run(self): def run(self):
# Safest way is to work from utc and localize afterwards # set correct timezone
if self.format[self.current_format_id][1]: if time.tzname[0] is not self.format[self.current_format_id][1]:
try: os.environ.putenv('TZ', self.format[self.current_format_id][1])
import pytz time.tzset()
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])
self.output = { self.output = {
"full_text": output, "full_text": time.strftime(self.format[self.current_format_id][0]),
"color": self.color,
"urgent": False, "urgent": False,
} }
if self.color != "i3Bar":
self.output["color"] = self.color
def scroll_format(self, step=1): def scroll_format(self, step=1):
self.current_format_id = (self.current_format_id + step) % len(self.format) self.current_format_id = (self.current_format_id + step) % len(self.format)