coharness
2 years agoOccasional Contributor
Validate a time with allowance for seconds to be off
I have a label that reports the time and I am validating it. Usually it's ok, but sometimes the check is done a few seconds late, and so the label appears to be wrong. How could I allow for a few seconds to be wrong without it failing the test? I am using Javascript as my written scripts.
A simple solution would be to convert the time to Epoch, in seconds. If it's within the range of -/+2 seconds, then it's considered ok.
Pseudocode,
var reportTime = 1690908743 ... var currentTime = getTimeStamp() // 1690908745 if (reportTime >= (currentTime - 2)) and (reportTime <= (currentTime + 2)) { // reportTime is within range }