Use less portable timegm() instead of mktime() and setenv(). Not touching environment...
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Thu, 12 Jul 2012 12:02:49 +0000 (12:02 +0000)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Thu, 12 Jul 2012 12:02:49 +0000 (12:02 +0000)
org.glite.lbjp-common.db/src/db.c

index fa3e0c5..3ac36bd 100644 (file)
@@ -21,8 +21,8 @@ limitations under the License.
 #include <errno.h>
 #include <pthread.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
+#include <time.h>
 #include <unistd.h>
 
 #include <glite/lbu/trio.h>
@@ -132,39 +132,6 @@ void glite_lbu_TimestampToStr(double t, char **str) {
 }
 
 
-static time_t tm2time(struct tm *tm) {
-       static struct tm tm_last = { tm_year:0, tm_mon:0 };
-       static time_t t = (time_t)-1;
-       static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
-       char *tz;
-
-       pthread_mutex_lock(&lock);
-       if (tm->tm_year == tm_last.tm_year && tm->tm_mon == tm_last.tm_mon) {
-               t = t + (tm->tm_sec - tm_last.tm_sec)
-                     + (tm->tm_min - tm_last.tm_min) * 60
-                     + (tm->tm_hour - tm_last.tm_hour) * 3600
-                     + (tm->tm_mday - tm_last.tm_mday) * 86400;
-               memcpy(&tm_last, tm, sizeof tm_last);
-       } else {
-               tz = getenv("TZ");
-               if (tz) tz = strdup(tz);
-               setenv("TZ", "UTC", 1);
-               tzset();
-
-               t =  mktime(tm);
-               memcpy(&tm_last, tm, sizeof tm_last);
-
-               if (tz) setenv("TZ", tz, 1);
-               else unsetenv("TZ");
-               free(tz);
-               tzset();
-       }
-       pthread_mutex_unlock(&lock);
-
-       return t;
-}
-
-
 time_t glite_lbu_StrToTime(const char *str) {
        struct tm       tm;
 
@@ -175,7 +142,7 @@ time_t glite_lbu_StrToTime(const char *str) {
        tm.tm_year -= 1900;
        tm.tm_mon--;
 
-       return tm2time(&tm);
+       return timegm(&tm);
 }
 
 
@@ -191,7 +158,7 @@ double glite_lbu_StrToTimestamp(const char *str) {
        tm.tm_mon--;
        tm.tm_sec = sec;
 
-       return (sec - tm.tm_sec) + tm2time(&tm);
+       return (sec - tm.tm_sec) + timegm(&tm);
 }