最简洁方式是用 std::chrono + std::format(C++20):获取当前时间点并格式化为“YYYY-MM-DD HH:MM:SS”字符串,但需注意 std::localtime 的线程不安全性及 std::format 在 GCC/Clang 上的实验性状态。

用 std::chrono + std::format(C++20)最简洁
如果你的编译器支持 C++20 且启用了 std::format(如 MSVC 19.3x+、GCC 13+ 配合 -std=c++20 -D_GLIBCXX_USE_CXX11_ABI=1),这是最直接的方式:
auto now = std::chrono::system_clock::now();
auto time_t = std::chrono::system_clock::to_time_t(now);
std::string s = std::format("{:%Y-%m-%d %H:%M:%S}", *std::localtime(&time_t));
注意:std::localtime 返回的是非线程安全的静态指针,多线程下需用 std::localtime_r(POSIX)或 localtime_s(Windows)替代;std::format 目前在 GCC/Clang 上仍属实验性,部分发行版默认未启用。
用 std::put_time(C++11 起)兼容性最好
这是目前最稳妥的跨平台方案,不依赖 C++20,也避开 strftime 的缓冲区管理麻烦:
-
std::put_time需配合std::ostringstream和std::localtime使用 - 必须传入
const std::tm*,不能直接对time_t操作 - 同样要注意
std::localtime的线程安全性问题
auto now = std::time(nullptr); auto tm_ptr = std::localtime(&now); // ⚠️ 多线程请改用 localtime_r 或 localtime_s std::ostringstream oss; oss << std::put_time(tm_ptr, "%Y-%m-%d %H:%M:%S"); std::string s = oss.str();
避免用 ctime 或 asctime——它们带换行且不可控
常见错误是直接写 std::string s = std::ctime(&now);,结果字符串末尾带 \n,而且格式固定(如 "Wed Apr 10 15:30:45 2024\n"),无法自定义。这不是 bug,是标准规定的行为。
组合式C++代码评审方案,融合静态分析、AI推理、多轮迭代评审和C++专项检查,适用于PR审查、增量代码审查、全项目评审和代码质量评分,触发词包括review cpp、cpp代码评审、C++review、代码审查。
立即学习“C++免费学习笔记(深入)”;
-
ctime(&t)等价于asctime(localtime(&t)) - 返回的 C 字符串以
\n\0结尾,std::string构造时会把\n一起包进去 - 若硬要清理,得额外调用
s.pop_back(),但不如一开始就选可控方式
需要毫秒级精度?别只靠 std::time_t
std::time(nullptr) 和 std::localtime 只能到秒级。若要包含毫秒(比如日志打点),必须从 std::chrono::system_clock::now() 出发:
auto now = std::chrono::system_clock::now();
auto time_t = std::chrono::system_clock::to_time_t(now);
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
std::ostringstream oss;
oss << std::put_time(std::localtime(&time_t), "%Y-%m-%d %H:%M:%S");
oss << '.' << std::setfill('0') << std::setw(3) << ms.count();
std::string s = oss.str(); // e.g. "2024-04-10 15:30:45.123"
这里 std::setw 和 std::setfill 是关键,否则毫秒可能输出为 7 而不是 007;另外注意 localtime 的线程安全仍是隐患,生产环境建议封装成线程局部缓存或使用 std::gmtime + 手动时区偏移。

















