Day of the week centuries from now

Yesterday I wrote a post outlining mental math posts I’d written over the years. I don’t write about mental math that often, but I’ve been writing for a long time, and so the posts add up. In the process of writing the outline post I noticed a small gap.

In the post on mentally calculating the day of the week I focus on this century, then tersely say “For dates in the 20th century, add 1. For dates in the 22nd century, subtract 2.” This post will expand on that. I’ll say a little more about what these rules mean, how to extend them to other centuries, and why they work.

Dates in the 20th century

Suppose you wanted to know the day of the week for July 4, 1976. You could use the method in the post linked above and end up with Saturday. Moving ahead one day of the week gives you Sunday.

Dates in future centuries

For days in the 2100s you would find the day of the week for the corresponding date in this century, then subtract two days. For the 2200s you’d add three days, and for the 2300s you’d add one day.

The Gregorian calendar repeats every 400 years, so if you can find the days of the week for the years 2000 through 2399, you can find the day of the week for all years in the future by subtracting enough multiples of 400 to get into that range of years. For example, in the year 2825, September 18 will fall on a Thursday because it falls on a Thursday today in 2025.

You could also go backward by centuries as well as forward, but there’s a complication. If you run the Gregorian calendar backward far enough, you run into a time before the Gregorian calendar was adopted.

Why the rules work

So why do the century adjustment rules work? There’s a simple but incomplete explanation, and a more complicated complete explanation.

Partial explanation

The number of days in most centuries, i.e. those not divisible by 400, is

365 × 100 + 24 = 36524

which is congruent to 5 mod 7, which is congruent to −2 mod 7. Dates in the 2100s are 36524 days ahead of their counterparts in the 2000s, so we move back two days in the week. The same applies when going from the 2100s to the 22oos, and from the 2200s to the 2300s.

For example, September 18, 2125 is 36524 days from now—note that February, 2100 will not have a Leap Day—and so it will fall two days earlier in the week, on a Tuesday.

That explanation is mostly correct, but there’s a wrinkle. Not all dates in the 2100s are 36524 days ahead of their counterparts in the 2000s. There are indeed 36524 days between September 18, 2025 and September 18, 2125. But here are 36525 days between January 1, 2000 and January 1, 2100. The former was on a Saturday and the latter will be on a Friday. It would seem that our process is wrong, but it’s not.

Full explanation

Let’s go back and look at the algorithm for finding days of the week,

  1. Take the last two digits of the year and add the number of times 4 divides that number [1].
  2. Add a constant corresponding to the month.
  3. Add the day of the month.
  4. Subtract 1 in January or February of a leap year.
  5. Take the remainder by 7.
  6. Adjust for the century.

Suppose we wanted to find the day of the week for January 1, 2100. The first step gives us 0. The constant for January is 6, so at the end of step 2 we have 6. At the end of step 3 we have 7.

Now comes the key part: in step 4 we do not subtract 1 because 2100 is not a leap year. Step 5 gives us 0, i.e. Sunday. When we adjust for the century by moving back two days, we get Friday.

This explanation is admittedly tedious, but it reveals a subtle part of the algorithm: you have to carry out the algorithm, in particular step 4, for the original year. To find the day of the week for a year in the 2200s, for example, you carry out the algorithm as if it were valid for the 2200s, until you get to the last step.

Related posts

[1] This is known as the year share. There are many ways to calculate it (mod 7) that are less obvious but easier to calculate mentally. It’s interesting that these methods generally look more complicated in writing, but they’re easier to carry out.

A bit-twiddling marvel

Pop quiz: what does the following code do?

bool is_leap_year_fast(uint32_t y) {
    return ((y * 1073750999) & 3221352463) <= 126976;
}

It determines whether the year y is a leap year in the Gregorian calendar, of course. :)

It’s very efficient, though I don’t image that would ever matter. But it’s very clever.

Gregorian vs Julian calendar

