如何使用chrono包验证我的数据是否是x秒? [英] How to validate whether my data is x seconds old using chrono package?

查看:180
本文介绍了如何使用chrono包验证我的数据是否是x秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过查看数据的时间戳来查看我的数据是否是120秒老,所以我下面的小代码在我的库项目,它使用 std :: chrono package:

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

//一些日志以打印以上值
LOG4CXX_WARN(logger,data logging,now:<< now<<,data holder timestamp:< < data_holder-> getTimestamp()<<,is_old:<< is_old<<,difference:< <(now- data_holder-> getTimestamp()

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



现在当我打印出现在看到这个 433425679 ,当我打印出 data_holder-> getTimestamp() 1437943796841 ,现在和数据存储器时间戳之间的差异来自 18446742636199180454 ,如下所示:

  2015-07-26 13:49:56,850 WARN 0x7fd050bc9700 simple_process  - 数据记录,现在:433425679,数据存储器时间戳记:1437943796841,is_old:1,差异:18446742636199180454 

现在,如果我转换数据存储器时间戳 1437943796841 epoch converter,我看到:

 您的时区:7/26/2015,1:49:56 PM 


这与日志中显示的时间戳完全相同 2015-07-26 13: 49:56,850 WARN ,这意味着我的数据看起来不是120秒的旧数据。如果是的话,为什么我看到 is_old 的值为1?



现在如果我运行这个简单的主程序,我看到 is_old 正在返回0而不是1:

  #include< ; iostream> 

int main()
{

bool is_old =(120 * 1000<(433425679 - 1437943796841));
std :: cout<<is_old:< is_old<< std :: endl;
}

发生了什么事?为什么当我从main方法运行时返回0,而当我从我的项目中记录时,返回0。这是否意味着我运行我的主要代码的方式是不同的方式我的库是使用cmake命令构建的方式?我正在建立我的项目可执行文件使用cmake命令生成一个tar文件,然后我运行它通过解开tar文件。我在Ubuntu 14.04上运行。



以前,我想我需要使用 system_clock c $ c> steady_clock 但是在我从main方法运行它之后,它看起来像是别的什么。

解决方案

无符号整数算术定义为算术模2 ^ numBits。



这意味着当你执行 now - data_holder-> getTimestamp ()与您的未签名变量和 getTimestamp()大于现在例如,操作将包装,并且不会得到一个负值,但是一个(通常很大)与输入相同的无符号整数类型。



如果你



现在无论是从任何源中减去一些时间戳还是从<$>返回的值, c $ c> steady_clock :: now 首先是有意义的是一个不同的问题。它很可能不是。您应该将当前时间与来自同一来源的某些创建时间(例如,从 std :: steady_clock )进行比较。


I am trying to see whether my data is 120 second old or not by looking at the timestamp of the data so I have below small code in my library project which is using std::chrono package:

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

// some logging to print out above values
LOG4CXX_WARN(logger, "data logging, now: " << now << ", data holder timestamp: " << data_holder->getTimestamp() << ", is_old: " << is_old << ", difference: " << (now -         data_holder->getTimestamp()));

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

Now when I print out now variable value, I am seeing this 433425679 and when I print out data_holder->getTimestamp() value which is 1437943796841 and the difference of now and data holder timestamp is coming as 18446742636199180454 as shown below in the logs:

2015-07-26 13:49:56,850 WARN 0x7fd050bc9700 simple_process - data logging, now: 433425679 , data holder timestamp: 1437943796841 , is_old: 1 , difference: 18446742636199180454

Now if I convert data holder timestamp 1437943796841 using epoch converter, I see this:

Your time zone: 7/26/2015, 1:49:56 PM

which is exactly same as the timestamp shown in the logs 2015-07-26 13:49:56,850 WARN so that means my data doesn't look to be 120 second old data. If yes, then why I am seeing is_old value as 1?

Now if I run this simple main program, I see is_old is returning 0 instead of 1:

#include <iostream>

int main ()
{

bool is_old = (120 * 1000 < (433425679 - 1437943796841));
std::cout<<"is_old: " << is_old << std::endl;
}

What is going on? Why it is returning 0 when I run from main method as compared to 1 when I log it from my project. Does that mean the way I am running my main code is different as compared to way my library was build using cmake command? I am building my project executable using cmake command which is generating a tar file and then I am running it by untarring the tar file. I am running on Ubuntu 14.04 box.

Earlier, I was thinking I need to use system_clock instead of steady_clock but after I run it from main method, it looks like something else is going on.

解决方案

Unsigned integer arithmetic is defined as arithmetic modulo 2^numBits.

That means when you do now - data_holder->getTimestamp() with your unsigned variables and getTimestamp() is greater than now as in your example, the operation will wrap and you will not get a negative value, but a (usually pretty big) one of the same unsigned integer type as the inputs.

If you use literals instead, their types will be signed and thus the result will be negative, as expected.

Now whether subtracting some timestamp from whatever source and the value returned by steady_clock::now makes sense in the first place is a different question. It most likely does not. You should compare the current time with some creation time you got from the same source (e.g. both from std::steady_clock) instead.

这篇关于如何使用chrono包验证我的数据是否是x秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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