From e53f408e1030c381311bdf28ebb9e02e5c589345 Mon Sep 17 00:00:00 2001 From: Herman Semenoff Date: Wed, 28 Jan 2026 01:52:23 +0300 Subject: [PATCH] common: fix 32-bit overflow for avoiding year 2038 problem std::chrono::system_clock_::to_time_t() does not guarantee 64-bit time. More info: https://stackoverflow.com/questions/79248038/c-chrono-store-and-retrieve-date-and-time-in-64-bit-format-for-2038-rollover References: - https://en.wikipedia.org/wiki/Year_2038_problem - https://www.gnu.org/software/gnulib/manual/html_node/Avoiding-the-year-2038-problem.html --- common/common.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index 5a8cf52485..f202fa305b 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -426,10 +426,18 @@ std::string string_strip(const std::string & str) { std::string string_get_sortable_timestamp() { using clock = std::chrono::system_clock; - const clock::time_point current_time = clock::now(); - const time_t as_time_t = clock::to_time_t(current_time); + const auto current_time = clock::now(); + const auto as_time_t = std::chrono::duration_cast(current_time.time_since_epoch()).count(); + + struct tm timeinfo; +#ifdef _WIN32 + gmtime_s(&timeinfo, &as_time_t); +#else + gmtime_r(&as_time_t, &timeinfo); +#endif + char timestamp_no_ns[100]; - std::strftime(timestamp_no_ns, 100, "%Y_%m_%d-%H_%M_%S", std::localtime(&as_time_t)); + std::strftime(timestamp_no_ns, 100, "%Y_%m_%d-%H_%M_%S", &timeinfo); const int64_t ns = std::chrono::duration_cast( current_time.time_since_epoch() % 1000000000).count();