Ubuntu – How to show current timezone near time indicator in the menu bar

dateindicatortimetimezoneunity

How to show the current timezone near time and date in the menu bar in Ubuntu 16.10? This should be relevant for the other versions also.
It is a very missing feature, you can easily be confused without it.
I would like to get something like "Fri Dec 23 20:35:05 (Europe/Kiev)".

Also it would be nice to be able to customize that string arbitrarily, say "Friday, 23.12.2016 – 20:35:05 (Europe/Kiev)". Thank you!

Update

Referring to How to change the date format?
one can do

gsettings set com.canonical.indicator.datetime time-format "'custom'"

gsettings set com.canonical.indicator.datetime custom-time-format "'%a, %d.%m.%y - %H:%M:%S (%Z)'"

which changes the system time and date indicator to

Friday, 23.12.2016 – 20:35:05 (EET)

As you can see, everything is fine now, besides the timezone name. I set my timezones at Time & Date settings > Clock > Choose Locations. Then, if I write
timedatectl in terminal I get

Time zone: Europe/Kiev (EET, +0200)

So, there are several options for a timezone name and it seems that it is strftime function that decides which one to use and also that just by feeding it with a format string there is no way to get a timezone name "Europe/Kiev" (only "EET" or "+0200").

So are there any ways to choose a timezone's name format?

Maybe using Serg's script I could place the preferred name next to the system indicator?

Thanks!

Best Answer

Introduction

The indicator presented below displays current timezone in the top panel. The way it works is fairly simple. Timezone settings are set in /etc/timezone file. All the indicator does, is read that file, and update the information displayed if necessary.

Save the source code below as ~/bin/timezone_indicator , run chmod +x ~/bin/timezone_indicator to make it executable, and run as ~/bin/timezone_indicator. If you wish for it to start each time you log in automatically, open Startup Applications menu, and add the full path to the indicator as one of the commands there.

enter image description here

Feel free to test changing the timezone as show in https://askubuntu.com/a/524362/295286

Script Source

Also available on GitHub:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# Author: Serg Kolo , <1047481448@qq.com>
# Date: December 23, 2016
# Purpose: Indicator that displays timezone
# Written for:  https://askubuntu.com/q/863952/295286
# Tested on: Ubuntu 16.04 LTS
#
# Licensed under The MIT License (MIT).
# See included LICENSE file or the notice below.
#
# Copyright © 2016 Sergiy Kolodyazhnyy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import gi
gi.require_version('AppIndicator3', '0.1')
from gi.repository import GLib as glib
from gi.repository import AppIndicator3 as appindicator
from gi.repository import Gtk as gtk
from time import gmtime
import os

class TimezoneIndicator(object):

    def __init__(self):
        self.app = appindicator.Indicator.new(
            'timezone-ndicator', "",
            appindicator.IndicatorCategory.APPLICATION_STATUS)

        self.app.set_status(appindicator.IndicatorStatus.ACTIVE)
        self.app.set_icon('locale')

        self.app_menu = gtk.Menu()
        self.quit_app = gtk.MenuItem('Quit')
        self.quit_app.connect('activate', self.quit)
        self.quit_app.show()

        self.cache = None

        self.app_menu.append(self.quit_app)
        self.app.set_menu(self.app_menu)

        self.update_label()

    def run(self):
        gtk.main()

    def quit(self, data=None):
        gtk.main_quit()

    def update_label(self):
        timezone = None
        with open('/etc/timezone') as f: 
             timezone = f.read().strip()
        if timezone != self.cache:
             self.app.set_label(timezone,"")
        self.cache = timezone
        glib.timeout_add_seconds(1, self.callback)

    def callback(self):
        self.update_label()

def main():

    indicator = TimezoneIndicator()
    indicator.run()

if __name__ == '__main__':
    main()
Related Question