A year is a leap year in the Julian calendar if and only if it is a multiple of 4. But the Julian year is a bit too long on average to match the earth’s orbit around the sun. You get a much better fit if you add the complication that years divisible by 100 but not by 400 are not leap years.

Presumably no one reading this recalls 1900 not being a leap year, but some of you may need to know some day that 2100 is not a leap year either.

C code

The cryptic function above comes from the recent post A leap year check in three instructions by Falk Hüffner. The function is correct for the next 100 thousand years (more precisely, through 103499) and correct if we anachronistically extent the Gregorian calendar back to the year 0.

The following C code shows empirically that Hüffner’s code is correct, but you’ll have to read his post to understand why it is correct.

#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

bool is_leap_year_fast(uint32_t y) {
    return ((y * 1073750999) & 3221352463) <= 126976;
}

bool is_leap_year(uint32_t y) {
    if ((y % 4) != 0) return false;
    if ((y % 100) != 0) return true;
    if ((y % 400) == 0) return true;
    return false;
}

int main() {
    for (uint32_t y = 0; y <= 102499; y++)
        if (is_leap_year_fast(y) != is_leap_year(y))
            printf("Exception: %d\n", y);
    return 0;
}

Related posts

Running the Gregorian calendar backwards

Toward the end of last year I wrote several blog posts about calendars. The blog post about the Gregorian calendar began with this paragraph.

The time it takes for the earth to orbit the sun is not an integer multiple of the time it takes for the earth to rotate on its axis, nor is it a rational number with a small denominator. Why should it be? Much of the complexity of our calendar can be explained by rational approximations to an irrational number.

The post went on to say why the Gregorian calendar was designed as it was.

Calendar year and astronomical year

In a nutshell, the average length of a year in the Gregorian calendar is 365 97/400 days, which matches the astronomical year much better than the Julian calendar, which has an average year length of 365 ¼ days.

In the Julian calendar, every year divisible by 4 is a leap year. The Gregorian calendar makes an exception: centuries are not leap years unless they are divisible by 400. So the year 2000 was a leap year, but 1700, 1800, and 1900 were not. Instead of having 100 leap years every 400 years, the Gregorian calendar has 97 leap years every 400 years.

Why does it matter whether the calendar year matches the astronomical year? In the short run it makes little difference, but in the long run it matters more. Under the Julian calendar, the Spring equinox occurred around March 21. If the world had remained on the Julian calendar, the date of the Spring equinox would drift later and later, moving into the summer and eventually moving through the entire year. Plants that bloom in March would eventually bloom in what we’d call July. And instead of the dog days of summer being in July, eventually they’d be in what we’d call November.

Removing days

The Gregorian calendar wanted to do two things: stop the drift of the seasons. and restore the Spring equinox to March 21. The former could have been accomplished with little disruption by simply using the Gregorian calendar moving forward. The latter was more disruptive since it required removing days from the calendar.

The Julian year was too long, gaining 3 days every 400 years. So between the time of Jesus and the time of Pope Gregory, the calendar had drifted by about 12 days. In order to correct for this, the calendar would have to jump forward about a dozen years. If you think moving clocks forward an hour is disruptive, imagine moving the calendar forward a dozen days.

The Gregorian calendar didn’t remove 12 days; it removed 10. In the first countries to adopt the new calendar in 1582, Thursday, October 4th in 1582 was followed by Friday, October 15th. Note that Thursday was followed by Friday as usual. The seven-day cycle of days of the week was not disrupted. That’ll be important later on.

Why did the Gregorian calendar remove 10 days and not 12?

We can think of the 10 days that were removed as corresponding to previous years that the Julian calendar considered a leap year but that the Gregorian calendar would not have: 1500, 1400, 1300, 1100, 1000, 900, 700, 600, 500, and 300. Removing 10 days put the calendar in sync astronomically with the 300’s. This is significant because the Council of Nicaea met in 325 and made decisions regarding the calendar. Removing 10 days in 1582 put the calendar in sync with the calendar at the time of the council.

