From b72a75e993d60d50cb9ed067aaf83197b3b1c9e1 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Tue, 12 Jul 2016 22:21:10 +0100 Subject: lib: add timegm() portability function to lib/timeutils.c 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 Reviewed-by: J William Piggott Signed-off-by: Sami Kerola --- configure.ac | 1 + include/timeutils.h | 4 ++++ lib/timeutils.c | 19 +++++++++++++++++++ login-utils/utmpdump.c | 1 + 4 files changed, 25 insertions(+) diff --git a/configure.ac b/configure.ac index 37599212c..2dc10e331 100644 --- a/configure.ac +++ b/configure.ac @@ -416,6 +416,7 @@ AC_CHECK_FUNCS([ \ strnlen \ sysconf \ sysinfo \ + timegm \ usleep \ warn \ warnx \ diff --git a/include/timeutils.h b/include/timeutils.h index 00d18200d..85fc228db 100644 --- a/include/timeutils.h +++ b/include/timeutils.h @@ -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 */ diff --git a/lib/timeutils.c b/lib/timeutils.c index ea5c0aad8..d38970c10 100644 --- a/lib/timeutils.c +++ b/lib/timeutils.c @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -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[]) diff --git a/login-utils/utmpdump.c b/login-utils/utmpdump.c index 91eb4fb55..8e4839346 100644 --- a/login-utils/utmpdump.c +++ b/login-utils/utmpdump.c @@ -45,6 +45,7 @@ #include "timeutils.h" #include "xalloc.h" #include "closestream.h" +#include "timeutils.h" static time_t strtotime(const char *s_time) { -- cgit v1.2.3-55-g7522