aboutsummaryrefslogtreecommitdiffstats
path: root/xpybar/config/myleapsec.py
blob: e45c985e259288398ec16d0f9fa6ad2cb7031f29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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)))