gpt4 book ai didi

c++ - boost time_duration 总秒数计算

转载 作者:太空宇宙 更新时间:2023-11-04 13:56:56 25 4
gpt4 key购买 nike

我想计算 boost::posix_time:ptime 中两个时间戳之间的时间差(以秒为单位)。但是,由于 timestamp 最多包含微秒,结果可能会出乎我的意料。

boost::posix_time::time_period oPeriod(start, end);
int32_t iDuration = oPeriod.length().total_seconds();

printf("AAAA [%s], [%s], [%d]\n", boost::posix_time::to_iso_string(start).c_str(), boost::posix_time::to_iso_string(end).c_str(), iDuration);

输出:

AAAA [20131030T232150.619049], [20131030T232152.558225], [1]

我想要的是截断秒的小数部分,然后计算差值。所以上面的结果应该是 2 而不是 1。

有什么办法吗?

最佳答案

据我所知,没有直接的方法,你必须在执行之前从 startend 中删除 fractional_seconds()减法,即

start -= start.fractional_seconds();
end -= end.fractional_seconds();
auto diff = end - start;

(注意:我假设 startendtime_duration,如果它们是 ptime,然后你必须先从他们那里得到一天中的时间......)

关于c++ - boost time_duration 总秒数计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21014856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com