From a655be823b501063325a0580b05437d7bb8a995f Mon Sep 17 00:00:00 2001 From: Owen Clarke Date: Mon, 16 Jul 2012 22:45:11 +1000 Subject: [PATCH] Bug #1030 - Added portable implementation of timegm for non GNU/BSD platforms that don't have their own implementation - Removed the use of tm_gmtoff on non GNU/BSD platforms that don't have it as part of the tm struct in time.h - Added CMake tests HAVE_TIMEGM and HAVE_TM_GMTOFF --- CMakeLists.txt | 4 ++++ ChangeLog | 2 ++ cmake.h.in | 6 ++++++ src/Nibbler.cpp | 3 +++ src/util.cpp | 20 ++++++++++++++++++++ src/util.h | 4 ++++ 6 files changed, 39 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f3893411..bf3957d15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required (VERSION 2.8) set(CMAKE_LEGACY_CYGWIN_WIN32 0) # Remove when CMake >= 2.8.4 is required include (CheckFunctionExists) +include (CheckStructHasMember) set (HAVE_CMAKE true) @@ -65,6 +66,9 @@ endif (LUA51_FOUND) check_function_exists (random HAVE_RANDOM) check_function_exists (srandom HAVE_SRANDOM) +check_function_exists (timegm HAVE_TIMEGM) + +check_struct_has_member ("struct tm" tm_gmtoff time.h HAVE_TM_GMTOFF) message ("-- Looking for libuuid") if (DARWIN) diff --git a/ChangeLog b/ChangeLog index 18610baaf..18ff00d46 100644 --- a/ChangeLog +++ b/ChangeLog @@ -110,6 +110,8 @@ Bugs modification (thanks to Christoph Lange). + Fixed bug #1028, so that UDA durations are stored in seconds, not in raw form (thank to Uli Martens). + + Fixed bug #1030, which defines a portable implementation of timegm and removes + the use of tm_gmtoff for non GNU/BSD platforms. ------ old releases ------------------------------ diff --git a/cmake.h.in b/cmake.h.in index cb5088433..a3bb1aca1 100644 --- a/cmake.h.in +++ b/cmake.h.in @@ -57,6 +57,12 @@ Override PACKAGE_LANGUAGE, then /* Found srandom */ #cmakedefine HAVE_SRANDOM +/* Found tm_gmtoff */ +#cmakedefine HAVE_TM_GMTOFF + +/* Found timegm */ +#cmakedefine HAVE_TIMEGM + /* Found the uuid library */ #cmakedefine HAVE_UUID #cmakedefine HAVE_UUID_UNPARSE_LOWER diff --git a/src/Nibbler.cpp b/src/Nibbler.cpp index a6f2e57e1..0a967c46b 100644 --- a/src/Nibbler.cpp +++ b/src/Nibbler.cpp @@ -37,6 +37,7 @@ #ifdef NIBBLER_FEATURE_REGEX #include #endif +#include static const char* _uuid_pattern = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; static const unsigned int _uuid_min_length = 9; @@ -701,7 +702,9 @@ bool Nibbler::getDateISO (time_t& t) tms.tm_hour = hour; tms.tm_min = minute; tms.tm_sec = second; +#ifdef HAVE_TM_GMTOFF tms.tm_gmtoff = 0; +#endif t = timegm (&tms); return true; diff --git a/src/util.cpp b/src/util.cpp index e3024be41..63ab05a74 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -610,3 +610,23 @@ const std::string indentProject ( //////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +#ifndef HAVE_TIMEGM +time_t timegm (struct tm *tm) +{ + time_t ret; + char *tz; + tz = getenv ("TZ"); + setenv ("TZ", "UTC", 1); + tzset (); + ret = mktime (tm); + if (tz) + setenv ("TZ", tz, 1); + else + unsetenv ("TZ"); + tzset (); + return ret; +} +#endif +//////////////////////////////////////////////////////////////////////////////// + diff --git a/src/util.h b/src/util.h index 411b76ce2..d5d48831a 100644 --- a/src/util.h +++ b/src/util.h @@ -84,5 +84,9 @@ const std::string indentProject ( const std::string& whitespace = " ", char delimiter = '.'); +#ifndef HAVE_TIMEGM + time_t timegm (struct tm *tm); +#endif + #endif ////////////////////////////////////////////////////////////////////////////////