You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用libgpiod 2.1.x如何将gpiod::timestamp转换为std::chrono::nanoseconds?

解决libgpiod C++ 2.1.x中gpiod::timestamp转std::chrono::nanoseconds的问题

在libgpiod 2.1.x的C++ API里,gpiod::timestamp类型提供了两个成员方法拆分时间戳:

  • seconds():返回时间戳的秒数部分(类型为std::int64_t)
  • nanoseconds():返回秒数之外的纳秒偏移量(类型为std::uint32_t)

要将其转换为std::chrono::nanoseconds,只需把秒部分转成纳秒后与偏移量相加:

auto ts = event.timestamp_ns();

// 转换为总纳秒数
std::chrono::nanoseconds total_ns = 
    std::chrono::seconds(ts.seconds()) + 
    std::chrono::nanoseconds(ts.nanoseconds());

关联实时时钟(可选)

由于event.timestamp_ns()返回的是基于CLOCK_REALTIME的时间戳,若需要转换为系统实时时钟的时间点,可进一步处理:

// 转换为std::chrono::system_clock的时间点
std::chrono::system_clock::time_point tp = 
    std::chrono::system_clock::from_time_t(static_cast<std::time_t>(ts.seconds())) + 
    std::chrono::nanoseconds(ts.nanoseconds());

得到的total_ns或tp可直接用于依赖C++ chrono库的业务逻辑中。

内容的提问来源于stack exchange,提问作者Mendes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 18:40:10