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 data server code.

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

Signed-off-by: Guido Trentalancia <guido@trentalancia.net>
---
 addressbook/gui/widgets/eab-contact-formatter.c |    2
 calendar/gui/e-meeting-time-sel-item.c          |    2
 calendar/gui/e-meeting-time-sel.c               |    2
 calendar/gui/e-week-view-main-item.c            |    4 -
 e-util/e-misc-utils.c                           |   53 ++++++++++++++++++++++--
 e-util/e-misc-utils.h                           |    4 +
 6 files changed, 58 insertions(+), 9 deletions(-)

diff -pru evolution-3.20.1-orig/addressbook/gui/widgets/eab-contact-formatter.c evolution-3.20.1/addressbook/gui/widgets/eab-contact-formatter.c
--- evolution-3.20.1-orig/addressbook/gui/widgets/eab-contact-formatter.c	2015-08-19 14:06:44.000000000 +0200
+++ evolution-3.20.1/addressbook/gui/widgets/eab-contact-formatter.c	2016-04-18 17:13:23.300788873 +0200
@@ -363,7 +363,7 @@ accum_time_attribute (GString *buffer,
 			date->day,
 			date->month,
 			date->year);
-		g_date_strftime (sdate, 100, "%x", gdate);
+		e_match_lang_g_date_strftime (sdate, 100, "%x", gdate);
 		g_date_free (gdate);
 		render_table_row (buffer, html_label, sdate, icon, html_flags);
 		e_contact_date_free (date);
