跳转到主内容
趣航编程网 - 趣学编程,启航技术之路!

c++中如何获取系统当前时间_c++获取当前日期时间

现代C++推荐用std::chrono::system_clock::now()获取高精度时间点,精度达纳秒或微秒;需格式化时先转time_t再用std::put_time,毫秒级需手动拼接。 用
std::chrono
获取高精度当前时间点 现代 C++ 推荐用
std::chrono::system_clock::now()
获取当前时间点,它返回的是一个
time_point
,精度通常为纳秒或微秒,且不依赖 C 风格的
time_t
转换开销。 直接获取时间点:无需格式化,适合计时、差值计算等场景 跨平台稳定:在 Windows、Linux、 mac OS 上行为一致 注意:
system_clock
可能不保证是单调时钟(如系统时间被手动调整会影响结果)
auto now = std::chrono::system_clock::now(); auto duration = now.time_since_epoch(); auto millis = std::chrono::duration_cast(duration).count();
std::put_time
格式化输出 年月日时分秒 若需可读字符串(如
"2024-06-12 15:30:45"
),必须先将
time_point
转为
std::time_t
,再用
std::put_time
格式化。这是目前最简洁、类型安全的 C++11+ 方式。 必须包含
std::gmtime
返回 UTC 时间;
std::localtime
返回本地时区(但非线程安全) 推荐用
std::localtime_s
(MSVC)或
std::localtime_r
(POSIX)替代
std::localtime
避免数据竞争
auto now = std::chrono::system_clock::now(); auto time_t = std::chrono::system_clock::to_time_t(now); std::stringstream ss; ss << std::put_time(std::localtime(&time_t), "%Y-%m-%d %H:%M:%S"); std::string datetime_str = ss.str(); // 如 "2024-06-12 15:30:45"
避免
ctime()
asctime()
的陷阱
ctime(&t)
返回带换行符的 C 字符串(如
"Wed Jun 12 15:30:45 2024\n"
),
asctime()
同样不灵活且线程不安全。它们无法控制格式、不能去掉换行、也不支持毫秒级精度。 返回的指针指向静态缓冲区,多次调用会相互覆盖 格式固定,无法满足如
"20240612_153045"
或带毫秒的需求 在多线程中直接使用极易引发未定义行为 需要毫秒/微秒字符串?自己拼接更可靠
std::put_time
不支持毫秒(
%f
是 C23 扩展,主流编译器尚未普遍支持)。要输出带毫秒的时间字符串(如
"2024-06-12 15:30:45.123"
),得手动拆解
time_point
: 立即学习 “ C++免费学习笔记(深入) ”; 先取整秒部分,转成
time_t
并格式化日期时间 再用
duration_cast
提取毫秒数,补零到 3 位 最后字符串拼接(避免
sprintf
类型不安全操作)
auto now = std::chrono::system_clock::now(); auto ms = std::chrono::duration_cast(now.time_since_epoch()) % 1000; auto time_t = std::chrono::system_clock::to_time_t(now); std::stringstream ss; ss << std::put_time(std::localtime(&time_t), "%Y-%m-%d %H:%M:%S"); ss << '.' << std::setfill('0') << std::setw(3) << ms.count(); std::string datetime_ms = ss.str(); // "2024-06-12 15:30:45.123"
C++ 标准库 的时间处理不是“调一个函数就完事”,关键在于区分「时间点」和「格式化字符串」两个阶段——前者用
chrono
,后者绕不开
time_t
+ 线程安全的
localtime_r
/
localtime_s
。毫秒拼接看似啰嗦,却是目前最可控的方式。

相关文章