SoCoder -> Article Home -> Coding Basics
Created : 10 August 2007
System : Windows
Language : Blitz
MilliSecs - Where does it end?Explains the limitations of MilliSecs() and how to use it effectively.
Some believe that MilliSecs () returns
the number of milliseconds elapsed since midnight.
Unfortunately, this is not the case.
The MilliSecs () function returns the number of milliseconds
elapsed since Windows was first started.
So right after windows starts, MilliSecs () equals zero.
Over the edge
Since the maximum value of an integer in Blitz is 2,147,483,647
MilliSecs () will be counting up to that value.
How long will that take?
Although most people don't have their computer on that long.
Besides, it's pretty hard to keep Windows stable
for such a long time anyway
So what happens after that time?
Take a regular integer variable for example.
And you assign the maximum value to it.
Now add 1 and see what happens.
It wraps to -2,147,483,648!
Same thing happens with the milliseconds.
What is the problem about all this?
Imagine you have the following code:
If the number of milliseconds wrap
in the middle of the Repeat..Until,
the program could hang forever!
If Finish lies right before the wrapping point
and our program didn't have enough time
to check MilliSecs before it wraps,
the resulting value will be even smaller than before,
and remaining smaller than Finish for another 50 days!
So what's the solution you ask?
The solution is simply changing orientation to difference.
Say what you say?
I mean, by comparing difference instead of points in time
you can still accurately measure time
without running into any gaps in time.
If you look at the graph above
and then at the following equation:
Then you might wonder why it works.
Let's try it out in a program.
Which should give you 96.
Here's what happens:
And that's all there is to it