From 5a8d368927fab570a68f768ff027285f7c6403c1 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 6 Dec 2019 20:33:15 +0900 Subject: [PATCH] feat: add thread-safe detail::gmtime_s --- toml/datetime.hpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/toml/datetime.hpp b/toml/datetime.hpp index 371f93b..ba241b1 100644 --- a/toml/datetime.hpp +++ b/toml/datetime.hpp @@ -28,6 +28,13 @@ inline std::tm localtime_s(const std::time_t* src) if (!result) { throw std::runtime_error("localtime_r failed."); } return dst; } +inline std::tm gmtime_s(const std::time_t* src) +{ + std::tm dst; + const auto result = ::gmtime_r(src, &dst); + if (!result) { throw std::runtime_error("gmtime_r failed."); } + return dst; +} #elif _MSC_VER inline std::tm localtime_s(const std::time_t* src) { @@ -36,13 +43,26 @@ inline std::tm localtime_s(const std::time_t* src) if (result) { throw std::runtime_error("localtime_s failed."); } return dst; } -#else +inline std::tm gmtime_s(const std::time_t* src) +{ + std::tm dst; + const auto result = ::gmtime_s(&dst, src); + if (result) { throw std::runtime_error("gmtime_s failed."); } + return dst; +} +#else // fallback. not threadsafe inline std::tm localtime_s(const std::time_t* src) { const auto result = std::localtime(src); if (!result) { throw std::runtime_error("localtime failed."); } return *result; } +inline std::tm gmtime_s(const std::time_t* src) +{ + const auto result = std::gmtime(src); + if (!result) { throw std::runtime_error("gmtime failed."); } + return *result; +} #endif } // detail