diff options
Diffstat (limited to '')
-rw-r--r-- | xpybar/config/myleapsec.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/xpybar/config/myleapsec.py b/xpybar/config/myleapsec.py new file mode 100644 index 0000000..e45c985 --- /dev/null +++ b/xpybar/config/myleapsec.py @@ -0,0 +1,69 @@ +# -*- python -*- +from plugins.leapsec import LeapSeconds + +from common import * + +class MyLeapsec(Entry): + def __init__(self, *args, **kwargs): + self.announcement = None + Entry.__init__(self, *args, **kwargs) + def init(): + self.refresh() + xasync(lambda : Clock(sync_to = Clock.MINUTES).continuous_sync(Sometimes(t(self.refresh), 12 * 60)), name = 'leapsec') + xasync(init, name = 'leapsec') + + def action(self, col, button, x, y): + if button == RIGHT_BUTTON: + self.refresh() + + def refresh(self): + leapanon = LeapSeconds() + found = -1 + now = time.time() + for index in range(len(leapanon)): + if leapanon[index][3] >= now: + found = index + break + announcement = leapanon[found] + text = ('+%i' if announcement[4] > 0 else '%i') % announcement[4] + text += 's ' + ('\033[%sm%s\033[00m ago' if found < 0 else 'in \033[%sm%s\033[00m') + text += ' end of UTC %i-%02i-%0i' % announcement[:3] + if announcement[5] == LeapSeconds.SECONDARY: + text += ' (secondary)' + elif announcement[5] == LeapSeconds.OUT_OF_BAND: + text += ' (out of band)' + self.announcement = (text, announcement[3]) + self.invalidate() + + def dur(self, t): + s, t = t % 60, t // 60 + m, t = t % 60, t // 60 + h, d = t % 24, t // 24 + if d > 0: + return '%id%ih%i\'%02i"' % (d, h, m, s) + elif h > 0: + return '%ih%i\'%02i"' % (h, m, s) + elif m > 0: + return '%i\'%02i"' % (m, s) + else: + return '%02is' % s + + def colour(self, time_until): + c = '00' + if time_until >= 0: + if time_until < 10: + c = '41' + elif time_until < 60: + c = '31' + elif time_until < 60 * 60: + c = '33' + elif time_until < 24 * 60 * 60: + c = '32' + return c + + def function(self): + if self.announcement is None: + return '...' + (text, when) = self.announcement + time_until = when - int(time.time()) + return text % (self.colour(time_until), self.dur(abs(time_until))) |