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?
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):
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:
(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.