如何在菜单栏中的时间指示器附近显示当前时区?

vst*_*iuk 3 indicator time unity date timezone

如何在 Ubuntu 16.10 的菜单栏中显示时间和日期附近的当前时区?这也应该与其他版本相关。这是一个非常缺失的功能,没有它你很容易混淆。我想得到类似“星期五 12 月 23 日 20:35:05(欧洲/基辅)”的信息。

此外,能够任意自定义该字符串会很好,例如“星期五,23.12.2016 - 20:35:05(欧洲/基辅)”。谢谢!

更新

参考如何更改日期格式? 一个可以做

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)'"

这将系统时间和日期指示器更改为

星期五,23.12.2016 - 20:35:05 (EET)

如您所见,除了时区名称之外,现在一切都很好。我在Time & Date settings > Clock > Choose Locations 中设置我的时区。然后,如果我timedatectl在终端写 我得到

时区:欧洲/基辅(EET,+0200)

因此,时区名称有多种选项,似乎是strftime函数决定使用哪个选项,而且仅通过向其提供格式字符串就无法获得时区名称“欧洲/基辅”(仅“EET”或“+0200”)。

那么有没有办法选择时区的名称格式?

也许使用 Serg 的脚本我可以将首选名称放在系统指示器旁边?

谢谢!

Ser*_*nyy 5

介绍

下面显示的指示器在顶部面板中显示当前时区。它的工作方式相当简单。时区设置在/etc/timezone文件中设置。指标所做的就是读取该文件,并在必要时更新显示的信息。

将下面的源代码另存为~/bin/timezone_indicator,运行chmod +x ~/bin/timezone_indicator以使其可执行,然后以~/bin/timezone_indicator. 如果您希望它在每次自动登录时都启动,请打开“启动应用程序”菜单,并将指示器的完整路径添加为该处的命令之一。

在此处输入图片说明

随意测试更改时区,如https://askubuntu.com/a/524362/295286 所示

脚本源

也可以在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()
Run Code Online (Sandbox Code Playgroud)