utils: Rename/Add date functions

This commit is contained in:
Albert S. 2022-03-27 08:29:13 +02:00
parent b2a7ea4031
commit c5435c52f4
2 changed files with 17 additions and 2 deletions

View File

@ -166,7 +166,10 @@ std::string utils::regex_callback_replacer(std::regex regex, const std::string &
return result; return result;
} }
std::string utils::toISODate(time_t t) /* TODO: Convert to C++20, but currently the state is rather poor and would
* require workarounds, so keep it this way for now, and do it properly
* once compiler support gets there */
std::string utils::formatLocalDate(time_t t, std::string format)
{ {
struct tm lt; struct tm lt;
if(localtime_r(&t, &lt) == nullptr) if(localtime_r(&t, &lt) == nullptr)
@ -174,7 +177,7 @@ std::string utils::toISODate(time_t t)
return {}; return {};
} }
char result[20]; char result[20];
size_t x = strftime(result, sizeof(result), "%Y-%m-%d %H:%M:%S", &lt); size_t x = strftime(result, sizeof(result), format.c_str(), &lt);
if(x == 0) if(x == 0)
{ {
return {}; return {};
@ -182,6 +185,16 @@ std::string utils::toISODate(time_t t)
return std::string{result}; return std::string{result};
} }
std::string utils::toISODateTime(time_t t)
{
return utils::formatLocalDate(t, "%Y-%m-%d %H:%M:%S");
}
std::string utils::toISODate(time_t t)
{
return utils::formatLocalDate(t, "%Y-%m-%d");
}
std::string utils::trim(std::string_view view) std::string utils::trim(std::string_view view)
{ {
std::string_view chars = " \t\n\r"; std::string_view chars = " \t\n\r";

View File

@ -81,7 +81,9 @@ inline unsigned int toUInt(const std::string &str)
return result; return result;
} }
std::string formatLocalDate(time_t t, std::string format);
std::string toISODate(time_t t); std::string toISODate(time_t t);
std::string toISODateTime(time_t t);
template <class T> inline std::string toString(const T &v) template <class T> inline std::string toString(const T &v)
{ {