Script: alternatetz.py

Add an alternate timezone item.
Author: Chmouel — Version: 0.4 — License: GPL-3.0-or-later
For WeeChat ≥ 0.3.0, requires: pytz.
Tags: item, time, py2, py3
Added: 2010-01-29 — Updated: 2022-01-25

Download GitHub Repository

 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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010 Chmouel Boudjnah <chmouel@chmouel.com>
# Copyright (C) 2012-2013 bwidawsk <ben@bwidawsk.net>
# License: GPL3
#
# plugin to get alternate timezones in a weechat bar
#
# Changelog:
#  0.4 Sébastien Helleu <flashcode@flashtux.org>
#      Remove trailing tabs
#  0.3 Pol Van Aubel <dev@polvanaubel.com>
#      Fix tab/space usage for Python3 compatibility
#  0.2 Added help, and multiple timezeones
#  0.1 first version
#

import weechat as w
import pytz
import datetime

SCRIPT_NAME    = "alternatetz"
SCRIPT_AUTHOR  = "Chmouel Boudjnah <chmouel@chmouel.com>"
SCRIPT_VERSION = "0.4"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC    = "Display Alternate Time from different Timezones"

SCRIPT_COMMAND = 'alternatetz'

OPTIONS = {
'timezone': ('GMT', 'list of timezones to display. The list is comprised of space separated list timezones using the Olson tz database'),
'timeformat': ('%H:%M', 'strftime compatible format')
}

def alternatetz_item_cb(*kwargs):
    ret = ''
    tznames = OPTIONS['timezone'].split()
    for tzname in tznames:
        tz = pytz.timezone(tzname)
        ret += tz.zone + ': ' + datetime.datetime.now(tz).strftime(OPTIONS['timeformat']) + ' '
    return ret[:-1]

def alternatetz_timer_cb(*kwargs):
    w.bar_item_update('alternatetz')
    return w.WEECHAT_RC_OK

if __name__ == '__main__':
    w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', '')
    for option,value in list(OPTIONS.items()):
        w.config_set_desc_plugin(option, '%s (default: "%s")' % (value[1], value[0]))
        if not w.config_is_set_plugin(option):
            w.config_set_plugin(option, value[0])
            OPTIONS[option] = value[0]
        else:
            OPTIONS[option] = w.config_get_plugin(option)

    w.bar_item_new('alternatetz', 'alternatetz_item_cb', '')
    w.bar_item_update('alternatetz')
    w.hook_timer(1000*60, 60, 0, 'alternatetz_timer_cb', '')

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4