[libgda/LIBGDA_4.2] Use localtime_r and localtime_s when possible



commit d581a44eb036ec05d7c4feaca59f1d3cbe9ae3e0
Author: Vivien Malerba <malerba gnome-db org>
Date:   Mon Jun 6 20:55:01 2011 +0200

    Use localtime_r and localtime_s when possible

 configure.ac                                     |    1 +
 libgda-ui/data-entries/gdaui-entry-common-time.c |    9 ++++++
 libgda/gda-init.c                                |    8 ++++-
 libgda/gda-value.c                               |   14 ++++++++-
 libgda/handlers/gda-handler-time.c               |   27 ++++++++++++++---
 providers/ldap/gda-ldap-util.c                   |   15 ++++++++++
 tools/browser/ldap-browser/entry-properties.c    |   33 ++++++++++++++--------
 7 files changed, 88 insertions(+), 19 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 34bf1d8..da27fd8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -62,6 +62,7 @@ AC_SUBST(INTLTOOL_XML_RULE)
 AC_CHECK_SIZEOF(unsigned int,0)
 AC_CHECK_SIZEOF(unsigned long int,0)
 AC_CHECK_TYPE(uint8_t, unsigned char)
+AC_CHECK_FUNCS(localtime_r localtime_s)
 
 GDA_BUILDDATE=`date '+%F'`
 AC_SUBST(GDA_BUILDDATE, $GDA_BUILDDATE)
diff --git a/libgda-ui/data-entries/gdaui-entry-common-time.c b/libgda-ui/data-entries/gdaui-entry-common-time.c
index a130555..79e66d2 100644
--- a/libgda-ui/data-entries/gdaui-entry-common-time.c
+++ b/libgda-ui/data-entries/gdaui-entry-common-time.c
@@ -874,7 +874,16 @@ date_calendar_choose_cb (GtkWidget *button, GdauiEntryCommonTime *mgtim)
                 struct tm *stm;
 
                 now = time (NULL);
+#ifdef HAVE_LOCALTIME_R
+		struct tm tmpstm;
+		stm = localtime_r (&now, &tmpstm);
+#elif HAVE_LOCALTIME_S
+		struct tm tmpstm;
+		g_assert (localtime_s (&tmpstm, &now) == 0);
+		stm = &tmpstm;
+#else
                 stm = localtime (&now);
+#endif
                 year = stm->tm_year + 1900;
                 month = stm->tm_mon;
                 day = stm->tm_mday;
diff --git a/libgda/gda-init.c b/libgda/gda-init.c
index 0dd9ca7..500012b 100644
--- a/libgda/gda-init.c
+++ b/libgda/gda-init.c
@@ -1,5 +1,8 @@
 /*
- * Copyright (C) 1998 - 2008 The GNOME Foundation.
+ * Copyright (C) 1998 - 2011 The GNOME Foundation.
+ *
+ * AUTHORS:
+ *      Vivien Malerba <malerba gnome-db org>
  *
  * This Library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public License as
@@ -171,6 +174,9 @@ gda_init (void)
 	type = GDA_TYPE_ERROR;
 	g_assert (type);
 
+	/* force TZ init */
+	tzset ();
+
 	/* acquire locale */
 	gda_locale_changed ();
 
diff --git a/libgda/gda-value.c b/libgda/gda-value.c
index 17f2046..7562d11 100644
--- a/libgda/gda-value.c
+++ b/libgda/gda-value.c
@@ -1251,7 +1251,19 @@ gda_value_new_timestamp_from_timet (time_t val)
 	struct tm *ltm;
 
         value = g_new0 (GValue, 1);
