




std::system_clock::now()返回的是自纪元起的time_point(如纳秒计数),非可读日历时间;必须经to_time_t()转为time_t,再用localtime_r()/localtime_s()解析为tm,最后strftime格式化输出。
直接用 std::system_clock 获取日历时间,核心就两步:获取当前时间点(time_point),再转换为 std::time_t 用于格式化。它不是“系统时钟类”,而是标准库定义的、与系统实时时钟对齐的时钟类型。
std::system_clock::now() 当作年月日?std::system_clock::now() 返回的是一个 time_point,本质是自纪元起的某个时长(比如纳秒或微秒),不是可读的年月日。必须通过 std::system_clock::to_time_t() 转成 std::time_t,才能交给 std::localtime() 或 std::gmtime() 解析。
常见错误现象:
std::system_clock::now() —— 输出一堆无意义的数字(内部计数值)to_time_t() 就传给 localtime() —— 编译失败,类型不匹配std::chrono::system_clock::to_time_t() 时传入了错误类型的 time_point(比如用了 steady_clock 的结果)—— 编译报错或未定义行为这是最常用场景:输出当前本地时间字符串。关键在于顺序不能乱,且注意线程安全(std::localtime 返回静态缓冲区指针)。
std::chrono::system_clock::now() 获取当前时间点std::chrono::system_clock::to_time_t() 转为 std::time_t
std::localtime()(或 std::localtime_r() 在 POSIX 系统上)转为 std::tm
std::strftime() 格式化输出,避免直接操作 tm 成员(易出错)auto now = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(now);
std::tm tm_buf;
#ifdef _WIN32
localtime_s(&tm_buf, &t);
#else
localtime_r(&t, &tm_buf); // POSIX
#endif
char buf[64];
std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm_buf);
std::cout << buf << '\n';
如果需要跨时区一致的时间表示(如日志时间戳、网络协议时间),应使用 std::gmtime() 替代 std::localtime()。两者输入都是 std::time_t,区别只在是否应用本地时区偏移。
std::gmtime() 是 UTC,std::localtime() 是本地时区(受 TZ 环境变量影响)gmtime_s(),POSIX 下是 gmtime_r()
std::time_t 本身是 UTC 秒数,只是解析方式不同 —— 这点容易被误解auto now = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(now);
std::tm tm_buf;
#ifdef _WIN32
gmtime_s(&tm_buf, &t);
#else
gmtime_r(&t, &tm_buf);
#endif
char buf[64];
std::strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", &tm_buf);
std::cout << buf << '\n'; // ISO 8601 UTC 格式
std::system_clock 的精度和纪元起点由实现定义,但 C++11 起要求其 time_point 必须能转为 std::time_t,且该转换需保持单调性和大致同步于系统实时时钟。
GetSystemTimeAsFileTime(),精度约 100ns,纪元为 1601-01-01clock_gettime(CLOCK_REALTIME),精度可达纳秒级,纪元为 1970-01-01system_clock::duration 是微秒或纳秒 —— 用 auto 或
decltype 推导system_clock::period::num/den 做手动换算真正容易被忽略的是:即使你只想要秒级时间,也必须走 to_time_t → gmtime_r 这条路径,而不是试图从 time_point 中直接除以某个固定值 —— 因为不同平台的底层计时单位和纪元都不同。