diff options
Diffstat (limited to 'e-util')
-rw-r--r-- | e-util/ChangeLog | 8 | ||||
-rw-r--r-- | e-util/e-util.c | 94 | ||||
-rw-r--r-- | e-util/e-util.h | 4 |
3 files changed, 106 insertions, 0 deletions
diff --git a/e-util/ChangeLog b/e-util/ChangeLog index d44416e18e..0c5834debc 100644 --- a/e-util/ChangeLog +++ b/e-util/ChangeLog @@ -1,3 +1,11 @@ +2007-12-03 Matthew Barnes <mbarnes@redhat.com> + + ** Fixes part of bug #392747 + + * e-utils.c (e_get_month_name), (e_get_weekday_name): + New functions cache localized month and weekday names (respectively) + for easier retrieval than resorting to strftime(). + 2007-11-13 Ondrej Jirman <megous@megous.com> ** Fix for bug #494320 diff --git a/e-util/e-util.c b/e-util/e-util.c index 2515e78eb2..625c617a8b 100644 --- a/e-util/e-util.c +++ b/e-util/e-util.c @@ -547,6 +547,100 @@ e_utf8_strftime_fix_am_pm (gchar *str, gsize max, const gchar *fmt, } /** + * e_get_month_name: + * @month: month index + * @abbreviated: if %TRUE, abbreviate the month name + * + * Returns the localized name for @month. If @abbreviated is %TRUE, + * returns the locale's abbreviated month name. + * + * Returns: localized month name + **/ +const gchar * +e_get_month_name (GDateMonth month, + gboolean abbreviated) +{ + /* Make the indices correspond to the enum values. */ + static const gchar *abbr_names[G_DATE_DECEMBER + 1]; + static const gchar *full_names[G_DATE_DECEMBER + 1]; + static gboolean first_time = TRUE; + + g_return_val_if_fail (month >= G_DATE_JANUARY, NULL); + g_return_val_if_fail (month <= G_DATE_DECEMBER, NULL); + + if (G_UNLIKELY (first_time)) { + gchar buffer[256]; + GDateMonth ii; + GDate date; + + memset (abbr_names, 0, sizeof (abbr_names)); + memset (full_names, 0, sizeof (full_names)); + + /* First Julian day was in January. */ + g_date_set_julian (&date, 1); + + for (ii = G_DATE_JANUARY; ii <= G_DATE_DECEMBER; ii++) { + g_date_strftime (buffer, sizeof (buffer), "%b", &date); + abbr_names[ii] = g_intern_string (buffer); + g_date_strftime (buffer, sizeof (buffer), "%B", &date); + full_names[ii] = g_intern_string (buffer); + g_date_add_months (&date, 1); + } + + first_time = FALSE; + } + + return abbreviated ? abbr_names[month] : full_names[month]; +} + +/** + * e_get_weekday_name: + * @weekday: weekday index + * @abbreviated: if %TRUE, abbreviate the weekday name + * + * Returns the localized name for @weekday. If @abbreviated is %TRUE, + * returns the locale's abbreviated weekday name. + * + * Returns: localized weekday name + **/ +const gchar * +e_get_weekday_name (GDateWeekday weekday, + gboolean abbreviated) +{ + /* Make the indices correspond to the enum values. */ + static const gchar *abbr_names[G_DATE_SUNDAY + 1]; + static const gchar *full_names[G_DATE_SUNDAY + 1]; + static gboolean first_time = TRUE; + + g_return_val_if_fail (weekday >= G_DATE_MONDAY, NULL); + g_return_val_if_fail (weekday <= G_DATE_SUNDAY, NULL); + + if (G_UNLIKELY (first_time)) { + gchar buffer[256]; + GDateWeekday ii; + GDate date; + + memset (abbr_names, 0, sizeof (abbr_names)); + memset (full_names, 0, sizeof (full_names)); + + /* First Julian day was a Monday. */ + g_date_set_julian (&date, 1); + + for (ii = G_DATE_MONDAY; ii <= G_DATE_SUNDAY; ii++) { + g_date_strftime (buffer, sizeof (buffer), "%a", &date); + abbr_names[ii] = g_intern_string (buffer); + g_date_strftime (buffer, sizeof (buffer), "%A", &date); + full_names[ii] = g_intern_string (buffer); + g_date_add_days (&date, 1); + } + + first_time = FALSE; + } + + return abbreviated ? abbr_names[weekday] : full_names[weekday]; +} + +/** * e_flexible_strtod: * @nptr: the string to convert to a numeric value. * @endptr: if non-NULL, it returns the character after diff --git a/e-util/e-util.h b/e-util/e-util.h index c2983f454f..e3774c5852 100644 --- a/e-util/e-util.h +++ b/e-util/e-util.h @@ -82,6 +82,10 @@ gsize e_utf8_strftime_fix_am_pm (gchar *str, 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, + gboolean abbreviated); /* String to/from double conversion functions */ gdouble e_flexible_strtod (const gchar *nptr, |