Skip to content

Commit

Permalink
using the safer versions of gmtime - fixes #423
Browse files Browse the repository at this point in the history
  • Loading branch information
onqtam committed Dec 22, 2020
1 parent 5ee1ebe commit 9f0d32b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 7 additions & 5 deletions doctest/doctest.h
Original file line number Diff line number Diff line change
Expand Up @@ -5046,24 +5046,26 @@ namespace {

struct JUnitTestCaseData
{
DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations") // gmtime
static std::string getCurrentTimestamp() {
// Beware, this is not reentrant because of backward compatibility issues
// Also, UTC only, again because of backward compatibility (%z is C++11)
time_t rawtime;
std::time(&rawtime);
auto const timeStampSize = sizeof("2017-01-16T17:06:45Z");

std::tm* timeInfo;
timeInfo = std::gmtime(&rawtime);
std::tm timeInfo;
#ifdef DOCTEST_PLATFORM_WINDOWS
gmtime_s(&timeInfo, &rawtime);
#else // DOCTEST_PLATFORM_WINDOWS
gmtime_r(&rawtime, &timeInfo);
#endif // DOCTEST_PLATFORM_WINDOWS

char timeStamp[timeStampSize];
const char* const fmt = "%Y-%m-%dT%H:%M:%SZ";

std::strftime(timeStamp, timeStampSize, fmt, timeInfo);
std::strftime(timeStamp, timeStampSize, fmt, &timeInfo);
return std::string(timeStamp);
}
DOCTEST_CLANG_SUPPRESS_WARNING_POP

struct JUnitTestMessage
{
Expand Down
12 changes: 7 additions & 5 deletions doctest/parts/doctest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2389,24 +2389,26 @@ namespace {

struct JUnitTestCaseData
{
DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations") // gmtime
static std::string getCurrentTimestamp() {
// Beware, this is not reentrant because of backward compatibility issues
// Also, UTC only, again because of backward compatibility (%z is C++11)
time_t rawtime;
std::time(&rawtime);
auto const timeStampSize = sizeof("2017-01-16T17:06:45Z");

std::tm* timeInfo;
timeInfo = std::gmtime(&rawtime);
std::tm timeInfo;
#ifdef DOCTEST_PLATFORM_WINDOWS
gmtime_s(&timeInfo, &rawtime);
#else // DOCTEST_PLATFORM_WINDOWS
gmtime_r(&rawtime, &timeInfo);
#endif // DOCTEST_PLATFORM_WINDOWS

char timeStamp[timeStampSize];
const char* const fmt = "%Y-%m-%dT%H:%M:%SZ";

std::strftime(timeStamp, timeStampSize, fmt, timeInfo);
std::strftime(timeStamp, timeStampSize, fmt, &timeInfo);
return std::string(timeStamp);
}
DOCTEST_CLANG_SUPPRESS_WARNING_POP

struct JUnitTestMessage
{
Expand Down

0 comments on commit 9f0d32b

Please sign in to comment.