-        ltm = localtime ((const time_t *) &val);
+#ifdef HAVE_LOCALTIME_R
+	struct tm tmpstm;
+	ltm = localtime_r ((const time_t *) &val, &tmpstm);
+#elif HAVE_LOCALTIME_S
+	struct tm tmpstm;
+	if (localtime_s (&tmpstm, (const time_t *) &val) == 0)
+		ltm = &tmpstm;
+	else
+		ltm = NULL;
+#else
+	ltm = localtime ((const time_t *) &val);
+#endif
+
         if (ltm) {
                 GdaTimestamp tstamp;
                 tstamp.year = ltm->tm_year + 1900;
diff --git a/libgda/handlers/gda-handler-time.c b/libgda/handlers/gda-handler-time.c
index 5393f5e..6e92fb6 100644
--- a/libgda/handlers/gda-handler-time.c
+++ b/libgda/handlers/gda-handler-time.c
@@ -1,6 +1,5 @@
-/* gda-handler-time.c
- *
- * Copyright (C) 2003 - 2010 Vivien Malerba
+/*
+ * Copyright (C) 2003 - 2011 Vivien Malerba
  *
  * This Library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public License as
@@ -358,7 +357,7 @@ handler_compute_locale (GdaHandlerTime *hdl)
 		time_t now;
 		struct tm *now_tm;
 
-		for (i=0; i<3; i++) {
+		for (i=0; i < 3; i++) {
 			switch (nums[i]) {
 			case 7:
 				hdl->priv->str_locale->dmy_order[i] = G_DATE_MONTH;
@@ -377,7 +376,16 @@ handler_compute_locale (GdaHandlerTime *hdl)
 		}
 		
 		now = time (NULL);
-		now_tm = localtime (&now);
+#ifdef HAVE_LOCALTIME_R
+		struct tm tmpstm;
+		now_tm = localtime_r (&now, &tmpstm);
+#elif HAVE_LOCALTIME_S
+		struct tm tmpstm;
+		g_assert (localtime_s (&tmpstm, &now) == 0);
+		now_tm = &tmpstm;
+#else
+                now_tm = localtime (&now);
+#endif
 		hdl->priv->str_locale->current_offset = ((now_tm->tm_year + 1900) / 100) * 100;
 
 #ifdef GDA_DEBUG_NO		
@@ -1118,7 +1126,16 @@ gda_handler_time_get_sane_init_value (GdaDataHandler *iface, GType type)
 	g_return_val_if_fail (hdl->priv, NULL);
 
 	now = time (NULL);
+#ifdef HAVE_LOCALTIME_R
+	struct tm tmpstm;
+	stm = localtime_r (&now, &tmpstm);
+#elif HAVE_LOCALTIME_S
+	struct tm tmpstm;
+	g_assert (localtime_s (&tmpstm, &now) == 0);
+	stm = &tmpstm;
+#else
 	stm = localtime (&now);
+#endif
 
 	if (type == G_TYPE_DATE) {
 		GDate *gdate;
diff --git a/providers/ldap/gda-ldap-util.c b/providers/ldap/gda-ldap-util.c
index fbf5f8d..edc8379 100644
--- a/providers/ldap/gda-ldap-util.c
+++ b/providers/ldap/gda-ldap-util.c
@@ -849,7 +849,22 @@ gda_ldap_attr_value_to_g_value (LdapConnectionData *cdata, GType type, BerValue
 		}
 		if (conv) {
 			struct tm *ptm;
+#ifdef HAVE_LOCALTIME_R
+			struct tm tmpstm;
+			ptm = localtime_r (&(tv.tv_sec), &tmpstm);
+#elif HAVE_LOCALTIME_S
+			struct tm tmpstm;
+			if (localtime_s (&tmpstm, &(tv.tv_sec)) == 0)
+				ptm = &tmpstm;
+			else
+				ptm = NULL;
+#else
 			ptm = localtime (&(tv.tv_sec));
+#endif
+
+			if (!ptm)
+				return NULL;
+
 			if (type == GDA_TYPE_TIMESTAMP) {
 				GdaTimestamp ts;
 				ts.year = ptm->tm_year + 1900;
diff --git a/tools/browser/ldap-browser/entry-properties.c b/tools/browser/ldap-browser/entry-properties.c
index 52f5a44..ff62d17 100644
--- a/tools/browser/ldap-browser/entry-properties.c
+++ b/tools/browser/ldap-browser/entry-properties.c
@@ -579,25 +579,34 @@ ad_1601_timestamp_to_string (const gchar *value, const gchar *attname)
 		return NULL;
 
 	GdaDataHandler *dh;
-	struct tm stm, *pstm;
+	struct tm *stm;
 	GValue tvalue;
 	GdaTimestamp ts;
 	time_t nsec = (time_t) i64;
 	gchar *str;
-
-#ifdef G_OS_WIN32
-	pstm = localtime (&nsec);
+#ifdef HAVE_LOCALTIME_R
+	struct tm tmpstm;
+	stm = localtime_r (&nsec, &tmpstm);
+#elif HAVE_LOCALTIME_S
+	struct tm tmpstm;
+	if (localtime_s (&tmpstm, &nsec) == 0)
+		stm = &tmpstm;
+	else
+		stm = NULL;
 #else
-	localtime_r (&nsec, &stm);
-	pstm = &stm;
+	stm = localtime (&nsec);
 #endif
+
+	if (!stm)
+		return NULL;
+
 	memset (&ts, 0, sizeof (GdaTimestamp));
-	ts.year = pstm->tm_year + 1900;
-	ts.month = pstm->tm_mon + 1;
-	ts.day = pstm->tm_mday;
-	ts.hour = pstm->tm_hour;
-	ts.minute = pstm->tm_min;
-	ts.second = pstm->tm_sec;
+	ts.year = stm->tm_year + 1900;
+	ts.month = stm->tm_mon + 1;
+	ts.day = stm->tm_mday;
+	ts.hour = stm->tm_hour;
+	ts.minute = stm->tm_min;
+	ts.second = stm->tm_sec;
 	ts.timezone = GDA_TIMEZONE_INVALID;
 	memset (&tvalue, 0, sizeof (GValue));
 	gda_value_set_timestamp (&tvalue, &ts);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]