Dates Are Difficult
Written by Mike James   
Thursday, 29 August 2019
Article Index
Dates Are Difficult
Fractional Days
Date Bugs

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

knowcover

Contents

  1. The Computer - What's The Big Idea?*
  2. The Memory Principle - Computer Memory and Pigeonholes*
  3. Principles of Execution - The CPU
  4. The Essence Of Programming
  5. Variables - Scope, Lifetime And More*
  6. Binary Arithmetic
  7. Hexadecimal*
  8. Binary - Negative Numbers*
  9. Floating Point Numbers*
  10. Inside the Computer - Addressing
  11. The Mod Function
  12. Recursion
  13. The Lost Art Of The Storage Mapping Function *
  14. Hashing - The Greatest Idea In Programming
  15. Advanced Hashing
  16. XOR - The Magic Swap*
  17. Programmer's Introduction to XML
  18. From Data To Objects*
  19. What Exactly Is A First Class Function - And Why You Should Care*
  20. Stacks And Trees*
  21. The LIFO Stack - A Gentle Guide*
  22. Data Structures - Trees
  23. Inside Random Numbers
  24. The Monte Carlo Method
  25. Cache Memory And The Caching Principle
  26. Data Compression The Dictionary Way
  27. Dates Are Difficult*
  28. Sequential Storage*
  29. Magic of Merging*
  30. Power of Operators
  31. The Heart Of A Compiler*
  32. The Fundamentals of Pointers
  33. Functional And Dysfunctional Programming*

* 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.

 

sundial

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…

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:

answer = 3*60*60+2*60+1-(1*60*60+5*60+10)

Of course you now have the problem of converting back to hours, minutes and seconds but this is where the Mod function comes in.

You’ll find the Mod function in most programming languages, spreadsheets etc with minor variations in spelling and the way it is used and some languages provide a mod operator, JavaScript uses % for example.

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:

  1. seconds = Mod(T,60)
  2. minutes = Mod(Int(T/60),60)
  3. hours = Int(T/(60*60))

 

dali

The Persistence of Memory by Salvador Dali

 

<ASIN:0486409139>

<ASIN:0380793245>

<ASIN:0192862057>

<ASIN:0879304960>



Last Updated ( Monday, 13 April 2020 )