=== modified file 'bin/indicator-weather' --- bin/indicator-weather 2011-03-30 21:40:33 +0000 +++ bin/indicator-weather 2011-04-01 01:25:30 +0000 @@ -51,7 +51,7 @@ sys.path.insert(0, PROJECT_ROOT_DIRECTORY) os.putenv('PYTHONPATH', PROJECT_ROOT_DIRECTORY) # for subprocesses -from indicator_weather.helpers import get_builder +from indicator_weather.helpers import get_builder, TimeFormatter class Settings: """ Class to read/write settings """ @@ -667,11 +667,13 @@ # Get sunrise label def get_sunrise_label(self): - return "%s: %s" % (_("Sunrise"), self.__sunrise_t.strftime('%X').replace(":00", "")) + return "%s: %s" % (_("Sunrise"), + TimeFormatter.format_time(self.__sunrise_t)) # Get sunset label def get_sunset_label(self): - return "%s: %s" % (_("Sunset"), self.__sunset_t.strftime('%X').replace(":00", "")) + return "%s: %s" % (_("Sunset"), + TimeFormatter.format_time(self.__sunset_t)) # Additional functions # Convert wind direction from degrees to localized direction === modified file 'indicator_weather/helpers.py' --- indicator_weather/helpers.py 2011-03-09 00:42:12 +0000 +++ indicator_weather/helpers.py 2011-04-01 01:37:18 +0000 @@ -46,3 +46,68 @@ builder.set_translation_domain('indicator-weather') builder.add_from_file(ui_filename) return builder + + +class TimeFormatter: + """ + Formats a time object with respect to the settings of indicator-datetime + """ + + # default format from locale + format = "%X" + + SETTINGS_TIME_LOCALE = 0 + SETTINGS_TIME_12_HOUR = 1 + SETTINGS_TIME_24_HOUR = 2 + SETTINGS_TIME_CUSTOM = 3 + + @staticmethod + def format_time(t): + """ do the format and strip :00 secs """ + s = t.strftime(TimeFormatter.format) + if t.second == 0: + if t.minute!=0: + return s.replace(":00", "") + else: + return s.replace(":00:00", ":00") + else: + return s + + @staticmethod + def calc_format(gsettings, changed_key=None): + """ settings init or changed """ + + time_format = gsettings.get_enum("time-format") + show_seconds = gsettings.get_boolean("show-seconds") + + if time_format == TimeFormatter.SETTINGS_TIME_24_HOUR: + if show_seconds: + TimeFormatter.format = "%H:%M:%S" + else: + TimeFormatter.format = "%H:%M" + + elif time_format == TimeFormatter.SETTINGS_TIME_12_HOUR: + if show_seconds: + TimeFormatter.format = "%l:%M:%S %p" + else: + TimeFormatter.format = "%l:%M %p" + + elif time_format == TimeFormatter.SETTINGS_TIME_CUSTOM: + # ignore this as it might contain date params + #TimeFormatter.format = gsettings.get_string("custom-time-format") + TimeFormatter.format = "%X" + + elif time_format == TimeFormatter.SETTINGS_TIME_LOCALE: + # datetime indicator does not use %X + # it always uses 12 hour format unless translated otherwise + TimeFormatter.format = "%X" + +try: + # Load GSettings from Gio + from gi.repository import Gio + gsettings = Gio.Settings.new("org.ayatana.indicator.datetime") + TimeFormatter.calc_format(gsettings) + gsettings.connect("changed", TimeFormatter.calc_format) + +except: + pass # Fail silently and use default locale format