]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: add timegm() portability function to lib/timeutils.c
authorSami Kerola <kerolasa@iki.fi>
Tue, 12 Jul 2016 21:21:10 +0000 (22:21 +0100)
committerSami Kerola <kerolasa@iki.fi>
Fri, 3 Feb 2017 22:31:18 +0000 (22:31 +0000)
Local timegm() is a replacement function in cases it is missing from libc
implementation.  Hopefully the replacement is never, or very rarely, used.

CC: Ruediger Meier <ruediger.meier@ga-group.nl>
Reviewed-by: J William Piggott <elseifthen@gmx.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
configure.ac
include/timeutils.h
lib/timeutils.c
login-utils/utmpdump.c

index 37599212cef04b18a2c783f526b302e6f0e314b4..2dc10e331c14c7405acd56a2d53a9e8228572cb0 100644 (file)
@@ -416,6 +416,7 @@ AC_CHECK_FUNCS([ \
        strnlen \
        sysconf \
        sysinfo \
+       timegm \
        usleep \
        warn \
        warnx \
index 00d18200db9a0fde03bf2a325a04c41f6d1f8aa5..85fc228dbb7e0a46ceed9c9dfa52aee07c87ab8a 100644 (file)
@@ -78,4 +78,8 @@ int time_is_thisyear(const time_t *t, struct timeval *now);
 
 int strtime_short(const time_t *t, struct timeval *now, int flags, char *buf, size_t bufsz);
 
+#ifndef HAVE_TIMEGM
+extern time_t timegm(struct tm *tm);
+#endif
+
 #endif /* UTIL_LINUX_TIME_UTIL_H */
index ea5c0aad8fb79f5822d11bb20cf407d827ada8b7..d38970c108cc1c3c39959f8c6fd02dbefaf1be2f 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <assert.h>
 #include <ctype.h>
+#include <stdlib.h>
 #include <string.h>
 #include <time.h>
 #include <sys/time.h>
@@ -460,6 +461,24 @@ int strtime_short(const time_t *t, struct timeval *now, int flags, char *buf, si
        return rc <= 0 ? -1 : 0;
 }
 
+#ifndef HAVE_TIMEGM
+time_t timegm(struct tm *tm)
+{
+       const char *zone = getenv("TZ");
+       time_t ret;
+
+       setenv("TZ", "", 1);
+       tzset();
+       ret = mktime(tm);
+       if (zone)
+               setenv("TZ", zone, 1);
+       else
+               unsetenv("TZ");
+       tzset();
+       return ret;
+}
+#endif /* HAVE_TIMEGM */
+
 #ifdef TEST_PROGRAM_TIMEUTILS
 
 int main(int argc, char *argv[])
index 91eb4fb557b104fb22c32da44267e889f1b516b9..8e4839346705c8a73e0dbb19f10d1c3f7ced0334 100644 (file)
@@ -45,6 +45,7 @@
 #include "timeutils.h"
 #include "xalloc.h"
 #include "closestream.h"
+#include "timeutils.h"
 
 static time_t strtotime(const char *s_time)
 {