18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Calendrical Calculations<br />

designing efficient shortcuts for the computation.<br />

<strong>The</strong> basic approach behind calendar systems is to start with a particular reference date and count from<br />

there. <strong>The</strong> particular rules for wrapping the count around into months and years is what distinguishes a<br />

given calendar system from another. To implement a calendar, we need two functions, one that given a<br />

date returns the integer number of days that have elapsed since the reference start date, the other of which<br />

takes an integer n and returns the calendar date exactly n days from the reference date. This is analogous<br />

to the ranking and unranking rules for combinatorial objects, such as permutations (see Section ).<br />

<strong>The</strong> major source of complications in calendar systems is that the solar year is not an integer number of<br />

days long. Thus if a calendar seeks to keep its annual dates in sync with the seasons, leap days must be<br />

added at both regular and irregular intervals. Since a solar year is 365 days and 5:49:12 hours long, an<br />

extra 10:48 minutes would have to be accounted for at the end of each year if we were simply to add a<br />

leap day every four years.<br />

<strong>The</strong> original Julian calendar (from Julius Caesar) did not account for these extra minutes, which had<br />

accumulated to ten days by 1582 when Pope Gregory XIII proposed the Gregorian calendar used today.<br />

Gregory deleted the ten days and eliminated leap days in years that are multiples of 100 but not 400.<br />

Supposedly, riots ensued because the masses feared their lives were being shortened by ten days. Outside<br />

the Catholic church, resistance to change slowed the reforms. <strong>The</strong> deletion of days did not occur in<br />

England and America until September 1752, and not until 1927 in Turkey.<br />

<strong>The</strong> rules for most calendrical systems are sufficiently complicated and pointless that you should lift<br />

code from a reliable place rather than attempt to write your own. We identify suitable implementations<br />

below.<br />

<strong>The</strong>re are a variety of ``impress your friends'' algorithms that enable you to compute in your head on<br />

which day of the week a particular date occurred. Such algorithms often fail to work reliably outside the<br />

given century and should be avoided for computer implementation.<br />

Implementations: Dershowitz and Reingold provide a uniform algorithmic presentation [DR90,<br />

RDC93] for a variety of different calendar systems, including the Gregorian, ISO, Julian, Islamic, and<br />

Hebrew calendars, as well as other calendars of historical interest. Further, they provide Common Lisp<br />

and C++ routines to convert dates between calendars, day of the week computations, and the<br />

determination of secular and religious holidays. <strong>The</strong>se are likely to be the most comprehensive and<br />

reliable calendrical routines you will be able to get your hands on, and are available at<br />

http://emr.cs.uiuc.edu:80/reingold/calendars.html.<br />

A nice package for calendrical computations in Mathematica by Ilan Vardi is available in the<br />

Packages/Miscellaneous directory of the standard Mathematica distribution, and also from MathSource.<br />

Vardi's book [Var91] discusses the theory behind the implementation, which provides support for the<br />

file:///E|/BOOK/BOOK4/NODE155.HTM (2 of 3) [19/1/2003 1:30:49]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!