Core/State: Return an empty string on invalid input to SystemTimeAsDoubleToString().

This commit is contained in:
Admiral H. Curtiss 2023-10-30 19:16:59 +01:00
parent 03f8ec09eb
commit 99a3bbc055
No known key found for this signature in database
GPG Key ID: F051B4C4044F33FB
1 changed files with 7 additions and 4 deletions

View File

@ -265,16 +265,19 @@ static double GetSystemTimeAsDouble()
static std::string SystemTimeAsDoubleToString(double time) static std::string SystemTimeAsDoubleToString(double time)
{ {
// revert adjustments from GetSystemTimeAsDouble() to get a normal Unix timestamp again // revert adjustments from GetSystemTimeAsDouble() to get a normal Unix timestamp again
time_t seconds = (time_t)time + DOUBLE_TIME_OFFSET; time_t seconds = static_cast<time_t>(time) + DOUBLE_TIME_OFFSET;
tm* localTime = localtime(&seconds); errno = 0;
tm* local_time = localtime(&seconds);
if (errno != 0 || !local_time)
return "";
#ifdef _WIN32 #ifdef _WIN32
wchar_t tmp[32] = {}; wchar_t tmp[32] = {};
wcsftime(tmp, std::size(tmp), L"%x %X", localTime); wcsftime(tmp, std::size(tmp), L"%x %X", local_time);
return WStringToUTF8(tmp); return WStringToUTF8(tmp);
#else #else
char tmp[32] = {}; char tmp[32] = {};
strftime(tmp, sizeof(tmp), "%x %X", localTime); strftime(tmp, sizeof(tmp), "%x %X", local_time);
return tmp; return tmp;
#endif #endif
} }