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

后端 未结 1 1571
无人及你
无人及你 2021-01-29 10:25

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

相关标签:
1条回答
  • 2021-01-29 11:03

    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.

    0 讨论(0)
提交回复
热议问题