推荐用 std::chrono::steady_clock 获取单调微秒时间戳,但其非 unix 时间戳;需 unix 微秒时间戳时,c++ 跨平台用 system_clock::now() 配 duration_cast,windows 用 getsystemtimeasfiletime,posix 用 clock_gettime(clock_realtime)。

用 std::chrono::steady_clock 获取微秒级时间戳(推荐)
在 C++11 及以上,std::chrono::steady_clock 是最可靠的选择——它不随系统时钟调整跳变,适合计算间隔或生成单调递增的时间戳。但注意:它返回的是“自时钟起点起的纳秒/微秒数”,不是 Unix 时间戳(即不对应 1970-01-01),如需转为 Unix 微秒时间戳,得搭配 system_clock 或平台 API。
实操建议:
- 直接用
steady_clock::now().time_since_epoch().count()得到纳秒值,除以 1000 即为微秒整数;若要避免整数截断,可先转为duration_cast<microseconds></microseconds> - 若需 Unix 微秒时间戳(即从 1970-01-01 开始的微秒数),必须用
system_clock,但要注意其可能受 NTP 调整影响,且 Windows 上system_clock::period在旧 MSVC 中可能是 100ns(非 1ns),导致精度误判 - 示例代码(Unix 微秒时间戳,跨平台安全):
#include <chrono>
#include <cstdint><p>int64_t now_micros() {
auto now = std::chrono::system_clock::now();
auto us = std::chrono::duration_cast<:chrono::microseconds>(
now.time_since_epoch());
return us.count();
}
</:chrono::microseconds></p></cstdint></chrono>
Windows 下用 GetSystemTimeAsFileTime 获取微秒级时间戳(高精度 + Unix 兼容)
Windows 原生 API GetSystemTimeAsFileTime 返回 FILETIME(100 纳秒为单位),起点是 1601-01-01,转换为 Unix 微秒只需减去 11644473600000000LL(即 1970-01-01 对应的 FILETIME 值),再乘以 10 得到微秒。这个方法在 Windows 上比 system_clock 更稳定,尤其在旧版本 MSVC 或启用了高精度计时器时。
常见错误现象:
- 直接把
FILETIME的dwLowDateTime/dwHighDateTime当作 32 位整数拼接,没考虑字节序或符号扩展,导致负值或溢出 - 忘记乘以 10(100ns → 1μs),结果小了 10 倍
- 硬编码差值但写错位数(少一个 0 或多一个 0),偏差达数小时
正确做法(仅 Windows):
#ifdef _WIN32
#include <windows.h>
int64_t now_micros_win() {
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
int64_t t = (static_cast<int64_t>(ft.dwHighDateTime) <h3>Linux/macOS 下用 <code>clock_gettime(CLOCK_REALTIME)</code>(纳秒级,可转微秒)</h3>
<p>POSIX 标准接口 <code>clock_gettime</code> 在 Linux 和 macOS 上支持 <code>CLOCK_REALTIME</code>,返回 <code>struct timespec</code>(秒 + 纳秒),精度通常为纳秒(实际取决于硬件和内核配置)。它比 <code>gettimeofday</code> 更现代,也避免了后者在某些系统上已被废弃的风险。</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/gongju/2823" title="C++14"><img
src="https://img.php.cn/upload/manual/001/431/639/6ac8b33c327c4749.png" alt="C++14" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/gongju/2823" title="C++14" class="overflowclass">C++14</a>
<p class="overflowclass">C++14 对 C++11 的修正与增强版本,适合旧系统维护和较老工具链兼容。</p>
</div>
<a rel="nofollow" href="/xiazai/gongju/2823" title="C++14" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
<p>使用场景与坑点:</p>
<ul>
<li>必须链接 <code>-lrt</code>(Linux),macOS 不需要;否则链接时报 <code>undefined reference to clock_gettime</code>
</li>
<li>
<code>tv_nsec</code> 是 0–999999999,直接乘 1000 再加秒部分 × 10⁶ 得微秒,但要注意 64 位溢出:用 <code>int64_t(tv_sec) * 1000000LL + tv_nsec / 1000</code>(除法更安全)</li>
<li>某些容器环境或虚拟机中,<code>CLOCK_REALTIME</code> 可能被冻结或跳跃,如需单调性,请改用 <code>CLOCK_MONOTONIC</code>(但它不是 Unix 时间)</li>
</ul>
<p>示例:</p>
<pre class="brush:php;toolbar:false;">#include <time.h>
int64_t now_micros_posix() {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return static_cast<int64_t>(ts.tv_sec) * 1000000LL +
static_cast<int64_t>(ts.tv_nsec) / 1000;
}
</int64_t></int64_t></time.h>
为什么不用 gettimeofday?
gettimeofday 在很多系统上仍可用,但它已被 POSIX 标记为 legacy,且精度上限是微秒(struct timeval 的 tv_usec 字段),无法体现更高精度硬件能力;更重要的是,它在某些新内核或严格编译选项下可能被禁用(如 glibc 2.35+ 默认不导出该符号),导致链接失败。
实际踩坑情况:
- Clang + LTO 编译时,
gettimeofday被优化掉或符号未解析,报undefined reference - 交叉编译到 musl libc(如 Alpine)时,
gettimeofday行为与 glibc 不一致,返回值可能被截断 - 即使成功调用,也无法区分“真微秒精度”和“只是填充了 0 的微秒字段”——你看到的微秒值可能是假的
结论:除非维护极老代码,否则绕过 gettimeofday,优先用 clock_gettime(POSIX)或 system_clock(C++),它们语义清晰、精度可控、移植性更好。
真正麻烦的从来不是“怎么拿到微秒数”,而是“这个数到底代表什么”——是单调的?是否受系统调时影响?跨进程是否可比?这些隐含假设一旦出错,调试成本远高于多写几行转换代码。
C++免费学习笔记(深入):立即使用
在学习笔记中,你将探索 C++ 的入门与实战技巧!










