Merge pull request #198 from richese/clock

Remove dependency on `pytz` in clock module.
This commit is contained in:
enkore 2015-04-28 12:17:51 +02:00
commit c3b6f095ff

View File

@ -3,14 +3,14 @@
import os import os
import locale import locale
import datetime import time
from i3pystatus import IntervalModule from i3pystatus import IntervalModule
class Clock(IntervalModule): class Clock(IntervalModule):
""" """
This class shows a clock This class shows a clock.
format can be passed in four different ways: format can be passed in four different ways:
@ -18,11 +18,39 @@ class Clock(IntervalModule):
- one two-tuple, first is the format, second the timezone - one two-tuple, first is the format, second the timezone
- list of strings - no timezones - list of strings - no timezones
- list of two tuples, first is the format, second is timezone - list of two tuples, first is the format, second is timezone
Use mousewheel to cycle between formats.
For complete time format specification see:
::
man strftime
All available timezones are located in directory:
::
/usr/share/zoneinfo/
.. rubric:: Format examples
::
# one format, local timezone
format = '%a %b %-d %b %X'
# multiple formats, local timezone
format = [ '%a %b %-d %b %X', '%X' ]
# one format, specified timezone
format = ('%a %b %-d %b %X', 'Europe/Bratislava')
# multiple formats, specified timezones
format = [ ('%a %b %-d %b %X', 'America/New_York'), ('%X', 'Etc/GMT+9') ]
""" """
settings = ( settings = (
("format", "`None` means to use the default, locale-dependent format. Can cycle between formats with mousewheel"), ("format", "`None` means to use the default, locale-dependent format."),
("color", "RGB hexadecimal code color specifier, default to #ffffff, set to `i3Bar` to use i3 bar default"), ("color", "RGB hexadecimal code color specifier, default to #ffffff"),
) )
format = None format = None
color = "#ffffff" color = "#ffffff"
@ -45,7 +73,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 +93,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 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)