Article Categories

Let's calculate a calendar

There are many algorithms already existing for calculating a calendar. Sometimes those are too much complex mathematically or in terms of asymptotic complexity notation. Here I have suggested a numeric process to calculate a calendar by looking at the year.

There is a 'cal' function in all Linux based operating systems. The main problem with the 'cal' command is that it can only give calendar up to the year 9999. Further years are not considered. The following algorithm can be applied to calculate the calendars beyond 9999.

The main problem during the calculation was six and a half million Britons went to bed on September 2, 1752, and woke up on September 14. So, there was a lose of 11 days in 1752. In that year the British Parliament rejected ancient Julian calendar and people started using the Georgian calendar. Still now, we use the Georgian calendar.

So, for this calculation I have rejected the complexity of that lag of 1752. I have started calculation from the year 1800. So, the minimum input year is 1801. The algorithm is as follows -

Step 1

Find the type of century of the given year.

century_type = { ( year/100 ) – 1 } % 4, if year % 100 = 0
                    = ( year/100 ) % 4, otherwise

Step 2

base_year[ ] = { 2001, 2101, 1801, 1901 }
index[ ] = { 13, 1, 9, 25 }
first_day[ ] = { M, SA, TH, T }

Here,
     S - 0 [Sunday]
     M - 1 [Monday]
     T - 2 [Tuesday]
     W - 3 [Wednesday]
     TH - 4 [Thursday]
     F - 5 [Friday]
     SA - 6 [Saturday]

offset = ( year – base_year[ century_type ] ) / 400.
first_index = [ index[ century_type ] + 8 * offset ] % 28

Step 3

Now make the table using first_day[ century_type ] at the first_index of the table. The
process of making the table is written bellow as a function “makeTable”.

makeTable( first_day, first_index ){
     index=first_index;
     day=first_day;
     counter=0;

     for i= 1 to 28 {
          table[index]=day;
          if(counter==3)
               day=(day+2)%7;
          else
               day=(day+1)%7;

          index=(index+1)(;
          counter=(counter+1)%4;
     }
     return(table);
}

Step 4

Now we have the table.

first_day_of_given_year = table [ year % 28 ]

Here, first_day_of_given_year means the 1st January of the given year.

Step 5

Now we can easily generate the calendar of the given year by only calculating whether the
year is a leap year or not.

Example

Let us consider, year = 2015
century_type= ( 2015/100 ) % 4 = 0

base_year[ 0 ] = 2001
index[ 0 ] = 13
first_day[ 0 ] = M

offset = ( 2015 – 2001 ) / 400 = 0

first_index = [13 + 8 * 0 ] % 28 = 13 ( marked bold )
table = { F, S, M, T, W, F, SA, S, M, W, TH, F, SA, M, T, W, TH, SA, S, M, T, TH, F, SA, S, T, W, TH }
2015 % 28 = 27
Hence, first_day_of_year = table [ 27 ] = TH

Now, we can calculate the whole calendar as 2015 is not a leap year.

Limitation

It can calculate the calendar only when year > 1800. Actually I did not consider the transformation from Julian calendar to Georgian calendar on September 1752.

 

About the Author

Arijit Ghoshal

Arijit Ghoshal

Arijit Ghoshal, working at Polaris Networks Inc. as Test Engineer.

(Show Bio)
 

Reader Comments