Include config.h to get HAVE_LOCALTIME_R macro.
[platform/upstream/glib.git] / gdate.c
diff --git a/gdate.c b/gdate.c
index 6672436..44a6bad 100644 (file)
--- a/gdate.c
+++ b/gdate.c
  * MT safe
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include "glib.h"
 
 #include <time.h>
@@ -388,7 +392,7 @@ g_date_clear (GDate       *d, guint ndates)
   memset (d, 0x0, ndates*sizeof (GDate)); 
 }
 
-static G_LOCK_DEFINE(g_date_global);
+G_LOCK_DECLARE_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
@@ -652,7 +656,7 @@ g_date_set_parse (GDate       *d,
   /* set invalid */
   g_date_clear (d, 1);
   
-  g_lock (g_date_global);
+  G_LOCK (g_date_global);
 
   g_date_prepare_to_parse (str, &pt);
   
@@ -664,7 +668,7 @@ g_date_set_parse (GDate       *d,
   
   if (pt.num_ints == 4) 
     {
-      g_unlock (g_date_global);
+      G_UNLOCK (g_date_global);
       return; /* presumably a typo; bail out. */
     }
   
@@ -782,7 +786,7 @@ g_date_set_parse (GDate       *d,
   else 
     g_message ("Rejected DMY %u %u %u", day, m, y);
 #endif
-  g_unlock (g_date_global);
+  G_UNLOCK (g_date_global);
 }
 
 void         
@@ -790,28 +794,33 @@ 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
+#  ifdef G_THREADS_ENABLED
+#  warning "the `g_date_set_time' function will not be MT-safe"
+#  warning "because there is no `localtime_r' on your system."
+#  endif
+  {
+    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