mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-19 07:08:54 +02:00
datetime: factor out a common function
The 'p' and 'P' cases are exactly the same, except for one line. Factor out a common function for both. https://bugzilla.gnome.org/show_bug.cgi?id=761889
This commit is contained in:
@@ -2196,6 +2196,48 @@ format_number (GString *str,
|
|||||||
g_string_append (str, tmp[--i]);
|
g_string_append (str, tmp[--i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
format_ampm (GDateTime *datetime,
|
||||||
|
GString *outstr,
|
||||||
|
gboolean locale_is_utf8,
|
||||||
|
gboolean uppercase)
|
||||||
|
{
|
||||||
|
gchar *ampm;
|
||||||
|
gchar *tmp;
|
||||||
|
gsize tmp_len;
|
||||||
|
|
||||||
|
ampm = (gchar *) GET_AMPM (datetime);
|
||||||
|
#if defined (HAVE_LANGINFO_TIME)
|
||||||
|
if (!locale_is_utf8)
|
||||||
|
{
|
||||||
|
/* This assumes that locale encoding can't have embedded NULs */
|
||||||
|
ampm = tmp = g_locale_to_utf8 (ampm, -1, NULL, NULL, NULL);
|
||||||
|
if (!tmp)
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (uppercase)
|
||||||
|
ampm = g_utf8_strup (ampm, -1);
|
||||||
|
else
|
||||||
|
ampm = g_utf8_strdown (ampm, -1);
|
||||||
|
tmp_len = strlen (ampm);
|
||||||
|
if (!locale_is_utf8)
|
||||||
|
{
|
||||||
|
#if defined (HAVE_LANGINFO_TIME)
|
||||||
|
g_free (tmp);
|
||||||
|
#endif
|
||||||
|
tmp = g_locale_from_utf8 (ampm, -1, NULL, &tmp_len, NULL);
|
||||||
|
g_free (ampm);
|
||||||
|
if (!tmp)
|
||||||
|
return FALSE;
|
||||||
|
ampm = tmp;
|
||||||
|
}
|
||||||
|
g_string_append_len (outstr, ampm, tmp_len);
|
||||||
|
g_free (ampm);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
static gboolean g_date_time_format_locale (GDateTime *datetime,
|
static gboolean g_date_time_format_locale (GDateTime *datetime,
|
||||||
const gchar *format,
|
const gchar *format,
|
||||||
GString *outstr,
|
GString *outstr,
|
||||||
@@ -2244,7 +2286,6 @@ g_date_time_format_locale (GDateTime *datetime,
|
|||||||
gboolean alt_digits = FALSE;
|
gboolean alt_digits = FALSE;
|
||||||
gboolean pad_set = FALSE;
|
gboolean pad_set = FALSE;
|
||||||
gchar *pad = "";
|
gchar *pad = "";
|
||||||
gchar *ampm;
|
|
||||||
const gchar *name;
|
const gchar *name;
|
||||||
const gchar *tz;
|
const gchar *tz;
|
||||||
|
|
||||||
@@ -2436,58 +2477,12 @@ g_date_time_format_locale (GDateTime *datetime,
|
|||||||
alt_digits = TRUE;
|
alt_digits = TRUE;
|
||||||
goto next_mod;
|
goto next_mod;
|
||||||
case 'p':
|
case 'p':
|
||||||
ampm = (gchar *) GET_AMPM (datetime);
|
if (!format_ampm (datetime, outstr, locale_is_utf8, TRUE))
|
||||||
#if defined (HAVE_LANGINFO_TIME)
|
return FALSE;
|
||||||
if (!locale_is_utf8)
|
break;
|
||||||
{
|
|
||||||
/* This assumes that locale encoding can't have embedded NULs */
|
|
||||||
ampm = tmp = g_locale_to_utf8 (ampm, -1, NULL, NULL, NULL);
|
|
||||||
if (!tmp)
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
ampm = g_utf8_strup (ampm, -1);
|
|
||||||
tmp_len = strlen (ampm);
|
|
||||||
if (!locale_is_utf8)
|
|
||||||
{
|
|
||||||
#if defined (HAVE_LANGINFO_TIME)
|
|
||||||
g_free (tmp);
|
|
||||||
#endif
|
|
||||||
tmp = g_locale_from_utf8 (ampm, -1, NULL, &tmp_len, NULL);
|
|
||||||
g_free (ampm);
|
|
||||||
if (!tmp)
|
|
||||||
return FALSE;
|
|
||||||
ampm = tmp;
|
|
||||||
}
|
|
||||||
g_string_append_len (outstr, ampm, tmp_len);
|
|
||||||
g_free (ampm);
|
|
||||||
break;
|
|
||||||
case 'P':
|
case 'P':
|
||||||
ampm = (gchar *) GET_AMPM (datetime);
|
if (!format_ampm (datetime, outstr, locale_is_utf8, FALSE))
|
||||||
#if defined (HAVE_LANGINFO_TIME)
|
return FALSE;
|
||||||
if (!locale_is_utf8)
|
|
||||||
{
|
|
||||||
/* This assumes that locale encoding can't have embedded NULs */
|
|
||||||
ampm = tmp = g_locale_to_utf8 (ampm, -1, NULL, NULL, NULL);
|
|
||||||
if (!tmp)
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
ampm = g_utf8_strdown (ampm, -1);
|
|
||||||
tmp_len = strlen (ampm);
|
|
||||||
if (!locale_is_utf8)
|
|
||||||
{
|
|
||||||
#if defined (HAVE_LANGINFO_TIME)
|
|
||||||
g_free (tmp);
|
|
||||||
#endif
|
|
||||||
tmp = g_locale_from_utf8 (ampm, -1, NULL, &tmp_len, NULL);
|
|
||||||
g_free (ampm);
|
|
||||||
if (!tmp)
|
|
||||||
return FALSE;
|
|
||||||
ampm = tmp;
|
|
||||||
}
|
|
||||||
g_string_append_len (outstr, ampm, tmp_len);
|
|
||||||
g_free (ampm);
|
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user