gdatetime: Disable ERA support on platforms which don’t support this

So far, that’s BSD: it supports `nl_langinfo()`, but not `ERA`.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3119
This commit is contained in:
Philip Withnall 2023-11-28 23:16:26 +00:00
parent 9eda0b2fcd
commit 8ee00cbad8
3 changed files with 68 additions and 19 deletions

View File

@ -165,11 +165,8 @@ static const guint16 days_in_year[2][13] =
#define GET_AMPM_IS_LOCALE TRUE
#define PREFERRED_DATE_TIME_FMT nl_langinfo (D_T_FMT)
#define PREFERRED_ERA_DATE_TIME_FMT nl_langinfo (ERA_D_T_FMT)
#define PREFERRED_DATE_FMT nl_langinfo (D_FMT)
#define PREFERRED_ERA_DATE_FMT nl_langinfo (ERA_D_FMT)
#define PREFERRED_TIME_FMT nl_langinfo (T_FMT)
#define PREFERRED_ERA_TIME_FMT nl_langinfo (ERA_T_FMT)
#define PREFERRED_12HR_TIME_FMT nl_langinfo (T_FMT_AMPM)
static const gint weekday_item[2][7] =
@ -193,10 +190,6 @@ static const gint month_item[2][12] =
#define MONTH_FULL(d) nl_langinfo (month_item[1][g_date_time_get_month (d) - 1])
#define MONTH_FULL_IS_LOCALE TRUE
#define ERA_DESCRIPTION nl_langinfo (ERA)
#define ERA_DESCRIPTION_IS_LOCALE TRUE
#define ERA_DESCRIPTION_N_SEGMENTS (int) (gintptr) nl_langinfo (_NL_TIME_ERA_NUM_ENTRIES)
#else
#define GET_AMPM(d) (get_fallback_ampm (g_date_time_get_hour (d)))
@ -204,15 +197,12 @@ static const gint month_item[2][12] =
/* Translators: this is the preferred format for expressing the date and the time */
#define PREFERRED_DATE_TIME_FMT C_("GDateTime", "%a %b %e %H:%M:%S %Y")
#define PREFERRED_ERA_DATE_TIME_FMT PREFERRED_DATE_TIME_FMT
/* Translators: this is the preferred format for expressing the date */
#define PREFERRED_DATE_FMT C_("GDateTime", "%m/%d/%y")
#define PREFERRED_ERA_DATE_FMT PREFERRED_DATE_FMT
/* Translators: this is the preferred format for expressing the time */
#define PREFERRED_TIME_FMT C_("GDateTime", "%H:%M:%S")
#define PREFERRED_ERA_TIME_FMT PREFERRED_TIME_FMT
/* Translators: this is the preferred format for expressing 12 hour time */
#define PREFERRED_12HR_TIME_FMT C_("GDateTime", "%I:%M:%S %p")
@ -232,10 +222,6 @@ static const gint month_item[2][12] =
#define MONTH_FULL(d) (get_month_name_standalone (g_date_time_get_month (d)))
#define MONTH_FULL_IS_LOCALE FALSE
#define ERA_DESCRIPTION NULL
#define ERA_DESCRIPTION_IS_LOCALE FALSE
#define ERA_DESCRIPTION_N_SEGMENTS 0
static const gchar *
get_month_name_standalone (gint month)
{
@ -572,6 +558,28 @@ get_month_name_abbr_with_day (gint month)
#endif /* HAVE_LANGINFO_ABALTMON */
#ifdef HAVE_LANGINFO_ERA
#define PREFERRED_ERA_DATE_TIME_FMT nl_langinfo (ERA_D_T_FMT)
#define PREFERRED_ERA_DATE_FMT nl_langinfo (ERA_D_FMT)
#define PREFERRED_ERA_TIME_FMT nl_langinfo (ERA_T_FMT)
#define ERA_DESCRIPTION nl_langinfo (ERA)
#define ERA_DESCRIPTION_IS_LOCALE TRUE
#define ERA_DESCRIPTION_N_SEGMENTS (int) (gintptr) nl_langinfo (_NL_TIME_ERA_NUM_ENTRIES)
#else /* if !HAVE_LANGINFO_ERA */
#define PREFERRED_ERA_DATE_TIME_FMT PREFERRED_DATE_TIME_FMT
#define PREFERRED_ERA_DATE_FMT PREFERRED_DATE_FMT
#define PREFERRED_ERA_TIME_FMT PREFERRED_TIME_FMT
#define ERA_DESCRIPTION NULL
#define ERA_DESCRIPTION_IS_LOCALE FALSE
#define ERA_DESCRIPTION_N_SEGMENTS 0
#endif /* !HAVE_LANGINFO_ERA */
/* Format AM/PM indicator if the locale does not have a localized version. */
static const gchar *
get_fallback_ampm (gint hour)
@ -2962,10 +2970,13 @@ date_time_lookup_era (GDateTime *datetime,
if (era_description_str != NULL)
static_era_description = _g_era_description_parse (era_description_str);
if (static_era_description == NULL)
g_warning ("Could not parse ERA description: %s", era_description_str);
}
else
{
g_clear_pointer (&static_era_description, g_ptr_array_unref);
}
if (static_era_description == NULL)
g_warning ("Could not parse ERA description: %s", era_description_str);
g_free (tmp);

View File

@ -1725,12 +1725,21 @@ test_non_utf8_printf (void)
TEST_PRINTF ("%%", "%");
TEST_PRINTF ("%", "");
TEST_PRINTF ("%9", NULL);
#ifdef HAVE_LANGINFO_ERA
TEST_PRINTF ("%Ec", "平成21年10月24日 00時00分00秒");
TEST_PRINTF ("%EC", "平成");
TEST_PRINTF ("%Ex", "平成21年10月24日");
TEST_PRINTF ("%EX", "00時00分00秒");
TEST_PRINTF ("%Ey", "21");
TEST_PRINTF ("%EY", "平成21年");
#else
TEST_PRINTF ("%Ec", "平成21年10月24日 00時00分00秒");
TEST_PRINTF ("%EC", "平成");
TEST_PRINTF ("%Ex", "2009\345\271\26410\346\234\21024\346\227\245");
TEST_PRINTF ("%EX", "00\346\231\20200\345\210\20600\347\247\222");
TEST_PRINTF ("%Ey", "09");
TEST_PRINTF ("%EY", "2009");
#endif
setlocale (LC_ALL, oldlocale);
g_free (oldlocale);
@ -1889,14 +1898,25 @@ test_modifiers (void)
setlocale (LC_ALL, "en_GB.utf-8");
if (strstr (setlocale (LC_ALL, NULL), "en_GB") != NULL)
{
#ifndef __APPLE__
TEST_PRINTF_DATE (2009, 1, 1, "%c", "thu 01 jan 2009 00:00:00 utc");
TEST_PRINTF_DATE (2009, 1, 1, "%Ec", "thu 01 jan 2009 00:00:00 utc");
#else
/* macOS uses a figure space (U+2007) to pad the day */
TEST_PRINTF_DATE (2009, 1, 1, "%c", "thu " "\xe2\x80\x87" "1 jan 00:00:00 2009");
TEST_PRINTF_DATE (2009, 1, 1, "%Ec", "thu " "\xe2\x80\x87" "1 jan 00:00:00 2009");
#endif
TEST_PRINTF_DATE (2009, 1, 1, "%C", "20");
TEST_PRINTF_DATE (2009, 1, 1, "%EC", "20");
#ifndef __APPLE__
TEST_PRINTF_DATE (2009, 1, 2, "%x", "02/01/09");
TEST_PRINTF_DATE (2009, 1, 2, "%Ex", "02/01/09");
#else
TEST_PRINTF_DATE (2009, 1, 2, "%x", "02/01/2009");
TEST_PRINTF_DATE (2009, 1, 2, "%Ex", "02/01/2009");
#endif
TEST_PRINTF_TIME (1, 2, 3, "%X", "01:02:03");
TEST_PRINTF_TIME (1, 2, 3, "%EX", "01:02:03");
@ -2252,6 +2272,7 @@ test_all_dates (void)
static void
test_date_time_eras_japan (void)
{
#ifdef HAVE_LANGINFO_ERA
gchar *oldlocale;
oldlocale = g_strdup (setlocale (LC_ALL, NULL));
@ -2290,11 +2311,15 @@ test_date_time_eras_japan (void)
setlocale (LC_ALL, oldlocale);
g_free (oldlocale);
#else
g_test_skip ("nl_langinfo(ERA) not supported, skipping era tests");
#endif
}
static void
test_date_time_eras_thailand (void)
{
#ifdef HAVE_LANGINFO_ERA
gchar *oldlocale;
oldlocale = g_strdup (setlocale (LC_ALL, NULL));
@ -2318,6 +2343,9 @@ test_date_time_eras_thailand (void)
setlocale (LC_ALL, oldlocale);
g_free (oldlocale);
#else
g_test_skip ("nl_langinfo(ERA) not supported, skipping era tests");
#endif
}
static void

View File

@ -1311,15 +1311,25 @@ if cc.links('''#include <langinfo.h>
str = nl_langinfo (ABMON_12);
str = nl_langinfo (DAY_1);
str = nl_langinfo (ABDAY_7);
return 0;
}''', name : 'nl_langinfo (PM_STR)')
glib_conf.set('HAVE_LANGINFO_TIME', 1)
endif
# Linux glibc supports ERA, but FreeBSD and macOS dont
if cc.links('''#include <langinfo.h>
int main (int argc, char **argv) {
char *str;
str = nl_langinfo (ERA);
str = nl_langinfo (ERA_D_T_FMT);
str = nl_langinfo (ERA_D_FMT);
str = nl_langinfo (ERA_T_FMT);
str = nl_langinfo (_NL_TIME_ERA_NUM_ENTRIES);
return 0;
}''', name : 'nl_langinfo (PM_STR)')
glib_conf.set('HAVE_LANGINFO_TIME', 1)
}''', name : 'nl_langinfo (ERA)')
glib_conf.set('HAVE_LANGINFO_ERA', 1)
endif
if cc.links('''#include <langinfo.h>
int main (int argc, char ** argv) {
char *str;