The Clarion Advisor - Calculating Times
Posted March 1 1999
If you've ever needed to perform math on Clarion time values, you know that you can sometimes get unexpected results. Clarion stores times as hundredths of seconds since midnight, which means that one second is equal to 100. Given this you'd expect that five seconds after midnight would be stored as 500, but in fact it's stored as 501, because a time of 1 is equal to midnight. This extra 1 causes all sorts of interesting problems.
Consider what happens if you try to subtract one time from another time. Figure 1 shows a test application (available for download) which lets you enter two times using the time picture @t4, which lets you enter hh:mm:ss.
Figure 1: The Time Calculation Application
If you enter a time of 0:00:05 for the Time A, and 0:00:01 for the Time B, and click on the Unadjusted Subtract A From B button, you get a result of not 4 seconds, as you'd expect, but 3 seconds!
This is not a bug, but a failure on the part of the developer (that's me) to account for the one one-hundredth of a second. The time 0:00:05 is stored as 501, and 0:00:01 is stored as 101. 501-101 = 400, and the display picture in effect is rounding down the time value, since it only displays whole seconds. All times from 301-400 will display as three seconds, from 401-500 as four seconds, and so forth.
If you're adding times you may not notice anything wrong, since 501+101=602, and anything from 601 to 700 is going to display as 6 seconds, but if you're doing repeated ads the error will accumulate and eventually could wrap around, adding a second.
One way to solve this problem is to subtract the extra 1 from the times before doing any math, and add the 1 back to the result of the calculation. This way you can do addition, subtraction, multiplication and division and get accurate results. You should also, however be wary of running over the end of day boundary. There are 8,640,000 hundredths of a second in a day, and a time of 8,640,000 is equivalent to 23:59:59:99 (keeping in mind that there is no Clarion time picture that will display those hundredths of a second for you).
TimeResult = (TimeA - 1) + (TimeB - 1) + 1
This code could also be written as
TimeResult = TimeA +
TimeB - 1 However, if you're dealing with a long list of
numbers, or mixing addition and substraction and possibly
multiplication and division, you'll be far better off subtracting
the 1 from each time right away, then doing the math, then adding
the 1 back.
If you're going to be adding times that may go over day boundaries, you can't simply add the times, because Clarion won't display time values greater than 8640000. You can either subtract 8640000 from such a time, or do it the easy way with with the modulus operator which returns the remainder after division.
CurrTime = CurrTime % 8640000
The above code will be correct if you're adding times, although you may want to keep track of how many days are involved. You can get the number of days with this code:
Days = INT(CurrTime/8640000)
If you're subtracting times you may end up with a value that's less than 1. Here you have to keep in mind that there are only 8640000 hundreths of a second in a day. If you subtract ten seconds from today at 12:00:05 a.m., the result you want is 11:59:55 p.m. yesterday. If the resulting time is less than 1, you need to add 8640000.
TimeResult = (TimeA - 1) - (TimeB - 1) + 1 IF TimeResult < 1 THEN TimeResult += 8640000.
Clarion times aren't overly difficult to calculate. The most important factor to keep in mind is that all whole seconds are offset from 1, not from 0.
Post a comment
You must be logged on to post comments.
From the archives
1/12/2010 12:00:00 AM
Many questions remain unanswered on the .NET front. When will there be a usable AppGen? The new template language is based on Microsoft's T4, but what will the implementation really look like? How will Clarion.NET support Windows Presentation Foundation and Silverlight, the current state of the art in .NET user interface development? How does the Clarion# language compare to C# and VB.NET? Dave Harms looks at these questions, and reveals a surprising fact about Clarion# that changes everything.