Merge branch 'localtime_errors' into 'master'

Handle localtime errors

See merge request OpenMW/openmw!2434
This commit is contained in:
psi29a 2022-09-26 07:59:41 +00:00
commit b58c658b56

View file

@ -1,8 +1,15 @@
#ifndef OPENMW_COMPONENTS_MISC_TIMECONVERT_H #ifndef OPENMW_COMPONENTS_MISC_TIMECONVERT_H
#define OPENMW_COMPONENTS_MISC_TIMECONVERT_H #define OPENMW_COMPONENTS_MISC_TIMECONVERT_H
#include <cerrno>
#include <chrono> #include <chrono>
#include <cstring>
#include <ctime>
#include <filesystem>
#include <iomanip>
#include <sstream> #include <sstream>
#include <string>
#include <system_error>
namespace Misc namespace Misc
{ {
@ -22,9 +29,11 @@ namespace Misc
{ {
tm time_info{}; tm time_info{};
#ifdef _WIN32 #ifdef _WIN32
(void)localtime_s(&time_info, &tp); if (const errno_t error = localtime_s(&time_info, &tp); error != 0)
throw std::system_error(error, std::generic_category());
#else #else
(void)localtime_r(&tp, &time_info); if (localtime_r(&tp, &time_info) == nullptr)
throw std::system_error(errno, std::generic_category());
#endif #endif
std::stringstream out; std::stringstream out;
out << std::put_time(&time_info, fmt); out << std::put_time(&time_info, fmt);