[glib: 1/2] gdate: Reinitialize locale information in g_date_prepare_to_parse.



commit a8fd91aae5f098e204f785703e33a500eb545f60
Author: Tomasz Miąsko <tomasz miasko gmail com>
Date:   Tue Oct 30 00:00:00 2018 +0000

    gdate: Reinitialize locale information in g_date_prepare_to_parse.
    
    When g_date_set_parse was used with more than one locale it could
    incorrectly retain information from previous one. Reinitialize all
    locale specific data inside g_date_prepare_to_parse to avoid the issue.

 glib/gdate.c      |  6 ++++++
 glib/tests/date.c | 27 +++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
---
diff --git a/glib/gdate.c b/glib/gdate.c
index 13a87099a..4925818b3 100644
--- a/glib/gdate.c
+++ b/glib/gdate.c
@@ -1131,6 +1131,12 @@ g_date_prepare_to_parse (const gchar      *str,
       g_date_strftime (buf, 127, "%x", &d);
       
       g_date_fill_parse_tokens (buf, &testpt);
+
+      using_twodigit_years = FALSE;
+      locale_era_adjust = 0;
+      dmy_order[0] = G_DATE_DAY;
+      dmy_order[1] = G_DATE_MONTH;
+      dmy_order[2] = G_DATE_YEAR;
       
       i = 0;
       while (i < testpt.num_ints)
diff --git a/glib/tests/date.c b/glib/tests/date.c
index ba621e4f0..6cd91ab6c 100644
--- a/glib/tests/date.c
+++ b/glib/tests/date.c
@@ -182,6 +182,32 @@ test_parse (void)
   g_date_free (d);
 }
 
+static void
+test_parse_locale_change (void)
+{
+  /* Checks that g_date_set_parse correctly changes locale specific data as
+   * necessary. In this particular case year adjustment, as Thai calendar is
+   * 543 years ahead of the Gregorian calendar. */
+
+  GDate date;
+
+  if (setlocale (LC_ALL, "th_TH") == NULL)
+    {
+      g_test_skip ("locale th_TH not available");
+      return;
+    }
+
+  g_date_set_parse (&date, "04/07/2519");
+
+  setlocale (LC_ALL, "C");
+  g_date_set_parse (&date, "07/04/76");
+  g_assert_cmpint (g_date_get_day (&date), ==, 4);
+  g_assert_cmpint (g_date_get_month (&date), ==, 7);
+  g_assert_cmpint (g_date_get_year (&date), ==, 1976);
+
+  setlocale (LC_ALL, "");
+}
+
 static void
 test_month_names (void)
 {
@@ -709,6 +735,7 @@ main (int argc, char** argv)
   g_test_add_func ("/date/julian", test_julian_constructor);
   g_test_add_func ("/date/dates", test_dates);
   g_test_add_func ("/date/parse", test_parse);
+  g_test_add_func ("/date/parse_locale_change", test_parse_locale_change);
   g_test_add_func ("/date/month_names", test_month_names);
   g_test_add_func ("/date/clamp", test_clamp);
   g_test_add_func ("/date/order", test_order);


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