Let's assume we have a simple function that takes a std::chrono::time_point and returns a string using a given formatting string. Like so:
std::string DateTimeToString(std::chrono::sys_time, const char * szFormat /*= "%Y/%m/%d %H:%M:%S"*/)
According to the cppreference documentation of std::formatter we should be able to use the individual tags to create a string like 2021/03/24 15:36:32.123123. However, when the duration of the time_point uses microseconds (which is mandatory for our use) then this throws a format_error.
If we split this into to separate calls so that we use format for the date and time separately then it works. Like this:
auto tSysTime = std::chrono::system_clock::now();
std::string sTime = std::format("{:%H:%M:%S}", tSysTime.time_since_epoch());
std::string sDate = std::format("{:%Y/%m/%d}", std::chrono::sys_days(std::chrono::duration_cast<std::chrono::days>(tSysTime.time_since_epoch())));
Am I missunderstanding something or is there no way to have a single format string for my purpose? Keep in mind that the format string is an optional parameter so I cannot hardcode a split of individual parameters.
My expection would be to write
std::format("{:%Y/%m/%d %H:%M:%S}",tSysTime.time_since_epoch());
However, this crashes...
Thank you in advance for any help!
Edit: I'm using /std::c++latest flag in Visual Studio 2019