[gnome-calendar/mcatanzaro/#424: 10/10] Fix locale lookup in is_workday()



commit 0bc40a32191a97841ebd519d8563b43784a48aa7
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Wed Jul 3 18:01:21 2019 -0500

    Fix locale lookup in is_workday()
    
    Trying to use environment variables to guess the locale was a good
    attempt, but this is tricky because they have to be looked up in a
    certain order: first LC_ALL, then fallback to LC_TIME, and finally LANG,
    which was missed here. This caused a huge warning spam on the year view
    when only LANG is set, and usually only LANG should be set since the
    other environment variables are usually used for temporary overrides.
    Instead, query the locale using setlocale().
    
    Also, don't assert if the locale is shorter than expected (missing
    territory code) since there is at least one valid locale that does not
    have any territory codes (Esperanto) and since it's programmer error to
    assert when the application processes unexpected input like this.
    
    Fixes #424

 src/utils/gcal-utils.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
---
diff --git a/src/utils/gcal-utils.c b/src/utils/gcal-utils.c
index 495efc0a..16dbe307 100644
--- a/src/utils/gcal-utils.c
+++ b/src/utils/gcal-utils.c
@@ -34,6 +34,7 @@
 #include <glib/gi18n.h>
 
 #include <langinfo.h>
+#include <locale.h>
 
 #include <string.h>
 #include <math.h>
@@ -1041,18 +1042,14 @@ is_workday (guint day)
 
   no_work_days = GCAL_WEEK_DAY_SATURDAY | GCAL_WEEK_DAY_SUNDAY;
 
-  locale = getenv ("LC_ALL");
-  if (!locale || g_utf8_strlen (locale, -1) < 5)
-    locale = getenv ("LC_TIME");
+  locale = setlocale (LC_TIME, NULL);
 
-  if (!locale)
+  if (!locale || g_utf8_strlen (locale, -1) < 5)
     {
-      g_warning ("Locale is NULL, assuming Saturday and Sunday as non workdays");
+      g_warning ("Locale is unset or lacks territory code, assuming Saturday and Sunday as non workdays");
       return !(no_work_days & 1 << day);
     }
 
-  g_return_val_if_fail (g_utf8_strlen (locale, -1) >= 5, TRUE);
-
   territory[0] = locale[3];
   territory[1] = locale[4];
 


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