utils: Use setlocale() 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
This commit is contained in:
Michael Catanzaro
2019-07-09 21:52:56 -03:00
parent 223ed62034
commit 621d69e0bc
+4 -7
View File
@@ -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];