From 33c17908501d5b1d528432b6f06954dc5002a41b Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Fri, 12 Apr 2024 08:10:03 -0400 Subject: [PATCH] Add a os_utils helper for locatime->put_time --- src/cli/tls_http_server.cpp | 14 +------------- src/lib/utils/os_utils.cpp | 22 ++++++++++++++++++++++ src/lib/utils/os_utils.h | 8 ++++++++ src/tests/runner/test_xml_reporter.cpp | 18 ++---------------- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/cli/tls_http_server.cpp b/src/cli/tls_http_server.cpp index e5c57f22deb..cfbc17111e6 100644 --- a/src/cli/tls_http_server.cpp +++ b/src/cli/tls_http_server.cpp @@ -63,19 +63,7 @@ using tcp_stream = typename beast::tcp_stream::rebind_executor< class Logger final { private: - auto timestamp() const { - const auto t = std::time(nullptr); - struct 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, &t); - #else - localtime_r(&t, &tm); - #endif - - return std::put_time(&tm, "%c"); - } + std::string timestamp() const { return Botan::OS::format_time(std::time(nullptr), "%c"); } public: Logger(std::ostream& out, std::ostream& err) : m_out(out), m_err(err) {} diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp index 6b37e32f1fb..45bea5daa16 100644 --- a/src/lib/utils/os_utils.cpp +++ b/src/lib/utils/os_utils.cpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include #if defined(BOTAN_TARGET_OS_HAS_EXPLICIT_BZERO) #include @@ -325,6 +327,26 @@ uint64_t OS::get_system_timestamp_ns() { return std::chrono::duration_cast(now).count(); } +std::string OS::format_time(time_t time, const std::string& format) { + std::tm tm; + +#if defined(BOTAN_TARGET_OS_HAS_WIN32) + localtime_s(&tm, &time); +#elif defined(BOTAN_TARGET_OS_HAS_POSIX1) + localtime_r(&time, &tm); +#else + if(auto tmp = std::localtime(&time)) { + tm = *tmp; + } else { + throw Encoding_Error("Could not convert time_t to localtime"); + } +#endif + + std::ostringstream oss; + oss << std::put_time(&tm, format.c_str()); + return oss.str(); +} + size_t OS::system_page_size() { const size_t default_page_size = 4096; diff --git a/src/lib/utils/os_utils.h b/src/lib/utils/os_utils.h index cda39f10a06..465e7974fee 100644 --- a/src/lib/utils/os_utils.h +++ b/src/lib/utils/os_utils.h @@ -84,6 +84,14 @@ uint64_t BOTAN_TEST_API get_high_resolution_clock(); */ uint64_t BOTAN_TEST_API get_system_timestamp_ns(); +/** +* Format a time +* +* Converts the time_t to a local time representation, +* then invokes std::put_time with the specified format. +*/ +std::string BOTAN_TEST_API format_time(time_t time, const std::string& format); + /** * @return maximum amount of memory (in bytes) Botan could/should * hyptothetically allocate for the memory poool. Reads environment diff --git a/src/tests/runner/test_xml_reporter.cpp b/src/tests/runner/test_xml_reporter.cpp index 9810b40e767..8a69e4b579f 100644 --- a/src/tests/runner/test_xml_reporter.cpp +++ b/src/tests/runner/test_xml_reporter.cpp @@ -12,11 +12,11 @@ #include #include #include + #include #include #include #include - #include namespace Botan_Tests { @@ -60,24 +60,10 @@ std::string full_compiler_name_string() { #endif } -std::tm* localtime(const time_t* timer, std::tm* buffer) { - #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(buffer, timer); - #else - localtime_r(timer, buffer); - #endif - return buffer; -} - /// formats a given time point in ISO 8601 format (with time zone) std::string format(const std::chrono::system_clock::time_point& tp) { auto seconds_since_epoch = std::chrono::system_clock::to_time_t(tp); - - std::ostringstream out; - std::tm buffer{}; - out << std::put_time(localtime(&seconds_since_epoch, &buffer), "%FT%T%z"); - return out.str(); + return Botan::OS::format_time(seconds_since_epoch, "%FT%T%z"); } std::string format(const std::chrono::nanoseconds& dur) {