Browse code

Replace old timegm code with code that works better on Solaris

Shawn Webb authored on 2014/03/14 05:29:51
Showing 1 changed files
... ...
@@ -62,30 +62,38 @@ char * strptime(const char *buf, const char *fmt, struct tm *tm);
62 62
 #if !defined(HAVE_TIMEGM) && !defined(_WIN32)
63 63
 /*
64 64
  * Solaris 10 and earlier don't have timegm. Provide a portable version of it.
65
- * This function is from the timegm manpage at http://man7.org/linux/man-pages/man3/timegm.3.html
65
+ * A special thank you to Dave Simonson for helping test and develop this.
66 66
  */
67
-time_t timegm(struct tm *tm)
67
+time_t timegm(struct tm *t)
68 68
 {
69
-    time_t ret;
70
-    char *tz;
71
-
72
-    tz = getenv("TZ");
73
-    if (tz)
74
-        tz = strdup(tz);
75
-
76
-    setenv("TZ", "", 1);
77
-    tzset();
78
-
79
-    ret = mktime(tm);
80
-    if (tz) {
81
-        setenv("TZ", tz, 1);
82
-        free(tz);
83
-    } else {
84
-        unsetenv("TZ");
69
+    time_t tl, tb;
70
+    struct tm *tg;
71
+
72
+    tl = mktime (t);
73
+    if (tl == -1)
74
+    {
75
+        t->tm_hour--;
76
+        tl = mktime (t);
77
+        if (tl == -1)
78
+            return -1; /* can't deal with output from strptime */
79
+        tl += 3600;
85 80
     }
86 81
 
87
-    tzset();
88
-    return ret;
82
+    tg = gmtime (&tl);
83
+    tg->tm_isdst = 0;
84
+    tb = mktime (tg);
85
+
86
+    if (tb == -1)
87
+    {
88
+        tg->tm_hour--;
89
+        tb = mktime (tg);
90
+        if (tb == -1)
91
+            return -1; /* can't deal with output from gmtime */
92
+
93
+        tb += 3600;
94
+    }
95
+
96
+    return (tl - (tb - tl));
89 97
 }
90 98
 #endif
91 99