From 35f4b9c6d71dc475859936ad7a30186324d3e174 Mon Sep 17 00:00:00 2001 From: otavepto <153766569+otavepto@users.noreply.github.com> Date: Fri, 7 Jun 2024 19:49:57 +0300 Subject: [PATCH] proper helper function to get utc time --- crash_printer/linux.cpp | 6 +----- crash_printer/win.cpp | 7 +----- helpers/common_helpers.cpp | 26 +++++++++++++++++++++++ helpers/common_helpers/common_helpers.hpp | 2 ++ 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/crash_printer/linux.cpp b/crash_printer/linux.cpp index e9719a13..df3a4d12 100644 --- a/crash_printer/linux.cpp +++ b/crash_printer/linux.cpp @@ -52,11 +52,7 @@ static void exception_handler(int signal, siginfo_t *info, void *context, struct std::ofstream file(logs_filepath, std::ios::app); - auto now = std::chrono::system_clock::now(); - auto t_now = std::chrono::system_clock::to_time_t(now); - auto gm_time = std::gmtime(&t_now); - auto time = std::string(std::asctime(gm_time)); - time.pop_back(); // remove the trailing '\n' added by asctime + std::string time(common_helpers::get_utc_time()); common_helpers::write(file, "[" + time + "]"); { std::stringstream ss{}; diff --git a/crash_printer/win.cpp b/crash_printer/win.cpp index b9de6b85..77a4ae93 100644 --- a/crash_printer/win.cpp +++ b/crash_printer/win.cpp @@ -106,12 +106,7 @@ static void log_exception(LPEXCEPTION_POINTERS ex_pointers) std::ofstream file(std::filesystem::path(logs_filepath), std::ios::app); - auto now = std::chrono::system_clock::now(); - auto t_now = std::chrono::system_clock::to_time_t(now); - auto gm_time = std::gmtime(&t_now); - auto asc_time = std::asctime(gm_time); - auto time = std::string(asc_time ? asc_time : ""); - time.pop_back(); // remove the trailing '\n' added by asctime + std::string time(common_helpers::get_utc_time()); common_helpers::write(file, "[" + time + "]"); { std::stringstream ss{}; diff --git a/helpers/common_helpers.cpp b/helpers/common_helpers.cpp index 98c59e5a..8d5cb5c5 100644 --- a/helpers/common_helpers.cpp +++ b/helpers/common_helpers.cpp @@ -5,6 +5,11 @@ #include #include +// for gmtime_s() +#define __STDC_WANT_LIB_EXT1__ 1 +#include + + namespace common_helpers { KillableWorker::KillableWorker( @@ -407,3 +412,24 @@ size_t common_helpers::rand_number(size_t max) return distrib(gen); } + +std::string common_helpers::get_utc_time() +{ + // https://en.cppreference.com/w/cpp/chrono/c/strftime + std::time_t time = std::time({}); + std::tm utc_time{}; + + bool is_ok{}; +#if defined(__GNUC__) || defined(POSIX) + is_ok = !!gmtime_s(&time, &utc_time); +#else + is_ok = !gmtime_s(&utc_time, &time); +#endif + std::string time_str(4 +1 +2 +1 +2 +3 +2 +1 +2 +1 +2 +1, '\0'); + if (is_ok) { + constexpr const static char fmt[] = "%Y/%m/%d - %H:%M:%S"; + size_t chars = std::strftime(&time_str[0], time_str.size(), fmt, &utc_time); + time_str.resize(chars); + } + return time_str; +} diff --git a/helpers/common_helpers/common_helpers.hpp b/helpers/common_helpers/common_helpers.hpp index 05967a7a..13f1c5e7 100644 --- a/helpers/common_helpers/common_helpers.hpp +++ b/helpers/common_helpers/common_helpers.hpp @@ -101,4 +101,6 @@ bool dir_exist(const std::wstring &dirpath); // between 0 and max, 0 and max are included size_t rand_number(size_t max); +std::string get_utc_time(); + }