[gnome-clocks] Try to handle first weekday based on locale



commit 72e6b6e63dcb7a6b9ad6d2d97347bc69a9eed800
Author: Paolo Borelli <pborelli gnome org>
Date:   Mon Sep 3 20:14:38 2012 +0200

    Try to handle first weekday based on locale
    
    Borrow code from the hamster applet to figure out the first day of the
    week and use it when displaying alarm days.
    Fix suggested by Alexandre Franke.

 gnomeclocks/alarm.py |   37 ++++++----------------------
 gnomeclocks/utils.py |   65 ++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 56 insertions(+), 46 deletions(-)
---
diff --git a/gnomeclocks/alarm.py b/gnomeclocks/alarm.py
index 10efebf..c28a415 100644
--- a/gnomeclocks/alarm.py
+++ b/gnomeclocks/alarm.py
@@ -112,40 +112,17 @@ class AlarmItem:
         if n == 0:
             return ""
         elif n == 1:
-            if 0 in self.days:
-                return _("Mondays")
-            elif 1 in self.days:
-                return _("Tuesdays")
-            elif 2 in self.days:
-                return _("Wednesdays")
-            elif 3 in self.days:
-                return _("Thursdays")
-            elif 4 in self.days:
-                return _("Fridays")
-            elif 5 in self.days:
-                return _("Saturdays")
-            elif 6 in self.days:
-                return _("Sundays")
+            return LocalizedWeekdays.get_plural(self.days[0])
         elif n == 7:
             return _("Every day")
         elif self.days == [0, 1, 2, 3, 4]:
             return _("Weekdays")
         else:
             days = []
-            if 0 in self.days:
-                days.append(LocalizedWeekdays.MON)
-            if 1 in self.days:
-                days.append(LocalizedWeekdays.TUE)
-            if 2 in self.days:
-                days.append(LocalizedWeekdays.WED)
-            if 3 in self.days:
-                days.append(LocalizedWeekdays.THU)
-            if 4 in self.days:
-                days.append(LocalizedWeekdays.FRI)
-            if 5 in self.days:
-                days.append(LocalizedWeekdays.SAT)
-            if 6 in self.days:
-                days.append(LocalizedWeekdays.SUN)
+            for i in range(7):
+                day_num = (LocalizedWeekdays.first_weekday() + i) % 7
+                if day_num in self.days:
+                    days.append(LocalizedWeekdays.get_abbr(day_num))
             return ", ".join(days)
 
     def check_expired(self):
@@ -252,7 +229,9 @@ class AlarmDialog(Gtk.Dialog):
         # create a box and put repeat days in it
         box = Gtk.Box(True, 0)
         box.get_style_context().add_class("linked")
-        for day_num, day_name in enumerate(LocalizedWeekdays.get_list()):
+        for i in range(7):
+            day_num = (LocalizedWeekdays.first_weekday() + i) % 7
+            day_name = LocalizedWeekdays.get_abbr(day_num)
             btn = Gtk.ToggleButton(label=day_name)
             btn.data = day_num
             if btn.data in days:
diff --git a/gnomeclocks/utils.py b/gnomeclocks/utils.py
index 471dae2..8c2f229 100644
--- a/gnomeclocks/utils.py
+++ b/gnomeclocks/utils.py
@@ -18,11 +18,15 @@
 
 import os
 import time
+import datetime
 import pycanberra
 from xdg import BaseDirectory
 from gi.repository import Gio, Notify
 
 
+def N_(message): return message
+
+
 class Dirs:
     @staticmethod
     def get_data_dir():
@@ -62,25 +66,52 @@ class SystemSettings:
 
 
 class LocalizedWeekdays:
-    MON = time.strftime("%a", (0, 0, 0, 0, 0, 0, 0, 0, 0))
-    TUE = time.strftime("%a", (0, 0, 0, 0, 0, 0, 1, 0, 0))
-    WED = time.strftime("%a", (0, 0, 0, 0, 0, 0, 2, 0, 0))
-    THU = time.strftime("%a", (0, 0, 0, 0, 0, 0, 3, 0, 0))
-    FRI = time.strftime("%a", (0, 0, 0, 0, 0, 0, 4, 0, 0))
-    SAT = time.strftime("%a", (0, 0, 0, 0, 0, 0, 5, 0, 0))
-    SUN = time.strftime("%a", (0, 0, 0, 0, 0, 0, 6, 0, 0))
+    # translate them ourselves since we want plurals
+    _plural = [
+        N_("Mondays"),
+        N_("Tuesdays"),
+        N_("Wednesdays"),
+        N_("Thursdays"),
+        N_("Fridays"),
+        N_("Saturdays"),
+        N_("Sundays")
+    ]
+
+    # fetch abbreviations from libc
+    _abbr = [
+        time.strftime("%a", (0, 0, 0, 0, 0, 0, 0, 0, 0)),
+        time.strftime("%a", (0, 0, 0, 0, 0, 0, 1, 0, 0)),
+        time.strftime("%a", (0, 0, 0, 0, 0, 0, 2, 0, 0)),
+        time.strftime("%a", (0, 0, 0, 0, 0, 0, 3, 0, 0)),
+        time.strftime("%a", (0, 0, 0, 0, 0, 0, 4, 0, 0)),
+        time.strftime("%a", (0, 0, 0, 0, 0, 0, 5, 0, 0)),
+        time.strftime("%a", (0, 0, 0, 0, 0, 0, 6, 0, 0))
+    ]
+
+    @staticmethod
+    def get_plural(day):
+        return _(LocalizedWeekdays._plural[day])
 
     @staticmethod
-    def get_list():
-        return [
-            LocalizedWeekdays.MON,
-            LocalizedWeekdays.TUE,
-            LocalizedWeekdays.WED,
-            LocalizedWeekdays.THU,
-            LocalizedWeekdays.FRI,
-            LocalizedWeekdays.SAT,
-            LocalizedWeekdays.SUN
-        ]
+    def get_abbr(day):
+        return LocalizedWeekdays._abbr[day]
+
+    # based on code from hamster-applet
+    # pretty ugly, but it seems this is the only way
+    # note that we use the convention used by struct_time.tm_wday
+    # which is 0 = Monday, note the one used by strftime("%w")
+    @staticmethod
+    def first_weekday():
+        try:
+            process = os.popen("locale first_weekday week-1stday")
+            week_offset, week_start = process.read().split('\n')[:2]
+            process.close()
+            week_start = datetime.date(*time.strptime(week_start, "%Y%m%d")[:3])
+            week_offset = datetime.timedelta(int(week_offset) - 1)
+            beginning = week_start + week_offset
+            return (int(beginning.strftime("%w")) + 6) % 7
+        except:
+            return 0
 
 
 class Alert:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]