From f4ec2174c8ca1ecb0d266d6c0aa3fdde337b34d8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Franti=C5=A1ek=20Dvo=C5=99=C3=A1k?= Date: Thu, 12 Jul 2012 12:02:49 +0000 Subject: [PATCH] Use less portable timegm() instead of mktime() and setenv(). Not touching environment is better for thread applications and the code is reduced. --- org.glite.lbjp-common.db/src/db.c | 39 +++------------------------------------ 1 file changed, 3 insertions(+), 36 deletions(-) diff --git a/org.glite.lbjp-common.db/src/db.c b/org.glite.lbjp-common.db/src/db.c index fa3e0c5..3ac36bd 100644 --- a/org.glite.lbjp-common.db/src/db.c +++ b/org.glite.lbjp-common.db/src/db.c @@ -21,8 +21,8 @@ limitations under the License. #include #include #include -#include #include +#include #include #include @@ -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); } -- 1.8.2.3