Making the calendar retroactive

The calendar that pushes the Gregorian calendar back in time, before the adoption of the calendar, is known as the proleptic Gregorian calendar. [1]

Most scholars say Jesus was crucified on Friday, April 3, 33 AD. What exactly does “April 3, 33” mean? Was that a Julian date or a Gregorian date? There’s a possible difference of two days, corresponding to whether or not the years 100 and 200 were considered leap years.

If we were to push the Gregorian calendar back to the first century, the calendar for April in 33 AD would be the same as the calendar in 2033 AD (five cycles of 400 years later). April 3, 2033 is on a Sunday. (You could look that up, or use the algorithm given here.) April 3, 33 in the Julian calendar corresponds to April 1, 33 in the Gregorian calendar. So April 3, 33 was a Friday in the Julian calendar, the calendar in use at the time.

Some scholars date the crucifixion as Friday, April 7, 30 AD. That would also be a Julian date.

Related posts

[1] Origin of prolepsis according to Etymonline:

1570s, “anticipation, the taking of something anticipated as already done or existing,” also “the assignment of something to a too early date,” from Latin prolepsis, from Greek prolēpsis “an anticipating,” etymologically “a taking beforehand,” from prolambanein “to take before, receive in advance,” from pro “before” + lambanein “to take”.

Martian Leap Years

The previous post looked at one challenge with designing a calendar for Mars, namely how to vary the number of days per month so that each month corresponds to a change of 30 degrees with respect to the sun. This is a bigger problem on Mars than here on Earth.

That post assumed that a Martian year consisted of 669 sols (Martian days). But a Martin year is not an integer number of sols, just as an Earth year is not an integer number of days. In fact, a Martian year is 668.5991 sols.

One way to handle this would be for 3 out of every 5 years to have 669 sols, with the remaining years having 668 sols. So maybe if the year mod 5 equals 0, 1 or 2 the year would be long, 669 sols, and otherwise it would be short, 668 sols. (This alternation of 3 and 2 reminds me of Dave Brubeck’s song Take Five.)

This scheme, which approximates 668.5991 by 668.6, is about as accurate as our Gregorian calendar, which approximates 365.2422 by 365.2425. For more accuracy, Martian’s would need something like our leap seconds.

A calendar for Mars

I recently started reading The Case for Mars by Robert Zubrin. This post will unpack one line from that book regarding creating a calendar for Mars:

Equipartitioned months don’t work for Mars, because the planet’s orbit is elliptical, which causes the seasons to be of unequal length.

This sentence doesn’t sit well at first for a couple reasons. First, Earth’s orbit is elliptical too, and the seasons here are of roughly equal length. Second, the orbit of Mars, like the orbit of Earth, is nearly circular.

There are three reasons why Zubrin’s statement is correct, despite the objections above. The first has to do with the nature of eccentricity, and the second with the reference to which angles are measured, and the third with variable speed.

Eccentricity

The orbit of Mars is about five and a half times as eccentric as that of Earth. That does not mean that the orbit of Mars is noticeably different from a circle, but it does mean the sun is noticeably not at the center of that (almost) circle.

There’s a kind of paradox interpreting eccentricity e. An ellipse with e = 0 is a circle, and the two foci of the ellipse coincide with the center of the circle. As e increases, the ellipse aspect ratio increases and the foci move apart. But here’s the key: the aspect ratio doesn’t change nearly as fast as the distance between the two foci changes. I’ve written more about this here and here.

So while the orbit of Mars is nearly a circle, the sun is substantially far from the center of the orbit. We can visualize this with a couple plots. First, here are the orbits of Earth and Mars, shifted so that both have their center at the origin.

Both are visually indistinguishable from circles.

How here are the two orbits with their correct placement relative to the sun at the center.

Angle reference

Zubrin writes

In order to predict the seasons, a calendar must divite the planet’s orbit not into equal division of days , but into equal angles of travel  around the sun. … a month is really 30 degrees of travel around the Sun.

