Using "Clock Math"

I’m trying to figure out how to implement “Clock Math” in Tads3. In other words, a clock has 12 possible values [1, 2, 3…12], so when it’s at 12 and 1 more hour is added, the result is 1, not 13. Likewise, we hit December 1st before November 32nd rolls around.

So, I know how to increment a value of something, but how do I cause the “roll-over” effect in clock math? How do I ensure that a value over Z goes back to A and continues counting from there?

Any thoughts? :confused:

Hi !

I don’t know anything about TADS, but what you are asking for is the mathematic module operation.

hours = ( hours + 1 ) % 12

The ‘%’ sign is the module. It is possible that TADS accepts it as is. The module is the remaining of dividing hours by 12, so you will always get values between 0 and 11.

The module operation can be simulated, if not available, by doing something like this (assuming that ‘/’ is the integral division):

hours = abs( hours - ( ( hours / 12 ) * 12 ) )

At a basic level, I’d have a ‘Time’ object to keep track of the time - with variables for the hour, minute, date, month etc. Then add functions to increase or decrease the time by a certain amount, or set it to a certain time and date. baltasarq’s module calc might come in handy here (TADS does support %, according to this).

But if you just want to keep track of time without changing it much, it’s probably just simple enough to write a method like this, to advance time:

incrementHour()
{
    hour++;

    if(hour>12)
        hour=1;
    else if(hour==12)
    {
        if(!isPM)
            isPM=true;
        else
        {
            isPM=nil;
            incrementDay();
        }
    }
}

(And this method could be called by incrementMinute when minute is greater than 60, etc.)

In terms of dates, I’d keep two lists - one of month names and one of the number of days in each month. The Time object would record the month as a number, but could then look up the name and length in the lists when it needed to print the date or check whether the day needed to be reset to 1.

Woo! Baltasarq, the second method works perfectly! Thanks so much! :smiley: