9 namespace DateTimeUtils {
14 [[nodiscard]]
static std::string
toString(
const std::time_t& time,
const std::string_view& format =
"%Y-%m-%d %H:%M:%S") {
18 localtime_s(&tm_, &time);
20 tm_ = *std::localtime(&time);
22 std::strftime(buffer,
sizeof(buffer), format.data(), &tm_);
23 return std::string{buffer};
30 [[nodiscard]]
static std::time_t
fromString(
const std::string& dateTimeStr,
const std::string_view& format =
"%Y-%m-%d %H:%M:%S") {
32 std::istringstream iss{dateTimeStr};
33 iss >> std::get_time(&tm, format.data());
34 return std::mktime(&tm);
40 [[nodiscard]]
static std::string
now(
const std::string_view& format =
"%Y-%m-%d %H:%M:%S") {
41 const std::time_t tm = std::time(
nullptr);
static std::string now(const std::string_view &format="%Y-%m-%d %H:%M:%S")
returns current date and time as a string with a specific format
static std::string toString(const std::time_t &time, const std::string_view &format="%Y-%m-%d %H:%M:%S")
Converts an std::time_t to a string date time with a specific format.
static std::time_t fromString(const std::string &dateTimeStr, const std::string_view &format="%Y-%m-%d %H:%M:%S")
Converts a string date time with a specific format to an std::time_t.