Use constexpr instead of #define where possible in time.cpp

This commit is contained in:
Dan Paulat 2025-02-18 12:28:30 -06:00
parent b60318c393
commit 163b703964

View file

@ -60,15 +60,19 @@ std::string TimeString(std::chrono::system_clock::time_point time,
using namespace std::chrono;
#if (__cpp_lib_chrono >= 201907L)
# define FORMAT_STRING_24_HOUR "{:%Y-%m-%d %H:%M:%S %Z}"
# define FORMAT_STRING_12_HOUR "{:%Y-%m-%d %I:%M:%S %p %Z}"
namespace date = std::chrono;
namespace df = std;
static constexpr std::string_view kFormatString24Hour =
"{:%Y-%m-%d %H:%M:%S %Z}";
static constexpr std::string_view kFormatString12Hour =
"{:%Y-%m-%d %I:%M:%S %p %Z}";
#else
# define FORMAT_STRING_24_HOUR "%Y-%m-%d %H:%M:%S %Z"
# define FORMAT_STRING_12_HOUR "%Y-%m-%d %I:%M:%S %p %Z"
using namespace date;
namespace df = date;
# define kFormatString24Hour "%Y-%m-%d %H:%M:%S %Z"
# define kFormatString12Hour "%Y-%m-%d %I:%M:%S %p %Z"
#endif
auto timeInSeconds = time_point_cast<seconds>(time);
@ -84,11 +88,11 @@ std::string TimeString(std::chrono::system_clock::time_point time,
if (clockFormat == ClockFormat::_24Hour)
{
os << df::format(FORMAT_STRING_24_HOUR, zt);
os << df::format(kFormatString24Hour, zt);
}
else
{
os << df::format(FORMAT_STRING_12_HOUR, zt);
os << df::format(kFormatString12Hour, zt);
}
}
catch (const std::exception& ex)
@ -110,11 +114,11 @@ std::string TimeString(std::chrono::system_clock::time_point time,
{
if (clockFormat == ClockFormat::_24Hour)
{
os << df::format(FORMAT_STRING_24_HOUR, timeInSeconds);
os << df::format(kFormatString24Hour, timeInSeconds);
}
else
{
os << df::format(FORMAT_STRING_12_HOUR, timeInSeconds);
os << df::format(kFormatString12Hour, timeInSeconds);
}
}
}