If we were to divide the orbit of Mars into partitions of 30 degrees relative to the center of the orbit then each month would be about the same length. But Zubrin is dividing the orbit into partitions of 30 degrees relative to the sun.

In the language of orbital mechanics, Zubrin’s months correspond to 30 degrees of true anomaly, not 30 degrees of mean anomaly. I explain the difference between true anomaly and mean anomaly here. That post shows that for Earth, true anomaly and mean anomaly never differ by more than 2 degrees. But for Mars, the two anomalies can differ by up to almost 19 degrees.

Variable speed

A planet in an elliptical orbit around the sun travels fastest when it is nearest the sun and slowest when it is furthest from the sun. Because Mars’s orbit is more eccentric than Earth’s, the variation in orbital speed is greater. We can calculate the ratio of the fastest speed to the slowest speed using the vis-viva equation. It works to be

(1 + e)/(1 − e).

For Earth, with eccentricity 0.0167 this ratio is about 1.03, i.e. orbital speed varies by about 3%.

For Mars, with eccentricity 0.0934 this ratio is about 1.21, i.e. orbital speed varies by about 21%.

Zubrin’s months

The time it takes for Mars to rotate on its axis is commonly called a sol, a Martian day. The Martian year is 669 sols. Zubrin’s proposed months range from 46 to 66 sols, each corresponding to 30 degrees difference in true anomaly.

Related posts

Cycle of New Year’s Days

Here’s a visualization of how the day of the week for New Year’s Day changes.

The green diamonds represent leap years and the blue squares represent ordinary years.

The day of the week for New Year’s Day advances one day after each ordinary year and two days after each leap year, hence the diagonal stripes in the graph above.

The whole cycle repeats every 28 years. During that 28 year cycle, New Year’s Day falls on each day of the week four times: three times in an ordinary year and once in a leap year. Or to put it another way, each horizontal row of the graph above contains three blue squares and one green diamond.

The comments above are true under the Julian calendar, without exception. And they’re true for long stretches of time under the Gregorian calendar. For example, the pattern above repeats from 1901 to 2099.

The Julian calendar had a leap day every four years, period. This made the calendar year longer than the solar year by about 3 days every 400 years, so the Gregorian calendar removed 3 leap days. A year divisible by 100 is not a leap year unless it is also divisible by 400. So the Gregorian calendar disrupts the pattern above every 100 years.

Related posts

What exactly is a second?

The previous post looked into the common definition of Unix time as “the number of seconds since January 1, 1970 GMT” and why it’s not exactly true. It was true for a couple years before we started inserting leap seconds. Strictly speaking, Unix time is the number of non-leap seconds since January 1, 1970.

This leads down the rabbit hole of how a second is defined. As long as a second is defined as 1/86400 th of a day, and a day is the time it takes for the earth to rotate once on its axis, there’s no cause for confusion. But when you measure the rotation of the earth precisely enough, you can detect that the rotation is slowing down.

Days are getting longer

The rotation of the earth has been slowing down for a long time. A day was about 23½ hours when dinosaurs roamed the earth, and it is believed a day was about 4 hours after the moon formed. For most practical purposes a day is simply 24 hours. But for precision science you can’t have the definition of a second changing as the ball we live on spins slower.

This lead to defining the second in terms of something more constant than the rotation of the earth, namely the oscillations of light waves, in 1967. And it lead to tinkering with the calendar by adding leap seconds starting in 1972.

Cesium

You’ll hear that the second is defined in terms of vibrations of a cesium atom. But what exactly does that mean? What about the atom is vibrating? The second is not defined in terms of motions inside an atom, but by the frequency of the radiation produced by changes in an atom. Specifically, a second has been defined since 1967 as

the duration of 9,192,631,770 periods of the radiation corresponding to the transition between the two hyperfine levels of the ground state of the caesium-133 atom.

Incidentally, “cesium” is the American English spelling of the name of atomic element 55, and “caesium” is the IUPAC spelling.

