如何将tv

编程入门 行业动态 更新时间:2024-10-20 08:01:27
如何将tv_sec和tv_usec的时间组合成一个实体?(How to combine time from tv_sec and tv_usec into one entity? What is safe?)

所以我需要做一些精确计时,并努力使用tv_sec和tv_usec。 现在我,超级凌乱,有一个我自己的家庭卷Tuple类的向量,持有秒和微秒。

这里的问题是......如果我想说...获取我的向量中最老的条目,我必须对元组中的值进行超毛和粗略的比较。

我真的想将tv_sec和tv_usec组合成一个值来存储。 这将使比较变得非常容易。

但是如何安全地做到这一点? 例如,这种尝试是否会在某些价值观中爆炸?

gettimeofday(&tv, NULL); time_t secs = tv.tv_sec; time_t msecs = tv.tv_usec; double the_answer = tv_sec + (1.0/1000000) * tv_usec; //ehhhh? Maybe?

我把问题归结为 - 如果我使用它会溢出/行为怪异/毁了我:

double the_answer = tv_sec + (1.0/1000000) * tv_usec; //ehhhh? Maybe?

So I need to do some precision timing and am endeavoring to use tv_sec and tv_usec. Right now I, super messily, have a vector of my own home-rolled Tuple class, holding both secs and microsecs.

The issue here is... if i want to say... get the oldest of the entries in my vector, I have to do super hairy and gross comparisons between the values in the tuples.

I'd really like to combine tv_sec and tv_usec into a single value to store. This would make comparison very easy.

But how does one do this safely? Is this attempt below going to blow up in my face for certain values, for example?

gettimeofday(&tv, NULL); time_t secs = tv.tv_sec; time_t msecs = tv.tv_usec; double the_answer = tv_sec + (1.0/1000000) * tv_usec; //ehhhh? Maybe?

I spose the question boils down to - will overflow/act weird/ruin me if I use it:

double the_answer = tv_sec + (1.0/1000000) * tv_usec; //ehhhh? Maybe?

最满意答案

如果你被困在最后一个千禧年,只需使用std::chrono或boost::chrono chrono。 该标准给出了每个单元所需的精确位数的要求,并且API 非常好。

所需位数(包括符号位)为:

23小时 29分钟 35秒 45毫秒 55微秒 64纳秒

对于纳秒,这些将持续到2262年。

Just use std::chrono, or boost::chrono if you're stuck in the last millenium. The standard gives requirements for exactly how many bits are required for each unit, and the API is really nice.

The number of required bits (including the sign bit) is:

23 for hours 29 for minutes 35 for seconds 45 for milliseconds 55 for microseconds 64 for nanoseconds

For nanoseconds, these will work until the year 2262.

更多推荐

本文发布于:2023-08-01 18:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   tv

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!