I´ve wrote the following function that receives a time point and return a ISO string with milliseconds:
std::string TimePointToISOString(const std::chrono::time_point<std::chrono::system_clock>& time)
{
std::time_t rawTime = std::chrono::system_clock::to_time_t(time);
struct tm timeinfo;
localtime_s(&timeinfo, &rawTime);
std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(time.time_since_epoch());
std::size_t frac_seconds = ms.count() % 1000;
char buffer[32];
std::strftime(buffer, 32, "%FT%TZ", &timeinfo);
std::string bufferStr(buffer);
std::stringstream ss;
ss << bufferStr.substr(0, 19) << "." << std::setw(3) << std::setfill ('0') << frac_seconds << bufferStr.substr(20);
return ss.str();
}
I´ve used to run that on Linux with the localtime()
function with no problems at all. Now I am migrating to Windows/VS2012 and the compiler suggests to change localtime()
for a safer localtime_s()
, done immediately.
After the conversion the strftime
call does crash at runtime with the following error:
Expression: ("Invalid format directive", 0)
Compilation runs fine. Help appreciated to find out what´s going on here. I guess something simple that I can´t notice...