[glib/backport-2449-2600-mingw-test-fixes-glib-2-72: 2/6] glib/tests/date.c: Fix 2-digit year test




commit ccc2a73e86c93d0fdcd4642ae1d0ea64677e14ff
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Mon Apr 11 15:18:30 2022 +0800

    glib/tests/date.c: Fix 2-digit year test
    
    ...for certain Windows locales, since the formats accepted for
    g_date_set_parse() will vary depending on the current system locale.  For
    instance, g_date_set_parse(gdate, "dd/mm/yy") is accepted on locales such
    as zh-HK (Chinese (Hong Kong SAR)) but is rejected on zh-TW (Chinese
    (Taiwan)).
    
    One can tell from the "date format" settings in the Windows system
    control panel whether there is a "dd/MM/YYYY" or "dd/MM/YY" option from the
    drop-down list of date formats to display for the locale, which will indicate
    whether g_date_set_parse(gdate, "dd/mm/yy") is accepted, which is true for
    zh-HK but is not true for zh-TW.
    
    If g_date_set_parse(gdate, "dd/mm/yy") is not accepted, try again with
    g_date_set_parse(gdate, "yy/mm/dd") thereafter for the 2-digit-year tests.

 glib/tests/date.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
---
diff --git a/glib/tests/date.c b/glib/tests/date.c
index 8903b8f0ce..e382295c42 100644
--- a/glib/tests/date.c
+++ b/glib/tests/date.c
@@ -826,6 +826,8 @@ test_two_digit_years (void)
   GDate *d;
   gchar buf[101];
   gchar *old_locale;
+  gboolean use_alternative_format = FALSE;
+
 #ifdef G_OS_WIN32
   LCID old_lcid;
 #endif
@@ -848,6 +850,25 @@ test_two_digit_years (void)
   g_date_strftime (buf, sizeof (buf), "%D", d);
   g_assert_cmpstr (buf, ==, "10/10/76");
   g_date_set_parse (d, buf);
+
+#ifdef G_OS_WIN32
+  /*
+   * It depends on the locale setting whether the dd/mm/yy
+   * format is allowed for g_date_set_parse() on Windows, which
+   * corresponds to whether there is an d/M/YY or d/M/YYYY (or so)
+   * option in the "Date and Time Format" setting for the selected
+   * locale in the Control Panel settings.  If g_date_set_parse()
+   * renders the GDate invalid with the dd/mm/yy format, use an
+   * alternative format (yy/mm/dd) for g_date_set_parse() for the
+   * 2-digit year tests.
+   */
+  if (!g_date_valid (d))
+    use_alternative_format = TRUE;
+#endif
+
+  if (use_alternative_format)
+    g_date_set_parse (d, "76/10/10");
+
   g_assert_cmpint (g_date_get_month (d), ==, 10);
   g_assert_cmpint (g_date_get_day (d), ==, 10);
   g_assert_true ((g_date_get_year (d) == 1976) ||
@@ -857,7 +878,7 @@ test_two_digit_years (void)
   g_date_set_dmy (d, 10, 10, 29);
   g_date_strftime (buf, sizeof (buf), "%D", d);
   g_assert_cmpstr (buf, ==, "10/10/29");
-  g_date_set_parse (d, buf);
+  g_date_set_parse (d, use_alternative_format ? "29/10/10" : buf);
   g_assert_cmpint (g_date_get_month (d), ==, 10);
   g_assert_cmpint (g_date_get_day (d), ==, 10);
   g_assert_true ((g_date_get_year (d) == 2029) ||


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