aboutsummaryrefslogtreecommitdiffstats
path: root/examples/plugins/leapsec
blob: ff86f620fcbc8dc105049e34e215f4ea40d1900e (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- python -*-
'''
xpybar – xmobar replacement written in python
Copyright © 2014, 2015, 2016, 2017, 2018, 2019  Mattias Andrée (m@maandreese)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

# A xpybar configuration example testing the features of plugins.leapsec

import time
import threading

from plugins.leapsec import LeapSeconds
from plugins.clock import Clock


OUTPUT, HEIGHT, YPOS, TOP = 0, 12, 24, True


clock = Clock(sync_to = 0.5 * Clock.SECONDS)

start_ = start
def start():
    start_()
    xasync(lambda : clock.continuous_sync(lambda : bar.invalidate()))


leapsec_ = Sometimes(LeapSeconds, 30 * 24 * 60 * 2)


def redraw():
    announcements = leapsec_()
    found = -1
    now = time.time()
    for index in range(len(announcements)):
        if announcements[index][3] >= now:
            found = index
            break
    announcement = announcements[found]
    text = '\033[%sm%s\033[00m ago' if found < 0 else 'in \033[%sm%s\033[00m'
    def dur(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
    amount = ('+%i' if announcement[4] > 0 else '%i') % announcement[4]
    c = '00'
    time_until = announcement[3] - int(now)
    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'
    text %= (c, dur(abs(time_until)))
    text = 'Leap: %s %s at end of UTC %s' % (amount, text, '%i-%02i-%0i' % announcement[:3])
    if announcement[5] == LeapSeconds.SECONDARY:
        text += ' (secondary)'
    if announcement[5] == LeapSeconds.OUT_OF_BAND:
        text += ' (out of band)'
    bar.clear()
    bar.draw_coloured_text(0, 10, 0, 2, text)