I have a bit of code that takes a timestamp with
const int64 start = Time::currentTimeMillis();
at some time, and then measures the time elapsed a few lines below with
const int64 delta = Time::currentTimeMillis() - start;
The code between the two calls is not relevant, suffice to say that it could take from a few seconds down to nearly nothing.
In consideration of that, I just assumed that delta would have been ranging from a few thousands down to 0 at minimum.
jassert (delta >= 0);
imagine my surprise when, seemingly at random times and only while letting the code run for many minutes, the above assertion was being hit, for apparently no reason.
After some thinking, I found the cause: the clock on my computer is set to be regularly updated with some internet time server. Also, it tends to run faster than actual/server time, so each of those regular updates actually sets it some seconds backwards. When said update happens between the two calls to Time::currentTimeMillis() above, I end up with a negative value of delta because the second call returned a value that's few seconds "before" the time represented by start.
Now the actual question: since I think this is a quite common pattern, and equally common is the assumption that the value returned by Time::currentTimeMillis() is expected to only grow in value between calls, what do you think about tweaking Time::currentTimeMillis() so that it only returns non-decreasing numbers, i.e. it will never yield a value that's lower than a value that was returned by a previous call?
(with a static value inside the function to hold the latest returned value, the implementation of this could be far easier than the description of the potential and counterintuitive issue in the documentation of the function itself)