修正日志时间

This commit is contained in:
cxh 2025-11-13 13:55:55 +08:00
parent df5da2ec5b
commit 5cf5be60d4

View File

@ -1,9 +1,10 @@
// logger.cpp // logger.cpp
#include "logger.hpp" #include "logger.hpp"
#include <iomanip>
#include <cstdio>
#include <ctime> #include <ctime>
#include <fstream> #include <fstream>
#include <cstdio> #include <iomanip>
std::ofstream Logger::log_file; std::ofstream Logger::log_file;
std::string Logger::current_log_filename; std::string Logger::current_log_filename;
@ -22,12 +23,13 @@ std::string Logger::get_time_string()
{ {
auto now = std::chrono::system_clock::now(); auto now = std::chrono::system_clock::now();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000; auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
std::time_t t = std::chrono::system_clock::to_time_t(now + std::chrono::hours(8)); // UTC+8
std::time_t t = std::chrono::system_clock::to_time_t(now);
std::tm tm_now = *std::localtime(&t); std::tm tm_now = *std::localtime(&t);
std::ostringstream ss; std::ostringstream ss;
ss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S") ss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S") << "." << std::setw(3) << std::setfill('0') << millis.count();
<< "." << std::setw(3) << std::setfill('0') << millis.count();
return ss.str(); return ss.str();
} }
@ -36,16 +38,13 @@ std::string Logger::get_current_time_utc8()
using namespace std::chrono; using namespace std::chrono;
auto now = system_clock::now(); auto now = system_clock::now();
now += hours(8); // UTC+8
std::time_t t = system_clock::to_time_t(now);
auto millis = duration_cast<milliseconds>(now.time_since_epoch()) % 1000; auto millis = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;
std::time_t t = system_clock::to_time_t(now);
std::tm tm_now = *std::localtime(&t); std::tm tm_now = *std::localtime(&t);
std::ostringstream ss; std::ostringstream ss;
ss << std::put_time(&tm_now, "%Y%m%d%H%M%S") ss << std::put_time(&tm_now, "%Y%m%d%H%M%S") << std::setw(3) << std::setfill('0') << millis.count();
<< std::setw(3) << std::setfill('0') << millis.count();
return ss.str(); return ss.str();
} }