Zeller's congruence
Zeller's congruence is an algorithm devised by Christian Zeller in the 19th century to calculate the day of the week for any Julian or Gregorian calendar date. It can be considered to be based on the conversion between Julian day and the calendar date.
Formula
For the Gregorian calendar, Zeller's congruence is
- [math]\displaystyle{ h = \left(q + \left\lfloor\frac{13(m+1)}{5}\right\rfloor + K + \left\lfloor\frac{K}{4}\right\rfloor + \left\lfloor\frac{J}{4}\right\rfloor - 2J\right) \bmod 7, }[/math]
for the Julian calendar it is
- [math]\displaystyle{ h = \left(q + \left\lfloor\frac{13(m+1)}{5}\right\rfloor + K + \left\lfloor\frac{K}{4}\right\rfloor + 5 - J\right) \bmod 7, }[/math]
where
- h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, ..., 6 = Friday)
- q is the day of the month
- m is the month (3 = March, 4 = April, 5 = May, ..., 14 = February)
- K the year of the century ([math]\displaystyle{ year \bmod 100 }[/math]).
- J is the zero-based century (actually [math]\displaystyle{ \lfloor year/100 \rfloor }[/math]) For example, the zero-based centuries for 1995 and 2000 are 19 and 20 respectively (not to be confused with the common ordinal century enumeration which indicates 20th for both cases).
- [math]\displaystyle{ \lfloor...\rfloor }[/math] is the floor function or integer part
- mod is the modulo operation or remainder after division
In this algorithm January and February are counted as months 13 and 14 of the previous year. E.g. if it is 2 February 2010, the algorithm counts the date as the second day of the fourteenth month of 2009 (02/14/2009 in DD/MM/YYYY format)
For an ISO week date Day-of-Week d (1 = Monday to 7 = Sunday), use
- [math]\displaystyle{ d = ((h + 5) \bmod 7) + 1 }[/math]
Analysis
These formulas are based on the observation that the day of the week progresses in a predictable manner based upon each subpart of that date. Each term within the formula is used to calculate the offset needed to obtain the correct day of the week.
For the Gregorian calendar, the various parts of this formula can therefore be understood as follows:
- [math]\displaystyle{ q }[/math] represents the progression of the day of the week based on the day of the month, since each successive day results in an additional offset of 1 in the day of the week.
- [math]\displaystyle{ K }[/math] represents the progression of the day of the week based on the year. Assuming that each year is 365 days long, the same date on each succeeding year will be offset by a value of [math]\displaystyle{ 365\bmod 7 = 1 }[/math].
- Since there are 366 days in each leap year, this needs to be accounted for by adding another day to the day of the week offset value. This is accomplished by adding [math]\displaystyle{ \left\lfloor\frac{K}{4}\right\rfloor }[/math] to the offset. This term is calculated as an integer result. Any remainder is discarded.
- Using similar logic, the progression of the day of the week for each century may be calculated by observing that there are 36,524 days in a normal century and 36,525 days in each century divisible by 400. Since [math]\displaystyle{ 36525\bmod 7 = 6 }[/math] and [math]\displaystyle{ 36524\bmod 7 = 5 }[/math], the term [math]\displaystyle{ \left\lfloor\frac{J}{4}\right\rfloor - 2J }[/math] accounts for this.
- The term [math]\displaystyle{ \left\lfloor\frac{13(m+1)}{5}\right\rfloor }[/math] adjusts for the variation in the days of the month. Starting from January, the days in a month are {31, 28/29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}. February's 28 or 29 days is a problem, so the formula rolls January and February around to the end so February's short count will not cause a problem. The formula is interested in days of the week, so the numbers in the sequence can be taken modulo 7. Then the number of days in a month modulo 7 (still starting with January) would be {3, 0/1, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3}. Starting in March, the sequence basically alternates 3, 2, 3, 2, 3, but every five months there are two 31-day months in a row (July–August and December–January).[1] The fraction 13/5 = 2.6 and the floor function have that effect; the denominator of 5 sets a period of 5 months.
- The overall function, [math]\displaystyle{ \operatorname{mod}\,7 }[/math], normalizes the result to reside in the range of 0 to 6, which yields the index of the correct day of the week for the date being analyzed.
The reason that the formula differs for the Julian calendar is that this calendar does not have a separate rule for leap centuries and is offset from the Gregorian calendar by a fixed number of days each century.
Since the Gregorian calendar was adopted at different times in different regions of the world, the location of an event is significant in determining the correct day of the week for a date that occurred during this transition period. This is only required through 1929, as this was the last year that the Julian calendar was still in use by any country on earth, and thus is not required for 1930 or later.
The formulae can be used proleptically, but "Year 0" is in fact year 1 BC (see astronomical year numbering). The Julian calendar is in fact proleptic right up to 1 March AD 4 owing to mismanagement in Rome (but not Egypt) in the period since the calendar was put into effect on 1 January 45 BC (which was not a leap year). In addition, the modulo operator might truncate integers to the wrong direction (ceiling instead of floor). To accommodate this, one can add a sufficient multiple of 400 Gregorian or 700 Julian years.
Examples
For 1 January 2000, the date would be treated as the 13th month of 1999, so the values would be:
- [math]\displaystyle{ q = 1 }[/math]
- [math]\displaystyle{ m = 13 }[/math]
- [math]\displaystyle{ K = 99 }[/math]
- [math]\displaystyle{ J = 19 }[/math]
So the formula evaluates as [math]\displaystyle{ (1 + 36 + 99 + 24 + 4 - 38) \bmod 7 = 126 \bmod 7 = 0 = \text{Saturday} }[/math].
(The 36 comes from [math]\displaystyle{ (13 + 1) \times 13/5 = 182/5 }[/math], truncated to an integer.)
However, for 1 March 2000, the date is treated as the 3rd month of 2000, so the values become
- [math]\displaystyle{ q = 1 }[/math]
- [math]\displaystyle{ m = 3 }[/math]
- [math]\displaystyle{ K = 0 }[/math]
- [math]\displaystyle{ J = 20 }[/math]
so the formula evaluates as [math]\displaystyle{ (1 + 10 + 0 + 0 + 5 - 40) \bmod 7 = -24 \bmod 7 = 4 = \text{Wednesday} }[/math].
Implementations in software
Basic modification
The formulas rely on the mathematician's definition of modulo division, which means that −2 mod 7 is equal to positive 5. Unfortunately, in the truncating way most computer languages implement the remainder function, −2 mod 7 returns a result of −2. So, to implement Zeller's congruence on a computer, the formulas should be altered slightly to ensure a positive numerator. The simplest way to do this is to replace − 2J with + 5J and − J with + 6J.
For the Gregorian calendar, Zeller's congruence becomes
- [math]\displaystyle{ h = \left(q + \left\lfloor\frac{13(m+1)}{5}\right\rfloor + K + \left\lfloor\frac{K}{4}\right\rfloor + \left\lfloor\frac{J}{4}\right\rfloor + 5J\right) \bmod 7, }[/math]
For the Julian calendar, Zeller's congruence becomes
- [math]\displaystyle{ h = \left(q + \left\lfloor\frac{13(m+1)}{5}\right\rfloor + K + \left\lfloor\frac{K}{4}\right\rfloor + 5 + 6J\right) \bmod 7, }[/math]
One can readily see that, in a given year, the last day of February and March 1 are a good test dates.
As an aside note, if we have a three-digit number abc, where a, b, and c are the digits, each nonpositive if abc is nonpositive; we have (abc) mod 7 = 9*a + 3*b + c. Repeat the formula down to a single digit. If the result is 7, 8, or 9, then subtract 7. If, instead, the result is negative, then add 7. If the result is still negative, then add 7 one more time. Utilizing this approach, we can avoid the worries of language specific differences in mod 7 evaluations. This also may enhance a mental math technique.
Common simplification
Zeller used decimal arithmetic, and found it convenient to use J and K in representing the year. But when using a computer, it is simpler to handle the modified year Y and month m, which are Y - 1 and m + 12 during January and February:
For the Gregorian calendar, Zeller's congruence becomes
- [math]\displaystyle{ h = \left(q + \left\lfloor\frac{13(m+1)}{5}\right\rfloor + Y + \left\lfloor\frac{Y}{4}\right\rfloor - \left\lfloor\frac{Y}{100}\right\rfloor + \left\lfloor\frac{Y}{400}\right\rfloor\right) \bmod 7, }[/math]
In this case there is no possibility of underflow due to the single negative term because [math]\displaystyle{ \left\lfloor Y/4\right\rfloor \ge \left\lfloor Y/100\right\rfloor }[/math].
For the Julian calendar, Zeller's congruence becomes
- [math]\displaystyle{ h = \left(q + \left\lfloor\frac{13(m+1)}{5}\right\rfloor + Y + \left\lfloor\frac{Y}{4}\right\rfloor + 5\right) \bmod 7, }[/math]
The algorithm above is mentioned for the Gregorian case in RFC 3339, Appendix B, albeit in an abridged form that returns 0 for Sunday.
Other variations
At least three other algorithms share the overall structure of Zeller's congruence in its "common simplification" type, also using an m ∈ [3, 14] ∩ Z and the "modified year" construct.
- Michael Keith published a piece of very short C code in 1990 for Gregorian dates. The month-length component ([math]\displaystyle{ \left\lfloor\frac{13(m+1)}{5}\right\rfloor }[/math]) is replaced by [math]\displaystyle{ \left\lfloor\frac{23m}{9}\right\rfloor + 4 }[/math].[2]
- J R Stockton provides a Sunday-is-0 version with [math]\displaystyle{ \left\lfloor\frac{13(m-2)}{5}\right\rfloor + 2 }[/math], calling it a variation of Zeller.[2]
- Claus Tøndering describes [math]\displaystyle{ \left\lfloor\frac{31(m-2)}{12}\right\rfloor }[/math] as a Sunday-is-0 replacement.[3]
Both expressions can be shown to progress in a way that is off by one compared to the original month-length component over the required range of m, resulting in a starting value of 0 for Sunday.
See also
- Determination of the day of the week
- Doomsday rule
- ISO week date
- Julian day
References
- ↑ The every five months rule only applies to the twelve months of a year commencing on 1 March and ending on the last day of the following February.
- ↑ 2.0 2.1 Stockton, J R. "Material Related to Zeller's Congruence". https://people.cs.nctu.edu.tw/~tsaiwn/sisc/runtime_error_200_div_by_0/www.merlyn.demon.co.uk/zel-like.htm.
- ↑ Tøndering, Claus. "Week-related questions". https://www.tondering.dk/claus/cal/chrweek.php.
Bibliography
Each of these four similar imaged papers deals firstly with the day of the week and secondly with the date of Easter Sunday, for the Julian and Gregorian calendars. The pages link to translations into English.
- Zeller, Christian (1882). "Die Grundaufgaben der Kalenderrechnung auf neue und vereinfachte Weise gelöst" (in German). Württembergische Vierteljahrshefte für Landesgeschichte V: 313–314. Archived from the original on January 11, 2015. https://web.archive.org/web/20150111014825/http://www.merlyn.demon.co.uk/zel-82px.htm.
- Zeller, Christian (1883). "Problema duplex Calendarii fundamentale" (in Latin). Bulletin de la Société Mathématique de France 11: 59–61. Archived from the original on January 11, 2015. https://web.archive.org/web/20150111015331/http://www.merlyn.demon.co.uk/zel-83px.htm.
- Zeller, Christian (1885). "Kalender-Formeln" (in German). Mathematisch-naturwissenschaftliche Mitteilungen des mathematisch-naturwissenschaftlichen Vereins in Württemberg 1 (1): 54–58. Archived from the original on January 11, 2015. https://web.archive.org/web/20150111014149/http://www.merlyn.demon.co.uk/zel-85px.htm.
- Zeller, Christian (1886). "Kalender-Formeln" (in German). Acta Mathematica 9: 131–136. doi:10.1007/BF02406733.
External links
- The Calendrical Works of Rektor Chr. Zeller: The Day-of-Week and Easter Formulae by J R Stockton, near London, UK. The site includes images and translations of the above four papers, and of Zeller's reference card "Das Ganze der Kalender-Rechnung".
- This article incorporates public domain material from the NIST document: Black, Paul E.. "Zeller's congruence". https://xlinux.nist.gov/dads/HTML/zellersCongruence.html.
Original source: https://en.wikipedia.org/wiki/Zeller's congruence.
Read more |