Metalbeard
Metalbeard

Reputation: 347

Is the Linux cal command correct until year 9999?

The cal command obviously checks for leap years like this:

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) then leapyear=true

My question is whether there are any other leap years from now until the year 9999 (valid range of years for this command) that are not covered by this definition. If yes, are these also covered by the cal command?

Upvotes: 0

Views: 632

Answers (3)

Wintermute
Wintermute

Reputation: 44063

Not by the rules of the Gregorian calendar, so cal is a correct implementation of that calendar.

Whether the Gregorian calendar will be in use until the year 9999, though, is anyone's guess. My guess is that it will not survive unchanged because its imprecision becomes a noticeable factor after a few thousand years.

The tropical (natural) year is roughly 365.2422* days long at the moment (it becomes more over time as the Earth's rotation slows, but slowly so). The Gregorian calendar's 4-but-not-100-except-400 means that a Gregorian year is, on average, 365.2425 days long. This is not a bad approximation, but it means that a day is lost roughly every 3236+ years at this point in Earth's history.

When exactly people decide that this is a problem and insert another leap day (or change the rules altogether) is something that the implementors of cal cannot possibly predict, and so they stick to the rules of the Gregorian calendar.

*on average. It varies slightly because of the gravitational pull from other planets.

+ I looked that up. There's some variance on that figure as well, naturally.

Upvotes: 1

Aubrey Kilian
Aubrey Kilian

Reputation: 366

From https://www.timeanddate.com/date/leapyear.html :

In the Gregorian calendar 3 criteria must be taken into account to identify leap years:

  • The year is evenly divisible by 4;
  • If the year can be evenly divided by 100, it is NOT a leap year, unless;
  • The year is also evenly divisible by 400. Then it is a leap year. This means that 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.

Looking at the Debian source for cal: http://anonscm.debian.org/cgit/bsdmainutils/bsdmainutils.git/tree/usr.bin/ncal/calendar.c#n205

It does seem like they're doing the right thing there.

Upvotes: 1

llogiq
llogiq

Reputation: 14541

If your civilization follows the Gregorian calendar (otherwise you probably would not use Linux), this is the exact definition of the leap year. This somewhat strange definition was used because a celestial year (one rotation of the earth around the sun) takes a little more than 365, but a little less than 366 days. The way it is calculated comes out at 365 + 1/4 - 1/100 + 1/400 = 365.24925 days.

Upvotes: 1

Related Questions