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

Fractional days

Having to convert to the smallest unit of time and then back to hours, minutes and seconds is tedious and error prone.

As a result most spreadsheets and programming languages usually provide a date/time facility, which works in a slightly different way.

In this case the time is recorded in days and fractional days. That is 1.5 days is the same as 36 hours - 24 hours (i.e. one day) plus 12 hours (i.e. half a day). As long as the fraction is recorded to a sufficient precision this works equally well as working in seconds.

The only real problem is that one hour is 0.04166 recurring but you can generally avoid having to type in such numbers by using 1/24 for on hour, 1/(24*60) for a minute and 1/(24*60*60) for a second.

The big advantage of working in days and fractions of a day is that you can calculate time differences without having to do any conversions – you simply use subtraction. If you have two dates D1 and D2 in day, fractional day format then the time between then is just D1-D2.

Of course if you want to show the time in hours, minutes and seconds you do need to convert but it’s easy using the same techniques to convert seconds:

  1. days = Int(T)
  2. hours = Int((T-days)*24) for a 24 hour clock
  3. minutes = Int((T-days-hours/24)*60)
  4. seconds= Int((T-days-hours/24-
                         minutes/60)*60)

Fortunately you generally don’t have to do this because most programming languages and spreadsheets support a date/time data type which uses fractional days and provides automatic support for conversion.

For example, in Excel the Time(hours,minutes,seconds) function converts a time to fractional day, e.g. Time(12,0,0) is .5.

In most cases displaying a fractional day value as a time is just a matter of setting the correct format on the cell – and this usually happens automatically anyway.

Other languages have similar functions and facilities.

 

Banner

 

 

Hot Dates

So far so good. Time is fairly easy as long as you are working with intervals of a few days. Things really only start to get complicated when you start to work with dates specified in day, month and year.

The reason is that at this level there is very little regularity in the way time is measured and it is the definition of the month that makes things complicated. The month is not a fixed period of time. If I say that I will finish a project in a month - what exactly do I mean?

There are 7 days to a week and that’s about as simple and regular as it gets. A year is 365 days, which is the time it takes the earth to go around the sun – to be more precise it actually takes 365 days, 5 hr, 48 min, and 45.5 sec on average to go round.

Because the orbital period of the earth isn’t an exact number of rotations about its axis we have to tinker with the number of days in a year to correct the calendar every so often – hence leap years which have 366 days. As the error reaches 0.97 of a day after four years we add an extra day in February and call it a leap year. It is interesting to note that the error is almost a whole day before it is corrected which means for example that the summer solstice, the date and time when the sun's track through the sky reaches the highest point  in the northern hemisphere varies by around .75 of a day. This means that as we approach a leap year the day that this occurs on can be the 20th to the 22nd of June. What is even more surprising in that the leap year doesn't correct the problem completely and slowly the solstice drifts until it is correct by skipping a leap year every 100 years and extra leap year every 400 years. 

This results in the rule that every year divisible by 4 is a leap year except that century years, e.g. 2100, are not leap years - unless they are divisible by 400. Giving the algorithm:

if (year is not divisible by 4) then not leap year)
else if (year is not divisible by 100) then  leap year
else if (year is not divisible by 400) then not leap  year)
else  leap year

So even the unit of one year isn't fixed but in general use if you say that something will take a year then you mean 365 days. 

Lunar and solar

Leap years are a minor irritation when compared to the problem of the month. The month was originally the time it took the moon to go from full moon to full moon i.e. the time for the moon to go around the earth once.

The lunar or synodic month is a fairly inconvenient 29.53059 days on average. Usually we quote a lunar month as 29.5 days but the half a day still makes it a totally unworkable unit of time. If you take 12 lunar months then you get a year with only 354 days and this is 11 days shorter than a solar year.

There are many different ways of making the lunar year and the solar year agree to a greater or lesser extent and this is the origin of the different calendars that are in use.

For example the Islamic calendar has 12 lunar months but uses leap years to correct it. The Jewish calendar uses alternating 29 and 30-day lunar months. To correct it an extra month is added every 3 years. Interestingly the old Egyptian calendar is very similar to our own in being based on a solar year but it used 12 lunar months of 30 days but with 5 extra days tacked on at the end of the year. They even managed to correct over a longer period by adding a day every four years, i.e. they invented the leap year.

The calendar almost universally in use today is a slightly modified version of the Julian calendar, invented over a fairly lengthy period of time, by the Romans and, of course named after Julius Caesar. This more or less gives up on the lunar month but still divides the year into 12 calendar months with lengths of 28 to 31 days.

This is where things get really irritating for a computer because this isn’t just arithmetic to a different base – the base changes with the month!

So if you are counting days you have to test to see which month you are in to know when to move to the next month. There is no way that you can simply convert 6 months say to an equivalent number of days because it all depends which months you have selected.

This is all very complicated and we didn't get it right at the first try.

The Julian calendar was initially wrong by roughly 11 minutes per year and this error accumulated until in 1582 it became noticeable as a 10-day discrepancy. To correct this problem 10 days were dropped from the year with much complaining about stolen time.

To stop the problem happening again the remedy of skipping leap years at the turn of the century (unless divisible by 400) was added. This is the Gregorian calendar and it is what we use today. The Soviet Union only adopted the calendar in 1918 and Greece only in 1923. It is important to know these things because if you plan to interpret dates from different countries and from before the universal adoption of the Gregorian calendar then you need to be aware that there could be a 10 day discrepancy! There is also the small matter that many countries continued to use older or religious calendars long after the reform.

Working with very old dates is doubly difficult. 



Last Updated ( Monday, 13 April 2020 )