The definition of a second raises several questions. Why choose cesium? Why choose that number of periods? And what are hyperfine levels and all that? I’ll attempt to answer the first two questions and punt on the third.

OK, so why cesium? Do we use cesium because that’s what atomic clocks use? And if so, why do atomic clocks use cesium?

As I understand it, the first atomic clocks were based on cesium, though now some atomic clocks are based on hydrogen or rubidium. And one reason for using Cs 133 was that it was easy to isolate that particular isotope with high purity.

Backward compatibility

So why 9,192,631,770 periods? Surely if we’d started from this definition we’d go with something like 10,000,000,000 periods. Clearly the number was chosen for backward compatibility with the historical definition of a second, but that only approximately settles the question. The historical definition was fuzzy, which was the point of the new definition, so which variation on the historical definition was used for backward compatibility?

The time chosen for backward compatibility was basically the length of the year 1900. Technically, the number of periods was chosen so that a second would be

the fraction 1/31556925.9747 of the tropical year for 1900 January 0 at 12 hours ephemeris time.

Here “tropical year” means the time it took earth to orbit the sun from the perspective of the “fixed stars,” i.e. from a vantage point so far away that it doesn’t matter exactly how far away it is. The length of a year varies slightly, and that’s why they had to pick a particular one.

The astronomical definition was just motivation; it has been discarded now that 9,192,631,770 periods of a certain radiation is the definition. We would not change the definition of a second if an alien provided us some day a more accurate measurement of the tropical year 1900.

Unix Time and a Modest Proposal

The time it takes earth to orbit the sun is not a simple multiple of the time it takes earth to rotate on its axis. The ratio isn’t even constant. The time it takes earth to circle the sun wobbles a bit, and the rotation of the earth is slowing down slightly.

The ratio is around 365.2422. Calling it 365 is too crude. Julius Caesar said we should call it 365 1/4, and that was good enough for a while. Then Pope Gregory said we really should use 365 97/400, and that’s basically good enough, but not quite. More on that here.

Leap seconds

In 1972 we started adding leap seconds in order to synchronize the day and the year more precisely. Unlike leap days, leap seconds don’t occur on a strict schedule. A leap second is inserted when a committee of astronomers decides one should be inserted, about every two years.

An international standards body has decided to stop adding leap seconds by 2035. They cause so confusion that it was decide that letting the year drift by a few seconds was preferable.

Unix time

Unix time is the number of seconds since the “epoch,” i.e. January 1, 1970, sorta.

If you were to naively calculate Unix time for this coming New Year’s Day, you’d get the right result.

New Year’s Day 2025

When New Year’s Day 2025 begins in England, the Unix time will be

(55 × 365 + 14) × 24 × 60 × 60 = 1735689600

This because there are 55 years between 1970 and 2025, 14 of which were leap years.

However, that moment will be 1735689627 seconds after the epoch.

Non-leap seconds

Unix time is the number of non-leap seconds since 1970-01-01 00:00:00 UTC. There have been 27 leap seconds since 1970, and so Unix time is 27 seconds behind elapsed time.

Leap year analogy

You could think of a second in Unix time as being 1/86400 th of a day. Every day has 86400 non-leap seconds, but some days have had 86401 seconds. A leap second could potentially be negative, though this has not happened yet. A day containing a negative leap second would have 86399 seconds.

The analogous situation for days would be to insist that every year have 365 days. February 28 would be the 59th day of the year, and March 1 would be the 60th day, even in a leap year.

International Atomic Time

What if you wanted a time system based on the actual number of elapsed seconds since the epoch? This is almost what International Atomic Time is.

International Atomic Time (abbreviated TAI, from the French temps atomique international) is ahead of UTC [1] by 37 seconds, not 27 seconds as you might expect. Although there have been 27 leap seconds since 1972, TAI dates back to 1958.

So New Year’s Day will start in England at 2025-01-01 00:00:37 TAI.

