From de4b58a04fdc53a1a678c49d819f75e24ffe5fff Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Fri, 25 Apr 2025 16:09:54 +0200 Subject: [PATCH] tests: Move make_tm helper function to utils file Preparation to be able to use the function from different test files later on. --- test/CMakeLists.txt | 1 + test/icinga-legacytimeperiod.cpp | 30 +------------------------- test/utils.cpp | 36 ++++++++++++++++++++++++++++++++ test/utils.hpp | 8 +++++++ 4 files changed, 46 insertions(+), 29 deletions(-) create mode 100644 test/utils.cpp create mode 100644 test/utils.hpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 810b35bef..8ed300c1f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -57,6 +57,7 @@ add_boost_test(types set(base_test_SOURCES icingaapplication-fixture.cpp + utils.cpp base-array.cpp base-base64.cpp base-convert.cpp diff --git a/test/icinga-legacytimeperiod.cpp b/test/icinga-legacytimeperiod.cpp index 8661b75f7..d279a1cbb 100644 --- a/test/icinga-legacytimeperiod.cpp +++ b/test/icinga-legacytimeperiod.cpp @@ -2,6 +2,7 @@ #include "base/utility.hpp" #include "icinga/legacytimeperiod.hpp" +#include "test/utils.hpp" #include #include #include @@ -471,35 +472,6 @@ BOOST_AUTO_TEST_CASE(advanced) AdvancedHelper("09:00:03-30:00:04", {{2014, 9, 24}, {9, 0, 3}}, {{2014, 9, 25}, {6, 0, 4}}); } -tm make_tm(std::string s) -{ - int dst = -1; - size_t l = strlen("YYYY-MM-DD HH:MM:SS"); - if (s.size() > l) { - std::string zone = s.substr(l); - if (zone == " PST") { - dst = 0; - } else if (zone == " PDT") { - dst = 1; - } else { - // tests should only use PST/PDT (for now) - BOOST_CHECK_MESSAGE(false, "invalid or unknown time time: " << zone); - } - } - - std::tm t = {}; -#if defined(__GNUC__) && __GNUC__ < 5 - // GCC did not implement std::get_time() until version 5 - strptime(s.c_str(), "%Y-%m-%d %H:%M:%S", &t); -#else /* defined(__GNUC__) && __GNUC__ < 5 */ - std::istringstream stream(s); - stream >> std::get_time(&t, "%Y-%m-%d %H:%M:%S"); -#endif /* defined(__GNUC__) && __GNUC__ < 5 */ - t.tm_isdst = dst; - - return t; -} - time_t make_time_t(const tm* t) { tm copy = *t; diff --git a/test/utils.cpp b/test/utils.cpp new file mode 100644 index 000000000..4008db4a1 --- /dev/null +++ b/test/utils.cpp @@ -0,0 +1,36 @@ +/* Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+ */ + +#include "utils.hpp" +#include +#include +#include +#include + +tm make_tm(std::string s) +{ + int dst = -1; + size_t l = strlen("YYYY-MM-DD HH:MM:SS"); + if (s.size() > l) { + std::string zone = s.substr(l); + if (zone == " PST") { + dst = 0; + } else if (zone == " PDT") { + dst = 1; + } else { + // tests should only use PST/PDT (for now) + BOOST_CHECK_MESSAGE(false, "invalid or unknown time time: " << zone); + } + } + + std::tm t = {}; +#if defined(__GNUC__) && __GNUC__ < 5 + // GCC did not implement std::get_time() until version 5 + strptime(s.c_str(), "%Y-%m-%d %H:%M:%S", &t); +#else /* defined(__GNUC__) && __GNUC__ < 5 */ + std::istringstream stream(s); + stream >> std::get_time(&t, "%Y-%m-%d %H:%M:%S"); +#endif /* defined(__GNUC__) && __GNUC__ < 5 */ + t.tm_isdst = dst; + + return t; +} diff --git a/test/utils.hpp b/test/utils.hpp new file mode 100644 index 000000000..213c10e4b --- /dev/null +++ b/test/utils.hpp @@ -0,0 +1,8 @@ +/* Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+ */ + +#pragma once + +#include +#include + +tm make_tm(std::string s);