diff -pru evolution-3.20.1-orig/calendar/gui/e-meeting-time-sel.c evolution-3.20.1/calendar/gui/e-meeting-time-sel.c
--- evolution-3.20.1-orig/calendar/gui/e-meeting-time-sel.c	2016-03-21 09:07:25.000000000 +0100
+++ evolution-3.20.1/calendar/gui/e-meeting-time-sel.c	2016-04-18 17:13:30.911877137 +0200
@@ -2424,7 +2424,7 @@ e_meeting_time_selector_recalc_date_form
 	if (!e_datetime_format_includes_day_name ("calendar", "table",  DTFormatKindDate)) {
 		gchar *tmp;
 
-		g_date_strftime (buffer, sizeof (buffer), "%a", &date);
+		e_match_lang_g_date_strftime (buffer, sizeof (buffer), "%a", &date);
 
 		tmp = str;
 		str = g_strconcat (buffer, " ", str, NULL);
diff -pru evolution-3.20.1-orig/calendar/gui/e-meeting-time-sel-item.c evolution-3.20.1/calendar/gui/e-meeting-time-sel-item.c
--- evolution-3.20.1-orig/calendar/gui/e-meeting-time-sel-item.c	2014-03-24 10:25:23.000000000 +0100
+++ evolution-3.20.1/calendar/gui/e-meeting-time-sel-item.c	2016-04-18 17:10:22.930701492 +0200
@@ -456,7 +456,7 @@ e_meeting_time_selector_item_paint_day_t
 		gchar buffer[128];
 		gchar *tmp;
 
-		g_date_strftime (buffer, sizeof (buffer), "%a", date);
+		e_match_lang_g_date_strftime (buffer, sizeof (buffer), "%a", date);
 
 		tmp = str;
 		str = g_strconcat (buffer, " ", str, NULL);
diff -pru evolution-3.20.1-orig/calendar/gui/e-week-view-main-item.c evolution-3.20.1/calendar/gui/e-week-view-main-item.c
--- evolution-3.20.1-orig/calendar/gui/e-week-view-main-item.c	2014-07-07 12:56:09.000000000 +0200
+++ evolution-3.20.1/calendar/gui/e-week-view-main-item.c	2016-04-18 17:09:58.722421989 +0200
@@ -248,7 +248,7 @@ week_view_main_item_draw_day (EWeekViewM
 	}
 
 	if (today) {
-		g_date_strftime (
+		e_match_lang_g_date_strftime (
 			buffer, sizeof (buffer),
 			format_string ? format_string : "<b>%d</b>", date);
 		pango_cairo_update_context (cr, pango_context);
@@ -257,7 +257,7 @@ week_view_main_item_draw_day (EWeekViewM
 		pango_layout_set_text (layout, buffer, -1);
 		pango_layout_set_markup (layout, buffer, strlen (buffer));
 	} else {
-		g_date_strftime (
+		e_match_lang_g_date_strftime (
 			buffer, sizeof (buffer),
 			format_string ? format_string : "%d", date);
 		pango_cairo_update_context (cr, pango_context);
diff -pru evolution-3.20.1-orig/e-util/e-misc-utils.c evolution-3.20.1/e-util/e-misc-utils.c
--- evolution-3.20.1-orig/e-util/e-misc-utils.c	2016-03-21 09:07:25.000000000 +0100
+++ evolution-3.20.1/e-util/e-misc-utils.c	2016-04-18 17:06:47.520220219 +0200
@@ -1810,6 +1810,51 @@ e_utf8_strftime_fix_am_pm (gchar *str,
 }
 
 /**
+ * e_match_lang_g_date_strftime:
+ * s: destination buffer
+ * slen: destination buffer size
+ * format: format string
+ * date: valid GDate
+ *
+ * This function is a wrapper around g_date_strftime()
+ * provided by Gnome GLib, used to match the locale
+ * used for time and date strings to the locale used
+ * by the application. This is effective only on platforms
+ * which support both LC_MESSAGES and LC_TIME locale
+ * settings.
+ *
+ * Returns: the number of characters written to the buffer.
+ **/
+gsize e_match_lang_g_date_strftime (gchar *s,
+                             gsize slen,
+                             const gchar *format,
+                             const GDate *date)
+{
+#if defined(LC_MESSAGES) && defined(LC_TIME)
+	gchar *ctime, *cmessages, *saved_locale;
+
+	/* 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
+
+	g_date_strftime (s, slen, format, date);
+
+#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
+}
+ 
+/**
  * e_get_month_name:
  * @month: month index
  * @abbreviated: if %TRUE, abbreviate the month name
@@ -1843,9 +1888,9 @@ e_get_month_name (GDateMonth month,
 		g_date_set_julian (&date, 1);
 
 		for (ii = G_DATE_JANUARY; ii <= G_DATE_DECEMBER; ii++) {
-			g_date_strftime (buffer, sizeof (buffer), "%b", &date);
+			e_match_lang_g_date_strftime (buffer, sizeof (buffer), "%b", &date);
 			abbr_names[ii] = g_intern_string (buffer);
-			g_date_strftime (buffer, sizeof (buffer), "%B", &date);
+			e_match_lang_g_date_strftime (buffer, sizeof (buffer), "%B", &date);
 			full_names[ii] = g_intern_string (buffer);
 			g_date_add_months (&date, 1);
 		}
@@ -1890,9 +1935,9 @@ e_get_weekday_name (GDateWeekday weekday
 		g_date_set_julian (&date, 1);
 
 		for (ii = G_DATE_MONDAY; ii <= G_DATE_SUNDAY; ii++) {
-			g_date_strftime (buffer, sizeof (buffer), "%a", &date);
+			e_match_lang_g_date_strftime (buffer, sizeof (buffer), "%a", &date);
 			abbr_names[ii] = g_intern_string (buffer);
-			g_date_strftime (buffer, sizeof (buffer), "%A", &date);
+			e_match_lang_g_date_strftime (buffer, sizeof (buffer), "%A", &date);
 			full_names[ii] = g_intern_string (buffer);
 			g_date_add_days (&date, 1);
 		}
diff -pru evolution-3.20.1-orig/e-util/e-misc-utils.h evolution-3.20.1/e-util/e-misc-utils.h
--- evolution-3.20.1-orig/e-util/e-misc-utils.h	2016-03-21 09:07:25.000000000 +0100
+++ evolution-3.20.1/e-util/e-misc-utils.h	2016-04-18 17:14:01.127227676 +0200
@@ -167,6 +167,10 @@ gsize		e_utf8_strftime_fix_am_pm	(gchar
 						 gsize max,
 						 const gchar *fmt,
 						 const struct tm *tm);
+gsize		e_match_lang_g_date_strftime	(gchar *s,
+						 gsize slen,
+						 const gchar *format,
+						 const GDate *date);
 const gchar *	e_get_month_name		(GDateMonth month,
 						 gboolean abbreviated);
 const gchar *	e_get_weekday_name		(GDateWeekday weekday,