A Modest Proposal

It seems our choices are to add leap seconds and endure the resulting confusion, or not add leap seconds and allow the year to drift with respect to the day. There is a third way: adjust the position of the earth periodically to keep the solar year equal to an average Gregorian calendar day. I believe this modest proposal [2] deserves consideration.

Kepler’s law says the square of a planet’s orbital period is proportional to the cube of the semi-major axis of its orbit. This means that increasing earth’s orbital period by 1 second would only require moving earth 3.16 km further from the sun.

***

[1] UTC stands for Universal Coordinated Time. From an earlier post,

The abbreviation UTC is an odd compromise. The French wanted to use the abbreviation TUC (temps universel coordonné) and the English wanted to use CUT (coordinated universal time). The compromise was UTC, which doesn’t actually abbreviate anything.

[2] In case you’re not familiar with the term “modest proposal,” it comes from the title of a satirical essay by Jonathan Swift. A modest proposal has come to mean an absurdly simple solution to a complex problem presented satirically.

Perpetual Calendars

The previous post explained why the Gregorian calendar is the way it is, and that it consists of a whole number of weeks. It follows that the Gregorian calendar repeats itself every 400 years. For example, the calendar for 2025 will be exactly the same as the calendar for 1625 and 2425.

There are only 14 possible printed calendars, if you don’t print the year on the calendar. There are seven possibilities for the day of the week for New Year’s Day, and there are two possibilities for whether the year is a leap year.

A perpetual calendar is a set of the 14 possible calendars, along with some index that tells which possible calendar is appropriate in a given year.

Are each of the 14 calendars equally frequent? Almost, aside from the fact that leap years are less frequent. Each ordinary year calendar occurs 43 or 44 times, and each leap year calendar occurs 13, 14, or 15 times.

Related posts

Gregorian Calendar and Number Theory

The time it takes for the earth to orbit the sun is not an integer multiple of the time it takes for the earth to rotate on its axis, nor is it a rational number with a small denominator. Why should it be? Much of the complexity of our calendar can be explained by rational approximations to an irrational number.

Rational approximation

The ratio is of course approximately 365. A better approximation is 365.25, but that’s not right either. A still better approximation would be 365.2422.

A slightly less accurate, but more convenient, approximation is 365.2425. Why is that more convenient? Because 0.2425 = 97/400, and 400 is a convenient number to work with.

A calendar based on a year consisting of an average of 365.2425 days would have a 365 days most years, with 97 out of 400 years having 366 days.

In order to spread 97 longer years among the cycle of 400 years, you could insert an extra day every four years, but make three exceptions, such as years that are divisible by 100 but not by 400. That’s the Gregorian calendar that we use.

It’s predecessor, the Julian calendar, had an average year of 365.25 days, which was good enough for a while, but the errors began to accumulate to the point that the seasons were drifting noticeably with respect to the calendar.

Not much room for improvement

It would be possible to create a calendar with an even more accurate average year length, but at the cost of more complexity. Even so, such a calendar wouldn’t be much more accurate. After all, even the number we’ve been trying to approximate, 365.2422 isn’t entirely accurate.

The ratio of the time of the earth’s orbit to the time of its rotation isn’t even entirely constant. The Gregorian calendar is off by about 1 day in 3030 years, but the length of the year varies by about 1 day in 7700 years.

I don’t know how accurately the length of the solar year was known when the Gregorian calendar was designed over four centuries ago. Maybe the error in the calendar was less than the uncertainty in the length of the solar year.

Days of the week

Four centuries of the Gregorian calendar contain 146097 days, which is a multiple of 7. This seems to be a happy coincidence. There was no discussion of weeks in derivation above.

The implicit optimization criteria in the design of the calendar were minimizing the discrepancy between the lengths of the average calendar year and the solar year, minimizing the length of the calendar cycle, and using a cycle length that is a round number. It’s plausible that there was no design goal of making the calendar cycle an integer number of weeks.

Related posts