last day (14 days later) » 

1:00 AM
1
A: Validate timestamp for a particular duration in C++

WorldSEnderI'd probably do something like this: auto now = system_clock::now().time_since_epoch(); // Use the correct time duration below. Milliseconds could be wrong, see 1) auto diff = now - std::chrono::milliseconds(data_holder->getTimestamp()); bool is_old = diff > std::chrono::seconds{120}; // bool is...

 
Thanks for your suggestion. data_holder->getTimestamp() will return timestamp in milliseconds. Can you help me understand this - My now variable value is 10011360 and data holder timestamp value is 1437520382241 then why difference is coming as 18446742636199180735 of now - data_holder->getTimestamp(). It should come as negative right?
The above result is with my code what I have. Any thoughts?
 
@user1950349 now() - then() should be positive because now() is farer away from epoch that then(). So the difference should be positive. What disturbs me is that when I output duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count(), it is something like 28654237, not what I anticipated
 
hmmm that's what I was confuse as well what is count doing. But when I print out now and data holder timestamp, I get above details and if I subtract it, it should be negative then why it is positive?
 
@user1950349, I fixed it, and explained a bit in the answer why your attempt failed. Feel free to read
 
Thanks for help. What is the difference between steady_clock vs system_clock? I was not able to understand this. And also if I need to go with my solution what I had in my question, I can just replace steady_clock with system_clock right?
 
1:00 AM
@user1950349, the difference is quite technical. steady_clock is guaranteed to never be reset as far as I understood it, thus being monotone, whereas system_clock is designed to represent the computer's calendar date. And no, you can't just replace them, see the last paragraph in the answer.
 
hey
so you are saying I cannot use it like this?
	uint64_t now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
	bool is_old = (120 * 1000 < (now - data_holder->getTimestamp()));
 
This will work... in most cases, but it isn't guaranteed by the standard
You see, system_clock::now().time_since_epoch() won't return the duration from UNIX-epoch until now, but the duration from the clock's epoch until now
and the standard doesn't guarantee that system_clock's epoch is UNIX-epoch
 
I see got that
tell me this
when I was using steady_clock::now().time_since_epoch()
what it was supposed to return?
 
Nothing particular, either. Most likely the ticks your processor worked for
 
Actually sorry for asking again, I am still confuse what is the difference between
steady_clock::now().time_since_epoch() vs system_clock::now().time_since_epoch()
not able to understand fully :(
 
1:10 AM
You understand that every clock has its own epoch?
 
yeah that I know
 
so the first returns the time since steady_clock's epoch, the second the time since system_clock's epoch
 
yes got that part. Now what does steady_clock do as compared to system_clock? Because of that result was coming as different?
 
steady_clock is most likely counting processor cycles: stackoverflow.com/questions/13772567/get-cpu-cycle-count. It can not count down.
system_clock is representing the date of your computer. Most often you can set it to whatever you like simply by changing your local date
 
I see
 
1:16 AM
steady_clock is monotonic and steady
system_clock is realtime and system-wide
 
yes that makes sense now
thanks for your help.
 
I guess your data_holder stores some realtime signature timestamp, so you should use system_clock
also No problem :)
I'm glad to help
 
yesh my data_holder holds realtime signature timestamp
cool, I will use system_clock then
 

last day (14 days later) »