How to validate whether my data is x seconds old using chrono package?

我只是一个虾纸丫 提交于 2019-12-04 07:25:26

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/31642087/how-to-validate-whether-my-data-is-x-seconds-old-using-chrono-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!