Skip to content
Snippets Groups Projects
Commit 204778f9 authored by Andre Meixner's avatar Andre Meixner :camera:
Browse files

Added bijective time-string to time conversion to SimoxUtility

parent 9a417900
No related branches found
No related tags found
No related merge requests found
......@@ -176,6 +176,25 @@ bool alg::contains(const std::string& haystack, const std::string& needle)
return boost::algorithm::contains(haystack, needle);
}
time_t alg::to_time_t(const std::string &time, const std::string &time_format)
{
std::tm tm = {};
std::istringstream ss(time);
ss.imbue(std::locale());
ss >> std::get_time(&tm, time_format.c_str());
if (ss.fail())
throw error::SimoxError("Failed converting string '" + time + "' to time with time format " + time_format);
else return mktime(&tm);
}
std::string alg::to_string(time_t t, const std::string &time_format)
{
char mbstr[100];
std::strftime(mbstr, 100, time_format.c_str(), std::localtime(&t));
return std::string(mbstr);
}
unsigned long alg::count(const std::string& input, const std::string& search)
{
auto start = 0U;
......
......@@ -144,4 +144,8 @@ namespace simox::alg
{
return multi_to_string(vector.begin(), vector.end());
}
time_t to_time_t(const std::string &time, const std::string &time_format);
std::string to_string(time_t t, const std::string &time_format);
}
......@@ -102,6 +102,13 @@ BOOST_AUTO_TEST_CASE(capitalize_words)
BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), ex.begin(), ex.end());
}
BOOST_AUTO_TEST_CASE(time_conversion)
{
std::string timeString = "2021-01-12";
std::string timeFormat = "%Y-%m-%d";
time_t time = simox::alg::to_time_t(timeString, timeFormat);
BOOST_CHECK_EQUAL(simox::alg::to_string(time, timeFormat), timeString);
}
BOOST_AUTO_TEST_CASE(remove_prefix_suffix)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment