Within the mail composer, always match the language used for time and
date formatting using strftime() with the application language for
for consistency (i.e. LC_MESSAGES, if supported by the platform,
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 (in particular
LC_TIME) 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 language used in the rest of the sentence generated
at the top of quoted mail messages.

The companion patch for the evolution data server code is no longer
needed as the problem is now tackled entirely within the evolution code.

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

Signed-off-by: Guido Trentalancia <guido@trentalancia.net>
---
 e-util/e-misc-utils.c    |   50 +++++++++++++++++++++++++++++++++++++++++++++++
 e-util/e-misc-utils.h    |    4 +++
 mail/em-composer-utils.c |    2 -
 3 files changed, 55 insertions(+), 1 deletion(-)

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 21:00:02.790756678 +0200
@@ -1810,6 +1810,56 @@ e_utf8_strftime_fix_am_pm (gchar *str,
 }
 
 /**
+ * e_utf8_strftime_match_locale:
+ * @string: The string array to store the result in.
+ * @max: The size of array @s.
+ * @fmt: The formatting to use on @tm.
+ * @tm: The time value to format.
+ *
+ * The UTF-8 equivalent of e_strftime (), which also
+ * makes sure that the locale used for time and date
+ * formatting matches the locale used by the
+ * application so that, for example, the quoted
+ * message header produced by the mail composer in a
+ * reply uses only one locale (i.e. LC_MESSAGES, where
+ * available, overrides LC_TIME for consistency).
+ *
+ * Returns: The number of characters placed in @s.
+ **/
+gsize
+e_utf8_strftime_match_locale (gchar *string,
+                              gsize max,
+                              const gchar *fmt,
+                              const struct tm *tm)
+{
+	gsize ret;
+#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
+
+	ret = e_utf8_strftime(string, max, 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
+
+	return ret;
+}
+
+/**
  * e_get_month_name:
  * @month: month index
  * @abbreviated: if %TRUE, abbreviate the month name
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 20:44:44.390435173 +0200
@@ -167,6 +167,10 @@ gsize		e_utf8_strftime_fix_am_pm	(gchar
 						 gsize max,
 						 const gchar *fmt,
 						 const struct tm *tm);
+gsize		e_utf8_strftime_match_locale	(gchar *string,
+						 gsize max,
+						 const gchar *fmt,
+						 const struct tm *tm);
 const gchar *	e_get_month_name		(GDateMonth month,
 						 gboolean abbreviated);
 const gchar *	e_get_weekday_name		(GDateWeekday weekday,
diff -pru evolution-3.20.1-orig/mail/em-composer-utils.c evolution-3.20.1/mail/em-composer-utils.c
--- evolution-3.20.1-orig/mail/em-composer-utils.c	2016-03-30 19:35:53.000000000 +0200
+++ evolution-3.20.1/mail/em-composer-utils.c	2016-04-18 21:04:14.780868649 +0200
@@ -3111,7 +3111,7 @@ attribution_format (CamelMimeMessage *me
 				str, attribvars[i].v.format, tzone);
 			break;
 		case ATTRIB_STRFTIME:
-			e_utf8_strftime (
+			e_utf8_strftime_match_locale (
 				buf, sizeof (buf), attribvars[i].v.format, &tm);
 			g_string_append (str, buf);
 			break;
