[libgda] Use localtime_r and localtime_s when possible



commit 8953cbc1d34d4034792d9865c5b3347eff3d37b2
Author: Vivien Malerba <malerba gnome-db org>
Date:   Sat Jun 4 20:46:42 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    |   32 +++++++++++++++------
 7 files changed, 90 insertions(+), 16 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 3b4578d..099a93d 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 7823c58..8f9ed1c 100644
--- a/libgda-ui/data-entries/gdaui-entry-common-time.c
+++ b/libgda-ui/data-entries/gdaui-entry-common-time.c
@@ -877,7 +877,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 fd24523..754e170 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
@@ -169,6 +172,9 @@ gda_init (void)
 	type = G_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 3dfacd2..c49f1cd 100644
--- a/libgda/gda-value.c
+++ b/libgda/gda-value.c
@@ -1160,7 +1160,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 e320997..741eb56 100644
--- a/tools/browser/ldap-browser/entry-properties.c
+++ b/tools/browser/ldap-browser/entry-properties.c
@@ -578,20 +578,34 @@ ad_1601_timestamp_to_string (const gchar *value, const gchar *attname)
 		return NULL;
 
 	GdaDataHandler *dh;
-	struct tm stm;
+	struct tm *stm;
 	GValue tvalue;
 	GdaTimestamp ts;
 	time_t nsec = (time_t) i64;
 	gchar *str;
-	 
-	localtime_r (&nsec, &stm);
+#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
+	stm = localtime (&nsec);
+#endif
+
+	if (!stm)
+		return NULL;
+
 	memset (&ts, 0, sizeof (GdaTimestamp));
-	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.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]