From c5435c52f431821c9f1d877f6ecc4e31414fac2c Mon Sep 17 00:00:00 2001 From: Albert S Date: Sun, 27 Mar 2022 08:29:13 +0200 Subject: [PATCH] utils: Rename/Add date functions --- utils.cpp | 17 +++++++++++++++-- utils.h | 2 ++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/utils.cpp b/utils.cpp index bf7ffa5..c6db75a 100644 --- a/utils.cpp +++ b/utils.cpp @@ -166,7 +166,10 @@ std::string utils::regex_callback_replacer(std::regex regex, const std::string & 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; if(localtime_r(&t, <) == nullptr) @@ -174,7 +177,7 @@ std::string utils::toISODate(time_t t) return {}; } 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) { return {}; @@ -182,6 +185,16 @@ std::string utils::toISODate(time_t t) 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_view chars = " \t\n\r"; diff --git a/utils.h b/utils.h index a83aa60..0e6816c 100644 --- a/utils.h +++ b/utils.h @@ -81,7 +81,9 @@ inline unsigned int toUInt(const std::string &str) return result; } +std::string formatLocalDate(time_t t, std::string format); std::string toISODate(time_t t); +std::string toISODateTime(time_t t); template inline std::string toString(const T &v) {