From 2503232d8aadb7031a06f3bae8e19bb1f6e1df6d Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Fri, 12 Apr 2024 09:07:19 -0400 Subject: [PATCH] wip --- src/cli/tls_http_server.cpp | 2 +- src/lib/utils/os_utils.cpp | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/cli/tls_http_server.cpp b/src/cli/tls_http_server.cpp index 85d2519d920..1a9fb0cfac6 100644 --- a/src/cli/tls_http_server.cpp +++ b/src/cli/tls_http_server.cpp @@ -63,7 +63,7 @@ using tcp_stream = typename beast::tcp_stream::rebind_executor< class Logger final { private: - auto timestamp() const { + std::string timestamp() const { return Botan::OS::format_time(std::time(nullptr), "%c"); } diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp index b6512343c7f..2d9088e20c4 100644 --- a/src/lib/utils/os_utils.cpp +++ b/src/lib/utils/os_utils.cpp @@ -325,17 +325,24 @@ uint64_t OS::get_system_timestamp_ns() { return std::chrono::duration_cast(now).count(); } -std::string format_time(time_t time, const std::string& format) { +std::string OS::format_time(time_t time, const std::string& format) { std::tm tm; -#if defined(BOTAN_BUILD_COMPILER_IS_MSVC) || defined(BOTAN_TARGET_OS_IS_MINGW) || \ - defined(BOTAN_TARGET_OS_IS_CYGWIN) || defined(BOTAN_TARGET_OS_IS_WINDOWS) - localtime_s(&tm, time); +#if defined(BOTAN_TARGET_OS_HAS_WIN32) + localtime_s(&tm, &time); +#elif defined(BOTAN_TARGET_OS_HAS_POSIX1) + localtime_r(&time, &tm); #else - localtime_r(time, &tm); + if(auto tmp = std::localtime(&time)) { + tm = *tmp; + } else { + throw Encoding_Error("Could not convert time_t to localtime"); + } #endif - return std::put_time(tm, format.c_str()); + std::ostringstream oss; + oss << std::put_time(&tm, format.c_str()); + return oss.str(); } size_t OS::system_page_size() {