|Dates Are Difficult|
|Written by Mike James|
|Thursday, 29 August 2019|
Page 1 of 3
Date and times follow their own regularities, and they have nothing at all to do with binary, or even simple decimal, counting. First, clock and watch makers had to find ways of working with hours, minutes, seconds; and then programmers had to find ways that were much simpler. Join us on a quick tour of the time and date system and how it can be mastered using the mod function.
What Programmers Know
* Recently revised
Computers and programmers like regularity - it’s the exceptions that cause problems.
What could be more regular than our calendar and time-keeping methods? After all, they are designed to provide a regular measurement of the passage of time. The complication is that we use a human-oriented system of measurement designed to tie in with the seasons and astronomical phenomena.
Perhaps in these days of bright streetlights and a general stay-inside lifestyle perhaps it would be easier to divide the year up into 10 months each of 10, 10-hour days! Perhaps the minute should have 100 new seconds and there should be 100 minutes in a new hour!
Don't laugh just yet - there have been many serious proposals to decimalise time. It was even tried out during the French revolution, but never caught on.
Then again perhaps even this isn't radical enough and we should throw it all away and start again with something like the decimal-point-based “star date” of Star Trek and other science fiction. After all, it wouldn't be the first time that we had changed to other units of measurements to make it easier for computers to deal with them.
However, the problem with date and time goes much deeper than mere units. The problem lies in the very nature of the mechanics of the regularities that the system of measurement seeks to capture. So it looks as if computers have to learn to work with the existing system and, no matter how attractive decimal time sounds, it just isn’t going to happen.
Making a mod of it
The whole date and time thing starts off with two simple regularities of life.
The first is that the earth rotates about its own axis once a day – or more accurately we call the time it takes for the earth to rotate a day. The second is that the earth rotates around the Sun to define a period we call a year.
You could say that the day is all about time and the relationship between the day and the year is all about date. So starting off with time…
For reasons that have nothing to do with anything other than custom and practice we divide the day into 24 hours and an hour into 60 minutes and a minute into 60 seconds. After seconds we go completely decimal and work in centiseconds, milliseconds and so on.
The only advantage of these strange numbers is that they are highly factorable, i.e. you can divide 24 by 2, 4, 8 and 12 and get answers that don’t have fractional parts. As already mentioned In principle we could decimalise time measurement at this level but I doubt anything will ever come of this idea - mostly for the reason that because 10 isn't very factorable and if midnight is 0 midday would be 5 and breakfast would be around 2.5.
In practice working with time is fairly easy as long as you always convert to the smallest unit. Indeed this is the basic principle of time and data arithmetic in all computer systems.
For example, if you want the difference between 3 hours 2 minutes 1 second and 1 hour 5 minutes 10 seconds simply convert the whole lot to seconds and take the difference. What ever you do don't attempt to emulate the way humans compute with times by subtracting the seconds and minutes as separate operations with carries in different bases. It can be done but it is error prone to implement and not particularly efficient.
Converting to seconds is just a matter of multiplication by 60*60 for hours and 60 for the minutes:
Of course you now have the problem of converting back to hours, minutes and seconds but this is where the Mod function comes in.
The basic idea is that the Mod function gives you the remainder when you divide by something. For example Mod(7,3) or 7 % 3 is 1 because when you divide 7 by 3 it goes twice with remainder 1.
The only other tool you need is the Int function (variously called trunc, floor etc. in other languages), which simply chops off any fractional part a number might have.
To convert T in seconds into hours, minutes and seconds you use the following:
The Persistence of Memory by Salvador Dali
|Last Updated ( Monday, 13 April 2020 )|