In my project I have a header file which contains only functions which are put into a namespace, the purpose of these functions is to be used from another class of the framework I'm working on, or from client code.
Does this code follow best practices? How can I efficiently just keep such data?
#pragma once
#include <iomanip>
#include <sstream>
#include <chrono>
#include <ctime>
namespace lwlog::datetime
{
std::string get_chrono(std::string format)
{
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&in_time_t), format.c_str());
return ss.str();
}
std::string get_time() { return get_chrono("%H:%M:%S"); }
std::string get_date() { return get_chrono("%Y-%m-%d"); }
std::string get_date_short() { return get_chrono("%m/%d/%y"); }
std::string get_second() { return get_chrono("%S"); }
std::string get_minute() { return get_chrono("%M"); }
std::string get_hour_24() { return get_chrono("%H"); }
std::string get_hour_12() { return get_chrono("%I"); }
std::string get_weekday() { return get_chrono("%A"); }
std::string get_weekday_abbreviated() { return get_chrono("%a"); }
std::string get_day() { return get_chrono("%d"); }
std::string get_month() { return get_chrono("%m"); }
std::string get_month_name() { return get_chrono("%B"); }
std::string get_month_name_abbreviated() { return get_chrono("%b"); }
std::string get_year() { return get_chrono("%Y"); }
std::string get_year_short() { return get_chrono("%y"); }
}
get_chrono
is not a template function, so you'd be double-defining it. \$\endgroup\$get_minute()
andget_second()
of actual use to any code? I've never seen a need for getting single components from a timepoint, without needing one or more other components from the same timepoint (rather than from some future timepont). \$\endgroup\$