I have this function, it will compile just fine if I am using g++
. the problem is I have to use windows compiler, and it does not have ctime_r
. I am a little bit new in C/C++. can anyone help me make this work with MSVC cl.exe
?
The function:
void leaveWorld(const WorldDescription& desc)
{
std::ostringstream os;
const time_t current_date(time(0));
char current_date_string[27];
const size_t n = strlen(ctime_r(¤t_date,current_date_string));
if (n) {
current_date_string[n-1] = '\0'; // remove the ending \n
} else {
current_date_string[0] = '\0'; // just in case...
}
os << totaltime;
(*_o) << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" << endl;
(*_o) << "<testsuite name=\"" << desc.worldName() << "\" ";
(*_o) << "date=\"" << current_date_string;
(*_o) << "\" tests=\"" << ntests
<< "\" errors=\"" << nerror
<< "\" failures=\"" << nfail
<< "\" time=\"" << os.str().c_str() << "\" >";
_o->endl(*_o);
(*_o) << _os->str().c_str();
_os->clear();
(*_o) << "</testsuite>" << endl;
_o->flush();
}
In MS library, there is a ctime_s
, which allows for the same "not using a global" feature that ctime_r
has in Linux/Unix OS's. You will probably have to wrap it like this:
const char *my_ctime_r(char *buffer, size_t bufsize, time_t cur_time)
{
#if WINDOWS
errno_t e = ctime_s(buffer, bufsize, cur_time);
assert(e == 0 && "Huh? ctime_s returned an error");
return buffer;
#else
const char *res = ctime_r(buffer, cur_time);
assert(res != NULL && "ctime_r failed...");
return res;
#endif
}