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::ch
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.