Dates

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Introduction

Dates are really, really, REALLY hard.

It's not so much that individual languages or modules treat dates wrong, it's that dates are bad, period.

Understanding What Dates are

To get to the heart of the problem, you have to examine what a date really is. In short, dates are a measure of a length of time that has passed since a certain event, in some strange unit system that involves days, weeks, months, years and more. Combined with the fact that days, months, years, etc... are not even of equal length, you have serious problems.

One Date System to Rule Them All

Astronomers need to use dates, and they need their dates to extend from all time to beyond all time. They have settled on Julian Dates, which counts the number of days that have passed since a specific date in the past, with days all being of uniform length.

This would be the eternally useful system, since it's trivial to calculate stuff, but the problem is that the earth and sun and moon don't agree with it.

Gregorian Calendar

Most modern nations use the Gregorian Calendar. This contains a tiny correction to leap years, to keep the day of the year in sync year to year. This extra day in the year is added in because the earth does not take exactly 365 days to circle the sun. (It actually varies quite a bit year to year.)

When we talk about dates, most people assume we are talking about the Gregorian Calendar, which is fine.

Leap Seconds

Because the earth is not rotating at exactly 1 revolution per 24 hours, and it actually changes over time, we sometimes need to add leap seconds so that we don't drift too much. This causes all sorts of problems that need to be accounted for in your date system. Combined with the fact that we don't know whether there will be a leap second until a little while before it happens, and we have sheer chaos. It's important to ask whether the time range includes leap seconds or not, if you are worried about seconds. A range of years could have a few more seconds added into it. Of course, you could assume that every day has the same number of seconds, but your seconds won't line up exactly with everyone else's. Namely, in years with the leap second, your seconds will be slightly slower.

Other Calendars

Much of humanity still uses alternative calendars. Thankfully, they also use the Gregorian Calendar, but oftentimes important holidays are measured by the alternate calendars. Of note:

  • Asian countries may use the Chinese calendar for birthdays and important holidays such as New Years.
  • Muslim counties use the Muslim calendar for all of their holidays.
  • Israel uses the Jewish Calendar, as well as observant Jews, to calculate their holidays.

In short, it's a mess. You can find ways to convert from one calendar to another, but it's always complicated.

The Ideal "Date" Object

If you want a single type that can capture ALL of the possible calendars and days, you need a very flexible Date type, and you need the code to convert back and forth. It's not easy, but I think it can be done.