Guard against the POSIX allowed behavior where access (file, X_OK)
[platform/upstream/glib.git] / glib / gdate.c
index c8569fb..9153a0a 100644 (file)
@@ -2,25 +2,43 @@
  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
+
+/*
+ * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
+ * file for a list of people on the GLib Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GLib at ftp://ftp.gtk.org/pub/gtk/. 
+ */
+
+/* 
+ * MT safe
+ */
+
+#include "config.h"
+
+#define DEBUG_MSG(x)   /* */
+#ifdef G_ENABLE_DEBUG
+/* #define DEBUG_MSG(args)     g_message args ; */
+#endif
+
 #include "glib.h"
 
 #include <time.h>
 #include <string.h>
-#include <ctype.h>
 #include <stdlib.h>
 #include <locale.h>
 
@@ -79,7 +97,7 @@ g_date_free (GDate *d)
 }
 
 gboolean     
-g_date_valid (GDate       *d)
+g_date_valid (const GDate *d)
 {
   g_return_val_if_fail (d != NULL, FALSE);
   
@@ -146,8 +164,9 @@ g_date_valid_dmy (GDateDay     d,
  *   Jan 1, Year 1
  */
 static void
-g_date_update_julian (GDate *d)
+g_date_update_julian (const GDate *const_d)
 {
+  GDate *d = (GDate *) const_d;
   GDateYear year;
   gint index;
   
@@ -180,8 +199,9 @@ g_date_update_julian (GDate *d)
 }
 
 static void 
-g_date_update_dmy (GDate *d)
+g_date_update_dmy (const GDate *const_d)
 {
+  GDate *d = (GDate *) const_d;
   GDateYear y;
   GDateMonth m;
   GDateDay day;
@@ -228,7 +248,7 @@ g_date_update_dmy (GDate *d)
 }
 
 GDateWeekday 
-g_date_weekday (GDate *d)
+g_date_get_weekday (const GDate *d)
 {
   g_return_val_if_fail (d != NULL, G_DATE_BAD_WEEKDAY);
   g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
@@ -243,7 +263,7 @@ g_date_weekday (GDate *d)
 }
 
 GDateMonth   
-g_date_month (GDate *d)
+g_date_get_month (const GDate *d)
 {
   g_return_val_if_fail (d != NULL, G_DATE_BAD_MONTH);
   g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
@@ -258,7 +278,7 @@ g_date_month (GDate *d)
 }
 
 GDateYear    
-g_date_year (GDate *d)
+g_date_get_year (const GDate *d)
 {
   g_return_val_if_fail (d != NULL, G_DATE_BAD_YEAR);
   g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
@@ -273,7 +293,7 @@ g_date_year (GDate *d)
 }
 
 GDateDay     
-g_date_day (GDate *d)
+g_date_get_day (const GDate *d)
 {
   g_return_val_if_fail (d != NULL, G_DATE_BAD_DAY);
   g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
@@ -288,7 +308,7 @@ g_date_day (GDate *d)
 }
 
 guint32      
-g_date_julian (GDate *d)
+g_date_get_julian (const GDate *d)
 {
   g_return_val_if_fail (d != NULL, G_DATE_BAD_JULIAN);
   g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
@@ -303,7 +323,7 @@ g_date_julian (GDate *d)
 }
 
 guint        
-g_date_day_of_year (GDate *d)
+g_date_get_day_of_year (const GDate *d)
 {
   gint index;
   
@@ -322,7 +342,7 @@ g_date_day_of_year (GDate *d)
 }
 
 guint        
-g_date_monday_week_of_year (GDate *d)
+g_date_get_monday_week_of_year (const GDate *d)
 {
   GDateWeekday wd;
   guint day;
@@ -341,14 +361,14 @@ g_date_monday_week_of_year (GDate *d)
   
   g_date_set_dmy (&first, 1, 1, d->year);
   
-  wd = g_date_weekday (&first) - 1; /* make Monday day 0 */
-  day = g_date_day_of_year (d) - 1;
+  wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
+  day = g_date_get_day_of_year (d) - 1;
   
   return ((day + wd)/7U + (wd == 0 ? 1 : 0));
 }
 
 guint        
-g_date_sunday_week_of_year (GDate *d)
+g_date_get_sunday_week_of_year (const GDate *d)
 {
   GDateWeekday wd;
   guint day;
@@ -367,13 +387,26 @@ g_date_sunday_week_of_year (GDate *d)
   
   g_date_set_dmy (&first, 1, 1, d->year);
   
-  wd = g_date_weekday (&first);
+  wd = g_date_get_weekday (&first);
   if (wd == 7) wd = 0; /* make Sunday day 0 */
-  day = g_date_day_of_year (d) - 1;
+  day = g_date_get_day_of_year (d) - 1;
   
   return ((day + wd)/7U + (wd == 0 ? 1 : 0));
 }
 
+gint
+g_date_days_between (const GDate *d1,
+                    const GDate *d2)
+{
+  g_return_val_if_fail (d1 != NULL, 0);
+  g_return_val_if_fail (d2 != NULL, 0);
+
+  g_return_val_if_fail (g_date_valid (d1), 0);
+  g_return_val_if_fail (g_date_valid (d2), 0);
+
+  return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
+}
+
 void         
 g_date_clear (GDate       *d, guint ndates)
 {
@@ -383,6 +416,8 @@ g_date_clear (GDate       *d, guint ndates)
   memset (d, 0x0, ndates*sizeof (GDate)); 
 }
 
+G_LOCK_DEFINE_STATIC (g_date_global);
+
 /* These are for the parser, output to the user should use *
  * g_date_strftime () - this creates more never-freed memory to annoy
  * all those memory debugger users. :-) 
@@ -429,25 +464,26 @@ typedef struct _GDateParseTokens GDateParseTokens;
 
 #define NUM_LEN 10
 
+/* HOLDS: g_date_global_lock */
 static void
 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
 {
   gchar num[4][NUM_LEN+1];
   gint i;
-  const gchar *s;
+  const guchar *s;
   
   /* We count 4, but store 3; so we can give an error
    * if there are 4.
    */
   num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
   
-  s = str;
+  s = (const guchar *) str;
   pt->num_ints = 0;
   while (*s && pt->num_ints < 4) 
     {
       
       i = 0;
-      while (*s && isdigit (*s) && i <= NUM_LEN)
+      while (*s && g_ascii_isdigit (*s) && i <= NUM_LEN)
         {
           num[pt->num_ints][i] = *s;
           ++s; 
@@ -473,41 +509,46 @@ g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
   
   if (pt->num_ints < 3)
     {
-      gchar lcstr[128];
-      int i = 1;
-      
-      strncpy (lcstr, str, 127);
-      g_strdown (lcstr);
+      gchar *casefold;
+      gchar *normalized;
       
+      casefold = g_utf8_casefold (str, -1);
+      normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
+      g_free (casefold);
+
+      i = 1;
       while (i < 13)
         {
           if (long_month_names[i] != NULL) 
             {
-              const gchar *found = strstr (lcstr, long_month_names[i]);
+              const gchar *found = strstr (normalized, long_month_names[i]);
              
               if (found != NULL)
                 {
                   pt->month = i;
-                  return;
+                 break;
                 }
             }
          
           if (short_month_names[i] != NULL) 
             {
-              const gchar *found = strstr (lcstr, short_month_names[i]);
+              const gchar *found = strstr (normalized, short_month_names[i]);
              
               if (found != NULL)
                 {
                   pt->month = i;
-                  return;
+                 break;
                 }
             }
-         
+
           ++i;
         }
+
+      g_free (normalized);
     }
 }
 
+/* HOLDS: g_date_global_lock */
 static void
 g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
 {
@@ -536,28 +577,33 @@ g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
       
       while (i < 13) 
         {
+         gchar *casefold;
+         
           g_date_set_dmy (&d, 1, i, 1);
          
           g_return_if_fail (g_date_valid (&d));
          
           g_date_strftime (buf, 127, "%b", &d);
+
+         casefold = g_utf8_casefold (buf, -1);
           g_free (short_month_names[i]);
-          g_strdown (buf);
-          short_month_names[i] = g_strdup (buf);
-         
-          
+          short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
+         g_free (casefold);
          
           g_date_strftime (buf, 127, "%B", &d);
+         casefold = g_utf8_casefold (buf, -1);
           g_free (long_month_names[i]);
-          g_strdown (buf);
-          long_month_names[i] = g_strdup (buf);
+          long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
+         g_free (casefold);
           
           ++i;
         }
       
       /* Determine DMY order */
       
-      g_date_set_dmy (&d, 4, 7, 1776); /* had to pick a random day */
+      /* had to pick a random day - don't change this, some strftimes
+       * are broken on some days, and this one is good so far. */
+      g_date_set_dmy (&d, 4, 7, 1976);
       
       g_date_strftime (buf, 127, "%x", &d);
       
@@ -576,7 +622,7 @@ g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
               break;
             case 76:
               using_twodigit_years = TRUE; /* FALL THRU */
-            case 1776:
+            case 1976:
               dmy_order[i] = G_DATE_YEAR;
               break;
             default:
@@ -587,17 +633,15 @@ g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
         }
       
 #ifdef G_ENABLE_DEBUG
-      g_message ("**GDate prepared a new set of locale-specific parse rules.");
+      DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
       i = 1;
       while (i < 13) 
         {
-          g_message ("  %s   %s", long_month_names[i], short_month_names[i]);
+          DEBUG_MSG (("  %s   %s", long_month_names[i], short_month_names[i]));
           ++i;
         }
       if (using_twodigit_years)
-        {
-          g_message ("**Using twodigit years with cutoff year: %u", twodigit_start_year);
-        }
+       DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
       { 
         gchar *strings[3];
         i = 0;
@@ -620,8 +664,8 @@ g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
               }
             ++i;
           }
-        g_message ("**Order: %s, %s, %s", strings[0], strings[1], strings[2]);
-        g_message ("**Sample date in this locale: `%s'", buf);
+        DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
+        DEBUG_MSG (("**Sample date in this locale: `%s'", buf));
       }
 #endif
     }
@@ -641,15 +685,19 @@ g_date_set_parse (GDate       *d,
   /* set invalid */
   g_date_clear (d, 1);
   
+  G_LOCK (g_date_global);
+
   g_date_prepare_to_parse (str, &pt);
   
-#ifdef G_ENABLE_DEBUG
-  g_message ("Found %d ints, `%d' `%d' `%d' and written out month %d", 
-            pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month);
-#endif
+  DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d", 
+             pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
   
   
-  if (pt.num_ints == 4) return; /* presumably a typo; bail out. */
+  if (pt.num_ints == 4) 
+    {
+      G_UNLOCK (g_date_global);
+      return; /* presumably a typo; bail out. */
+    }
   
   if (pt.num_ints > 1)
     {
@@ -720,6 +768,13 @@ g_date_set_parse (GDate       *d,
           if (using_twodigit_years && y < 100) 
             y = G_DATE_BAD_YEAR; /* avoids ambiguity */
         }
+      else if (pt.num_ints == 2)
+       {
+         if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
+           {
+             m = pt.month;
+           }
+       }
     }
   else if (pt.num_ints == 1) 
     {
@@ -763,8 +818,9 @@ g_date_set_parse (GDate       *d,
     }
 #ifdef G_ENABLE_DEBUG
   else 
-    g_message ("Rejected DMY %u %u %u", day, m, y);
+    DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
 #endif
+  G_UNLOCK (g_date_global);
 }
 
 void         
@@ -772,28 +828,29 @@ g_date_set_time (GDate *d,
                 GTime  time)
 {
   time_t t = time;
-  struct tm *tm;
+  struct tm tm;
   
   g_return_if_fail (d != NULL);
   
-  tm = localtime (&t);
+#ifdef HAVE_LOCALTIME_R
+  localtime_r (&t, &tm);
+#else
+  {
+    struct tm *ptm = localtime (&t);
+    g_assert (ptm);
+    memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
+  }
+#endif
   
-  if (tm) 
-    {
-      d->julian = FALSE;
-      
-      d->month = tm->tm_mon + 1;
-      d->day   = tm->tm_mday;
-      d->year  = tm->tm_year + 1900;
-      
-      g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
-      
-      d->dmy    = TRUE;
-    }
-  else 
-    {
-      g_date_clear (d, 1);
-    }
+  d->julian = FALSE;
+  
+  d->month = tm.tm_mon + 1;
+  d->day   = tm.tm_mday;
+  d->year  = tm.tm_year + 1900;
+  
+  g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
+  
+  d->dmy    = TRUE;
 }
 
 void         
@@ -802,7 +859,8 @@ g_date_set_month (GDate     *d,
 {
   g_return_if_fail (d != NULL);
   g_return_if_fail (g_date_valid_month (m));
-  
+
+  if (d->julian && !d->dmy) g_date_update_dmy(d);
   d->julian = FALSE;
   
   d->month = m;
@@ -820,6 +878,7 @@ g_date_set_day (GDate     *d,
   g_return_if_fail (d != NULL);
   g_return_if_fail (g_date_valid_day (day));
   
+  if (d->julian && !d->dmy) g_date_update_dmy(d);
   d->julian = FALSE;
   
   d->day = day;
@@ -837,6 +896,7 @@ g_date_set_year (GDate     *d,
   g_return_if_fail (d != NULL);
   g_return_if_fail (g_date_valid_year (y));
   
+  if (d->julian && !d->dmy) g_date_update_dmy(d);
   d->julian = FALSE;
   
   d->year = y;
@@ -878,7 +938,7 @@ g_date_set_julian (GDate *d, guint32 j)
 
 
 gboolean     
-g_date_is_first_of_month (GDate *d)
+g_date_is_first_of_month (const GDate *d)
 {
   g_return_val_if_fail (d != NULL, FALSE);
   g_return_val_if_fail (g_date_valid (d), FALSE);
@@ -894,7 +954,7 @@ g_date_is_first_of_month (GDate *d)
 }
 
 gboolean     
-g_date_is_last_of_month (GDate *d)
+g_date_is_last_of_month (const GDate *d)
 {
   gint index;
   
@@ -1085,8 +1145,8 @@ g_date_is_leap_year (GDateYear  year)
 }
 
 guint8         
-g_date_days_in_month (GDateMonth month, 
-                      GDateYear  year)
+g_date_get_days_in_month (GDateMonth month, 
+                          GDateYear  year)
 {
   gint index;
   
@@ -1099,7 +1159,7 @@ g_date_days_in_month (GDateMonth month,
 }
 
 guint8       
-g_date_monday_weeks_in_year (GDateYear  year)
+g_date_get_monday_weeks_in_year (GDateYear  year)
 {
   GDate d;
   
@@ -1107,21 +1167,21 @@ g_date_monday_weeks_in_year (GDateYear  year)
   
   g_date_clear (&d, 1);
   g_date_set_dmy (&d, 1, 1, year);
-  if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
+  if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
   g_date_set_dmy (&d, 31, 12, year);
-  if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
+  if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
   if (g_date_is_leap_year (year)) 
     {
       g_date_set_dmy (&d, 2, 1, year);
-      if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
+      if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
       g_date_set_dmy (&d, 30, 12, year);
-      if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
+      if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
     }
   return 52;
 }
 
 guint8       
-g_date_sunday_weeks_in_year (GDateYear  year)
+g_date_get_sunday_weeks_in_year (GDateYear  year)
 {
   GDate d;
   
@@ -1129,22 +1189,22 @@ g_date_sunday_weeks_in_year (GDateYear  year)
   
   g_date_clear (&d, 1);
   g_date_set_dmy (&d, 1, 1, year);
-  if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
+  if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
   g_date_set_dmy (&d, 31, 12, year);
-  if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
+  if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
   if (g_date_is_leap_year (year)) 
     {
       g_date_set_dmy (&d, 2, 1, year);
-      if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
+      if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
       g_date_set_dmy (&d, 30, 12, year);
-      if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
+      if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
     }
   return 52;
 }
 
 gint         
-g_date_compare (GDate     *lhs, 
-                GDate     *rhs)
+g_date_compare (const GDate *lhs, 
+                const GDate *rhs)
 {
   g_return_val_if_fail (lhs != NULL, 0);
   g_return_val_if_fail (rhs != NULL, 0);
@@ -1194,7 +1254,7 @@ g_date_compare (GDate     *lhs,
 
 
 void        
-g_date_to_struct_tm (GDate      *d, 
+g_date_to_struct_tm (const GDate *d, 
                      struct tm   *tm)
 {
   GDateWeekday day;
@@ -1223,39 +1283,150 @@ g_date_to_struct_tm (GDate      *d,
   tm->tm_mon  = d->month - 1; /* 0-11 goes in tm */
   tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
   
-  day = g_date_weekday (d);
+  day = g_date_get_weekday (d);
   if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
   
   tm->tm_wday = (int)day;
   
-  tm->tm_yday = g_date_day_of_year (d) - 1; /* 0 to 365 */
+  tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
   tm->tm_isdst = -1; /* -1 means "information not available" */
 }
 
+void
+g_date_clamp (GDate *date,
+             const GDate *min_date,
+             const GDate *max_date)
+{
+  g_return_if_fail (date);
+  g_return_if_fail (g_date_valid (date));
+  if (min_date != NULL)
+    g_return_if_fail (g_date_valid (min_date));
+  if (max_date != NULL)
+    g_return_if_fail (g_date_valid (max_date));
+  if (min_date != NULL && max_date != NULL)
+    g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
+
+  if (min_date && g_date_compare (date, min_date) < 0)
+    *date = *min_date;
+
+  if (max_date && g_date_compare (max_date, date) < 0)
+    *date = *max_date;
+}
+
+void
+g_date_order (GDate *date1,
+              GDate *date2)
+{
+  g_return_if_fail (date1 != NULL);
+  g_return_if_fail (date2 != NULL);
+  g_return_if_fail (g_date_valid (date1));
+  g_return_if_fail (g_date_valid (date2));
+
+  if (g_date_compare (date1, date2) > 0)
+    {
+      GDate tmp = *date1;
+      *date1 = *date2;
+      *date2 = tmp;
+    }
+}
+
 gsize     
 g_date_strftime (gchar       *s, 
                  gsize        slen, 
                  const gchar *format, 
-                 GDate       *d)
+                 const GDate *d)
 {
   struct tm tm;
+  gsize locale_format_len = 0;
+  gchar *locale_format;
+  gsize tmplen;
+  gchar *tmpbuf;
+  gsize tmpbufsize;
+  gsize convlen = 0;
+  gchar *convbuf;
+  GError *error = NULL;
   gsize retval;
-  
+
   g_return_val_if_fail (d != NULL, 0);
   g_return_val_if_fail (g_date_valid (d), 0);
   g_return_val_if_fail (slen > 0, 0); 
   g_return_val_if_fail (format != 0, 0);
   g_return_val_if_fail (s != 0, 0);
-  
+
   g_date_to_struct_tm (d, &tm);
-  
-  retval = strftime (s, slen, format, &tm);
-  if (retval == 0)
+
+  locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
+
+  if (error)
+    {
+      g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
+      g_error_free (error);
+
+      s[0] = '\0';
+      return 0;
+    }
+
+  tmpbufsize = MAX (128, locale_format_len * 2);
+  while (TRUE)
     {
-      /* If retval == 0, the contents of s are undefined.  We define
-       *  them. 
+      tmpbuf = g_malloc (tmpbufsize);
+
+      /* Set the first byte to something other than '\0', to be able to
+       * recognize whether strftime actually failed or just returned "".
        */
+      tmpbuf[0] = '\1';
+      tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
+
+      if (tmplen == 0 && tmpbuf[0] != '\0')
+        {
+          g_free (tmpbuf);
+          tmpbufsize *= 2;
+
+          if (tmpbufsize > 65536)
+            {
+              g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
+              g_free (locale_format);
+
+              s[0] = '\0';
+              return 0;
+            }
+        }
+      else
+        break;
+    }
+  g_free (locale_format);
+
+  convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
+  g_free (tmpbuf);
+
+  if (error)
+    {
+      g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
+      g_error_free (error);
+
       s[0] = '\0';
+      return 0;
+    }
+
+  if (slen <= convlen)
+    {
+      /* Ensure only whole characters are copied into the buffer.
+       */
+      gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
+      g_assert (end != NULL);
+      convlen = end - convbuf;
+
+      /* Return 0 because the buffer isn't large enough.
+       */
+      retval = 0;
     }
+  else
+    retval = convlen;
+
+  memcpy (s, convbuf, convlen);
+  s[convlen] = '\0';
+  g_free (convbuf);
+
   return retval;
 }
+