Following up from here
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
?
It looks like data_holder->getTimestamp()
value is coming from this below code in our code base and then we are comparing it for 120 second old data check.
// is this the problem?
struct timeval val;
gettimeofday(&val, NULL);
uint64_t time_ms = uint64_t(val.tv_sec) * 1000 + val.tv_usec / 1000;
Now after carefully reading about various clock implementation in C++, it looks like we should use same clock to do the comparison.
Does my above code in which I am calculating data_holder->getTimestamp()
value is the problem? since I am not using steady_clock
there so epoch time will be different and that's why I see this issue?
Now my question is - what code should I use then to fix this issue? Should I use steady_clock
as well for data_holder->getTimestamp()
code? If yes, then what's the right way?
Also same code works fine in Ubuntu 12 box but it doesn't work fine in Ubuntu 14. I am running all statically linked libraries. For Ubuntu 12, code is compiled on Ubuntu 12 running 4.7.3 compiler and for Ubuntu 14, code is compiled on Ubuntu 14 running 4.8.2 compiler.