[Xfce4-commits] <xfce4-weather-plugin:master> Replace my_timegm() with something that doesn't lock up.

Harald Judt noreply at xfce.org
Wed Aug 1 09:24:01 CEST 2012


Updating branch refs/heads/master
         to c5e86912a6661c6a7b04d66c786693a01a2d45ea (commit)
       from 0e07ddaa53bbde785039dab22c055ae28feb2f76 (commit)

commit c5e86912a6661c6a7b04d66c786693a01a2d45ea
Author: Harald Judt <h.judt at gmx.at>
Date:   Wed Aug 1 09:22:08 2012 +0200

    Replace my_timegm() with something that doesn't lock up.
    
    It seems the original my_timegm from the Linux manpage that is
    supposed to be a portable replacement of timegm() does not work
    very well when we use multiple threads, such as when parsing
    astrological data and weather data at the same time.
    On occasion, the weather plugin would lock up somewhere in
    tzset(). With this new version, this does not happen anymore.
    
    It does do calculations with time_t however, so it may not be
    as portable as it could be.

 panel-plugin/weather-parsers.c |   50 ++++++++++++++++++++++++++-------------
 1 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/panel-plugin/weather-parsers.c b/panel-plugin/weather-parsers.c
index c94ad96..23d8267 100644
--- a/panel-plugin/weather-parsers.c
+++ b/panel-plugin/weather-parsers.c
@@ -34,25 +34,42 @@
 
 
 /*
- * This is a portable replacement for the deprecated timegm(),
- * copied from the man page.
+ * Portable? replacement for the deprecated GNU timegm() only
+ * available on Linux and BSDs. The one recommended by the (Linux!)
+ * man page does not work correctly, it seems threads get stuck in
+ * setenv(TZ).
+ * This one is from http://lists.debian.org/deity/2002/04/msg00082.html
+ * and seems to be rather uncomplicated compared to other solutions.
+ * One problem with it is that it does calculations with time_t, which
+ * is not defined in the standard but works on POSIX-conformant
+ * systems. If anyone uses something different, then this would need
+ * improvement, or an alternative approach needs to be found.
  */
 static time_t
-my_timegm(struct tm *tm)
+my_timegm(struct tm *t)
 {
-    time_t ret;
-    char *tz;
-
-    tz = getenv("TZ");
-    setenv("TZ", "", 1);
-    tzset();
-    ret = mktime(tm);
-    if (tz)
-        setenv("TZ", tz, 1);
-    else
-        unsetenv("TZ");
-    tzset();
-    return ret;
+    time_t tl, tb;
+    struct tm *tg;
+
+    tl = mktime(t);
+    if (tl == -1) {
+        t->tm_hour--;
+        tl = mktime(t);
+        if (tl == -1)
+            return -1; /* can't deal with output from strptime */
+        tl += 3600;
+    }
+    tg = gmtime(&tl);
+    tg->tm_isdst = 0;
+    tb = mktime(tg);
+    if (tb == -1) {
+        tg->tm_hour--;
+        tb = mktime(tg);
+        if (tb == -1)
+            return -1; /* can't deal with output from gmtime */
+        tb += 3600;
+    }
+    return (tl - (tb - tl));
 }
 
 
@@ -107,7 +124,6 @@ parse_xml_timestring(gchar *ts, gchar *format) {
     /* strptime needs an initialized struct, or unpredictable
      * behaviour might occur */
     memset(&tm, 0, sizeof(struct tm));
-    tm.tm_isdst = -1;
 
     if (G_UNLIKELY(strptime(ts, format, &tm) == NULL))
         return t;


More information about the Xfce4-commits mailing list