diff --git a/common/Util.cpp b/common/Util.cpp index 3dd1976f8..7ff50dc4f 100644 --- a/common/Util.cpp +++ b/common/Util.cpp @@ -925,13 +925,26 @@ namespace Util return timestamp; } + /// Returns the given time_point as string in the local time. + /// Format: Thu Jan 27 03:45:27.123 2022 std::string getSteadyClockAsString(const std::chrono::steady_clock::time_point &time) { auto now = std::chrono::steady_clock::now(); - const std::time_t t = std::chrono::system_clock::to_time_t( - std::chrono::time_point_cast( - std::chrono::system_clock::now() + (time - now))); - return std::ctime(&t); + const auto corrected = std::chrono::system_clock::now() + (time - now); + const auto ms = std::chrono::time_point_cast(corrected); + const std::time_t t = std::chrono::system_clock::to_time_t(ms); + const int msFraction = + std::chrono::duration_cast(corrected.time_since_epoch()) + .count() % + 1000; + + std::tm tm; + localtime_r(&t, &tm); + + char buffer[128] = { 0 }; + const size_t offset = std::strftime(buffer, 80, "%a %b %d %H:%M", &tm); + sprintf(&buffer[offset], ".%3d %d", msFraction, tm.tm_year + 1900); + return std::string(buffer); } bool isFuzzing() diff --git a/common/Util.hpp b/common/Util.hpp index ee68e6665..83a21645c 100644 --- a/common/Util.hpp +++ b/common/Util.hpp @@ -1219,6 +1219,7 @@ int main(int argc, char**argv) std::chrono::system_clock::time_point iso8601ToTimestamp(const std::string& iso8601Time, const std::string& logName); /// conversion from steady_clock for debugging / tracing + /// Format (local time): Thu Jan 27 03:45:27.123 2022 std::string getSteadyClockAsString(const std::chrono::steady_clock::time_point &time); /**