Always match the language used for time formatting using strftime()
with the application language for consistency (i.e. LC_MESSAGES, if
supported, overrides LC_TIME).

At the moment, a bug is triggered, for example, when LC_MESSAGES is set
to "C" (not infrequent at all) and the other locale variables are set
to the user's first language: in such case, the application incorrectly
produces, for example, the day of the week in a language different from
the rest of the sentence generated at the top of quoted mail messages
and similar inconsistencies in the time and date features of the calendar.

A companion patch is required for the evolution code.

This fourth version of the eds patch includes fixes as from a review by
Milan Crha (Redhat).

Signed-off-by: Guido Trentalancia <guido@trentalancia.net>
---
 libedataserver/e-data-server-util.c |   62 ++++++++++++++++++++++++++++++++++--
 1 file changed, 60 insertions(+), 2 deletions(-)

diff -pru evolution-data-server-3.20.1-orig/libedataserver/e-data-server-util.c evolution-data-server-3.20.1/libedataserver/e-data-server-util.c
--- evolution-data-server-3.20.1-orig/libedataserver/e-data-server-util.c	2016-04-15 16:46:52.220964656 +0200
+++ evolution-data-server-3.20.1/libedataserver/e-data-server-util.c	2016-04-18 16:02:30.059366759 +0200
@@ -25,6 +25,7 @@
 #include <sys/stat.h>
 #include <time.h>
 #include <unistd.h>
+#include <locale.h>
 
 #ifdef G_OS_WIN32
 #include <mbstring.h>
@@ -682,6 +683,9 @@ e_util_ensure_gdbus_string (const gchar
  * converts the &percnt;l and &percnt;k (12h and 24h) format variables
  * if necessary.
  *
+ * The function also makes sure that the language used for formatting
+ * time strings matches the application language.
+ *
  * Returns: The number of characters placed in @s.
  **/
 gsize
@@ -693,12 +697,26 @@ e_strftime (gchar *string,
 #ifndef HAVE_LKSTRFTIME
 	gchar *c, *ffmt, *ff;
 #endif
+#if defined(LC_MESSAGES) && defined(LC_TIME)
+	gchar *ctime, *cmessages, *saved_locale;
+#endif
 	gsize ret;
 
 	g_return_val_if_fail (string != NULL, 0);
 	g_return_val_if_fail (fmt != NULL, 0);
 	g_return_val_if_fail (tm != NULL, 0);
 
+#if defined(LC_MESSAGES) && defined(LC_TIME)
+	/* Use LC_MESSAGES instead of LC_TIME for time
+	 * formatting (for consistency).
+	 */
+	ctime = setlocale(LC_TIME, NULL);
+	saved_locale = strdup(ctime);
+	g_return_val_if_fail (saved_locale != NULL, 0);
+	cmessages = setlocale(LC_MESSAGES, NULL);
+	setlocale (LC_TIME, cmessages);
+#endif
+
 #ifdef HAVE_LKSTRFTIME
 	ret = strftime (string, max, fmt, tm);
 #else
@@ -731,6 +749,14 @@ e_strftime (gchar *string,
 	if (ret == 0 && max > 0)
 		string[0] = '\0';
 
+#if defined(LC_MESSAGES) && defined(LC_TIME)
+	/* Restore LC_TIME, if it has been changed to match
+	 * LC_MESSAGES.
+	 */
+	setlocale (LC_TIME, saved_locale);
+	g_free(saved_locale);
+#endif
+
 	return ret;
 }
 
@@ -741,7 +767,11 @@ e_strftime (gchar *string,
  * @fmt: The formatting to use on @tm.
  * @tm: The time value to format.
  *
- * The UTF-8 equivalent of e_strftime ().
+ * The UTF-8 equivalent of e_strftime (), which
+ * also makes sure that the language used for
+ * formatting time strings matches the application
+ * language (used for example in the string produced
+ * when quoting mail messages).
  *
  * Returns: The number of characters placed in @s.
  **/
@@ -753,16 +783,44 @@ e_utf8_strftime (gchar *string,
 {
 	gsize sz, ret;
 	gchar *locale_fmt, *buf;
+#if defined(LC_MESSAGES) && defined(LC_TIME)
+	gchar *ctime, *cmessages, *saved_locale;
+#endif
 
 	g_return_val_if_fail (string != NULL, 0);
 	g_return_val_if_fail (fmt != NULL, 0);
 	g_return_val_if_fail (tm != NULL, 0);
 
+#if defined(LC_MESSAGES) && defined(LC_TIME)
+	/* Use LC_MESSAGES instead of LC_TIME for time
+	 * formatting (e.g. for consistency when
+	 * quoting mail messages).
+	 */
+	ctime = setlocale(LC_TIME, NULL);
+	saved_locale = strdup(ctime);
+	g_return_val_if_fail (saved_locale != NULL, 0);
+	cmessages = setlocale(LC_MESSAGES, NULL);
+	setlocale (LC_TIME, cmessages);
+#endif
+
 	locale_fmt = g_locale_from_utf8 (fmt, -1, NULL, &sz, NULL);
-	if (!locale_fmt)
+	if (!locale_fmt) {
+#if defined(LC_MESSAGES) && defined(LC_TIME)
+		g_free(saved_locale);
+#endif
 		return 0;
+	}
 
 	ret = e_strftime (string, max, locale_fmt, tm);
+
+#if defined(LC_MESSAGES) && defined(LC_TIME)
+	/* Restore LC_TIME, if it has been changed to match
+	 * LC_MESSAGES.
+	 */
+	setlocale (LC_TIME, saved_locale);
+	g_free(saved_locale);
+#endif
+
 	if (!ret) {
 		g_free (locale_fmt);
 		return 0;
