如何转换std :: chrono :: time_point到uint64_t? [英] How to convert std::chrono::time_point to uint64_t?

查看:1646
本文介绍了如何转换std :: chrono :: time_point到uint64_t?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过查看数据的时间戳来查看我的数据是否为120秒(或2分钟),所以我有以下代码,因为我使用 chrono package in C ++:

  uint64_t now = duration_cast< milliseconds>(steady_clock :: now()。time_since_epoch 。计数(); 
//检查2分钟的旧数据
bool is_old =(120 * 1000<(now - data_holder-> getTimestamp()));

uint64_t value = now;
while(now< data_holder-> getTimestamp()+ 80 * 1000
&&&& now< value + 80 * 1000){
std :: this_thread :: sleep_for std :: chrono :: milliseconds(100));
now = duration_cast< milliseconds>(steady_clock :: now()。time_since_epoch())count();
}

在上述代码 data_holder-> getTimestamp )是uint64_t,返回以毫秒为单位的时间戳。



现在当我打印出现在变量值时,我看到 10011360 并且当我打印出 data_holder-> getTimestamp() 1437520382241

  2015-07-21 16:13:02,530 WARN 0x7f35312d1700 data_check  - 现在值:10011360,数据存储器时间戳记:1437520382241 

从上面的数据存储器时间戳,它看起来不是120秒旧的数据,所以我觉得有些问题我的代码?因为如果我将数据持有者时间戳转换为实际时间(使用epoch转换器),然后将其与日志时间进行比较,如上所示几乎相同。



使用 system_clock 而不是 steady_clock ,并提出了下面的代码,我开始使用 auto

解决方案A: / p>

  auto now = system_clock :: now(); 
auto dh_ts = system_clock :: time_point {milliseconds {data_holder-> getTimestamp()}};
bool is_old =(minutes {2}<(now - dh_ts));

早前,我使用现在 uint64_t 而不是 auto 。现在在上面的代码后,我有这样的在我的原始代码,因为现在不是 uint64_t 编译代码时出现编译错误。

  uint64_t value = now; 
while(now< data_holder-> getTimestamp()+ 80 * 1000
&&&& now< value + 80 * 1000){
std :: this_thread :: sleep_for std :: chrono :: milliseconds(100));
now = duration_cast< milliseconds>(steady_clock :: now()。time_since_epoch())count();
}

什么是正确的方法来解决这个问题?我不能更改 data_holder-> getTimestamp()数据类型,它必须是 uint64_t ,因为其他代码也使用



这是错误:

 错误:无法转换std :: chrono :: time_point< std :: chrono :: _V2 :: system_clock,std :: chrono :: duration< long int,std :: ratio< 1l,1000000000l> > >â到初始化中的âuint64_t{aka long unsigned int}â€

UPDATE: strong>



如果一切看起来不错,我可以这样使用,而不是使用解决方案A

解决方案B:

  uint64_t now = duration_cast< milliseconds>(system_clock :: now()。time_since_epoch())。count(); 
bool is_old =(120 * 1000<(now - data_holder-> getTimestamp()));


解决方案

实际上,我建议更多地朝解决方案A并将你的 uint64_t 时间的剩余部分转换成time_points: chrono 单位系统是非常有用的。我将首先定义一个帮助函数,将对象上的 uint64_t 时间戳转换为time_points:

  using u64_millis = duration< uint64_t,milli> 
static time_point< system_clock,u64_millis> u64_to_time(uint64_t timestamp){
return time_point< system_clock,u64_millis> {u64_millis {timestamp}};
}

如果您的纪元不同于 system_clock ,这将是修复它的地方。它也可能使用 milliseconds 而不是 u64_millis ,但毫秒的表示类型



现在,你发布的代码变成了一个东西例如:

  auto now = system_clock :: now 
bool is_old = now - u64_to_time(data_holder-> getTimestamp())>分钟{2};

auto value = now;
while(now - u64_to_time(data_holder-> getTimestamp())< seconds {80}
&&& now - value< seconds {80}){
this_thread :: sleep_for(milliseconds {100});
now = system_clock :: now();
}


I am trying to see whether my data is 120 second (or 2 minutes) old or not by looking at the timestamp of the data so I have below code as I am using chrono package in C++:

uint64_t now = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
// check for 2 minutes old data
bool is_old = (120 * 1000 < (now - data_holder->getTimestamp()));   

uint64_t value = now;
while (now < data_holder->getTimestamp() + 80 * 1000 
        && now < value + 80 * 1000) {
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    now = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
}

In the above code data_holder->getTimestamp() is uint64_t which returns timestamp in milliseconds.

Now when I print out now variable value, I see this 10011360 and when I print out data_holder->getTimestamp() value which is 1437520382241

2015-07-21 16:13:02,530 WARN 0x7f35312d1700 data_check - now value: 10011360 , data holder timestamp: 1437520382241

And from the above data holder timestamp, it doesn't look to be 120 second old data right so I feel something is wrong in my code? Since if I convert that data holder timestamp to actual time (using epoch converter) and then compare it with logs time as shown above it is almost same.

So I decided to use system_clock instead of steady_clock and came up with below code in which I started to use auto instead of uint64_t.

Solution A:

auto now = system_clock::now();
auto dh_ts = system_clock::time_point{milliseconds{data_holder->getTimestamp()}};
bool is_old = (minutes{2} < (now - dh_ts));

Earlier, I was using now variable value as uint64_t instead of auto. Now after the above code, I have something like this in my original code since now is not uint64_t so I am getting compilation error while compiling the code.

uint64_t value = now;
while (now < data_holder->getTimestamp() + 80 * 1000 
        && now < value + 80 * 1000) {
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    now = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
}

What is the right way to fix this? I cannot change data_holder->getTimestamp() data type, it has to be uint64_t since other code is also using it.

Here is the error:

error: cannot convert std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1l, 1000000000l> > >â to âuint64_t {aka long unsigned int}â in initialization

UPDATE:

Can I use like this instead of using Solution A if everything looks good below?

Solution B:

uint64_t now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
bool is_old = (120 * 1000 < (now - data_holder->getTimestamp()));

解决方案

Actually, I would suggest going more in the direction of solution A and converting the rest of your uint64_t times into time_points: the chrono unit system is pretty useful. I would start by defining a helper function to convert from the uint64_t timestamps on your objects to time_points:

using u64_millis = duration<uint64_t, milli>;
static time_point<system_clock, u64_millis> u64_to_time(uint64_t timestamp) {
    return time_point<system_clock, u64_millis>{u64_millis{timestamp}};
}

if your epoch is different than the one for system_clock, this would be the place to fix it. It would probably also work to use milliseconds instead of u64_millis, but the representation type for milliseconds isn't explicitly defined and doing it the above way ensures that the types match up correctly.

Now, the code that you posted becomes something like:

auto now = system_clock::now();
bool is_old = now - u64_to_time(data_holder->getTimestamp()) > minutes{2};

auto value = now;
while (now - u64_to_time(data_holder->getTimestamp()) < seconds{80}
        && now - value < seconds{80}) {
    this_thread::sleep_for(milliseconds{100});
    now = system_clock::now();
}

这篇关于如何转换std :: chrono :: time_point到uint64_t?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