utils: Rename/Add date functions
This commit is contained in:
parent
b2a7ea4031
commit
c5435c52f4
17
utils.cpp
17
utils.cpp
@ -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, <) == nullptr)
|
if(localtime_r(&t, <) == 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", <);
|
size_t x = strftime(result, sizeof(result), format.c_str(), <);
|
||||||
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";
|
||||||
|
2
utils.h
2
utils.h
@ -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)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user