# Check out the New Clarion Magazine!

Check out our new Clarion content at www.ClarionMag.com!

This site will remain online as an article archive.

## The Clarion Advisor - Calculating Times

### By David Harms

Posted March 1 1999

Printer-friendly version

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.

C7 version of the demo app

David Harms is an independent software developer and the editor and publisher of Clarion Magazine. He is also co-author with Ross Santos of Developing Clarion for Windows Applications, published by SAMS (1995), and has written or co-written several Java books. David is a member of the American Society of Journalists and Authors (ASJA).

by Jeff Easlick on September 11 2010 (comment link)

I'm still having difficulties getting two time fields to subtract properly. For example, if I enter 7:17pm as the first time and 7:21pm as the second time, I want to display the difference as 4 minutes (I'm using TIME @T3 in the dictionary).

However, more often than not, the time difference is displayed as 3 minutes (usually a minute short, regardless of the time span).

What configuration would give me an accurate time difference in minutes?

By the way, this application won't compile using 7.2.7583.

The error message reads: "Either the Sources list must contain only one File and that File must be a Project File or TargetName must be specified."

by David Harms on September 14 2010 (comment link)

Jeff,

I wasn't able to find the reason for the compile error in C7, so I recreated a new C7 version of the app. See the link above.

You need to deal with that offset from one, as shown in the demo app.

Dave

You must be logged on to post comments.