时区
This commit is contained in:
parent
5cf5be60d4
commit
5adfe421c7
@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.10)
|
||||
# 工程名称(随便改)
|
||||
project(rtsp_server)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_compile_definitions(GLIB_DISABLE_DEPRECATION_WARNINGS)
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
// logger.cpp
|
||||
#include "logger.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
|
||||
@ -21,14 +23,17 @@ void Logger::set_log_to_file(const std::string& filename)
|
||||
|
||||
std::string Logger::get_time_string()
|
||||
{
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
|
||||
using namespace std::chrono;
|
||||
|
||||
std::time_t t = std::chrono::system_clock::to_time_t(now);
|
||||
std::tm tm_now = *std::localtime(&t);
|
||||
auto now = system_clock::now();
|
||||
auto zt = zoned_time{current_zone(), now}; // 自动使用系统时区(Asia/Shanghai)
|
||||
|
||||
// 获取毫秒部分
|
||||
auto ms = duration_cast<milliseconds>(zt.get_local_time().time_since_epoch()) % 1000;
|
||||
|
||||
// 格式化
|
||||
std::ostringstream ss;
|
||||
ss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S") << "." << std::setw(3) << std::setfill('0') << millis.count();
|
||||
ss << format("{:%Y-%m-%d %H:%M:%S}", zt) << '.' << std::setw(3) << std::setfill('0') << ms.count();
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
@ -38,13 +43,12 @@ std::string Logger::get_current_time_utc8()
|
||||
using namespace std::chrono;
|
||||
|
||||
auto now = system_clock::now();
|
||||
auto millis = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;
|
||||
zoned_time zt{current_zone(), now};
|
||||
|
||||
std::time_t t = system_clock::to_time_t(now);
|
||||
std::tm tm_now = *std::localtime(&t);
|
||||
auto ms = duration_cast<milliseconds>(zt.get_local_time().time_since_epoch()) % 1000;
|
||||
|
||||
std::ostringstream ss;
|
||||
ss << std::put_time(&tm_now, "%Y%m%d%H%M%S") << std::setw(3) << std::setfill('0') << millis.count();
|
||||
ss << format("{:%Y%m%d%H%M%S}", zt) << std::setw(3) << std::setfill('0') << ms.count();
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user