503 Service Unavailable

2007-02-08

Gregorian and Julian calendars

Filed under: Programming,Software — rg3 @ 20:09

Some months ago my father asked me if it was possible to find out the day of the week for any day in history. I inmediately asked what range of years he was interested in, and he explained to me that he had a good amount of dates, some of which were relatively modern but the oldest ones dated back to the beginning of the XVIII century. I recalled using the cal Unix command to get the calendar for any month and volunteered to tell him as many dates as he wanted. cal is able to tell you the calendar for any month from year 1 to year 9999. This was alright until, one day, he asked me if I was sure that program used the correct Gregorian reformation data. I didn’t know what he was talking about because I was completely ignorant of the topic. I had heard we hadn’t always been using the current calendar, but I didn’t know when were the last changes introduced and what those changes consisted in. After all, I had been reading dates in history books and nobody ever mentioned anything. By searching in Google and reading the Wikipedia article on the Gregorian calendar I found out that the changes had happened not long ago. And what is even worse, that the changes were not applied to all countries at the same time.

I then headed to the cal manpage, only to find the following paragraph:

The Gregorian Reformation is assumed to have occurred in 1752 on the 3rd of September. By this time, most countries had recognized the reformation (although a few did not recognize it until the early 1900’s.) Ten days following that date were eliminated by the reformation, so the calendar for that month is a bit unusual.

Indeed, if we ask for the calendar of that month, we see that not 10, but 11 days were skipped. The first and main shock was finding out that not long ago people had skipped 11 days in one month. They jumped from September 2 to September 14! No, really, they did. I’ll paste the calendar for that month:

   September 1752   
Su Mo Tu We Th Fr Sa
       1  2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

In Spain and some other catholic countries the change had happened on October 1582, which compromised all the dates we had looked up prior to September 3, 1752 (Doh!). Why did they have to skip those days? In the Julian calendar, a leap year happened each 4 years to compensate for the inaccuracy of the 365 days calendar. But later, more precise calculations were done and they found out those were too many leap years. They introduced the modern leap year test (it’s a leap year if it’s divisible by 4, but not if it’s divisible by 100, unless it’s also divisible by 400) but also realized that the previous calendar had already introduced too many leap days, and that was why they were about 10 days behind and they needed to skip that amount of days.

I can imagine an hilarious situation of a group of scientifics going to meet pope Gregory XIII: “His Holiness, we found out we need to skip 10 days.” Gregory XIII raises an eyebrow, “What?”. “That we need to skip 10 days. Like jumping from October 4th to October 15th.” Gregory XIII blinks. They take out some papers full of charts and equations and start to explain everything to the pope. The amazing thing is that they really did it. I can’t imagine the situation if this was going to happen nowadays. People would print T-shirts with the sentence “I survived the Gregorian reformation”. Or the Benedictian reformation or whatever. This is the calendar for October 1582 in the countries that switched that year.

    October 1582
Mo Tu We Th Fr Sa Su
 1  2  3  4 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

As I said before, not every country switched at the same time. Russia, for example, didn’t switch until 1918. That’s not even 100 years ago! I imagine the Gregorian reformation is or was a problematic point for law people, insurance companies and others, and some computer software probably has or had to take it into account to calculate things.

Going back to the problem with my father, I started to search for a program that would use the correct reformation data but at that moment I couldn’t find any. Fortunately, the CVS server for the FreeBSD project has web access to the cal source code and someone was nice enough to describe the algorithm needed to get the calendar for any month[1], and I created a Python program first and a C++ program later that worked like a simplified cal and used the Spanish reformation data. I can mail both on request if you want. However, I don’t think it’s needed because later I found some websites which are able to give you calendars depending on the country you live[2] and I also found out about a tool called ncal which also has a country selector. It’s present in modern BSDs as well as some Linux distributions. Unfortunately for me, Slackware doesn’t have it. Debian users, for example, have it as part of the bsdmainutils package at the time I’m writting this.

The algorithm this programs feature is quite simple once you know a key fact: January 1, 1, was Saturday. From then, you only need to calculate the number of days that have passed until the day you need. This calculation is usually divided in the following steps:

  1. Take the number of previous years and multiply by 365.
  2. Add one more day for each previous leap year. This number can be easily computed as the year number divided by 4 for every year previous to the reformation, and substracting the number of years divisible by 100 and adding the number of years divisible by 400 for the amount of years passed since the reformation.
  3. Then, add the number of days in the previous months of the year you want.
  4. Add one more if the year you want is a leap year and you want a month after February.
  5. Finally, substract the number of days skipped in the Gregorian reformation if it’s a later date.

Knowing the previous number, plus knowing January 1, 1 was Saturday, plus using the modulus operation, you can know the day of the week any month starts at, and use it to print a calendar.

Links

  1. FreeBSD cal README.
  2. Online calendar generator.
Advertisements

Blog at WordPress.com.

%d bloggers like this: