mirror of
https://github.com/Detanup01/gbe_fork.git
synced 2024-12-28 03:14:15 +08:00
proper helper function to get utc time
This commit is contained in:
parent
122921bb48
commit
35f4b9c6d7
@ -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{};
|
||||
|
@ -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{};
|
||||
|
@ -5,6 +5,11 @@
|
||||
#include <cctype>
|
||||
#include <random>
|
||||
|
||||
// for gmtime_s()
|
||||
#define __STDC_WANT_LIB_EXT1__ 1
|
||||
#include <time.h>
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -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();
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user