Skip to content

Commit

Permalink
Use thread-safe version of localtime (#2503)
Browse files Browse the repository at this point in the history
`std::localtime` isn't guaranteed to be thead-safe, since it relies on a
globally defined buffer. We use variants based on local variables
instead.
  • Loading branch information
flomnes authored Nov 26, 2024
1 parent 2f98359 commit 7507ed8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/libs/antares/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,14 @@ void BeautifyName(std::string& out, const std::string& oldname)
std::tm getCurrentTime()
{
using namespace std::chrono;
auto time = system_clock::to_time_t(system_clock::now());
return *std::localtime(&time);
auto now = system_clock::to_time_t(system_clock::now());
std::tm local_time;
#ifdef _WIN32
localtime_s(&local_time, &now); // Windows
#else
localtime_r(&now, &local_time); // POSIX
#endif
return local_time;
}

std::string formatTime(const std::tm& localTime, const std::string& format)
Expand Down

0 comments on commit 7507ed8

Please sign in to comment.