tests: Move make_tm helper function to utils file

Preparation to be able to use the function from different test files later on.
This commit is contained in:
Julian Brost 2025-04-25 16:09:54 +02:00
parent a65f2d6b41
commit de4b58a04f
4 changed files with 46 additions and 29 deletions

View File

@ -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

View File

@ -2,6 +2,7 @@
#include "base/utility.hpp"
#include "icinga/legacytimeperiod.hpp"
#include "test/utils.hpp"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/date_time/posix_time/posix_time_duration.hpp>
@ -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;

36
test/utils.cpp Normal file
View File

@ -0,0 +1,36 @@
/* Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+ */
#include "utils.hpp"
#include <cstring>
#include <iomanip>
#include <sstream>
#include <boost/test/unit_test.hpp>
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;
}

8
test/utils.hpp Normal file
View File

@ -0,0 +1,8 @@
/* Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+ */
#pragma once
#include <ctime>
#include <string>
tm make_tm(std::string s);