The other day I was perusing a Ruby backtrace and paused as something caught my eye: Date::ITALY. What could that be?
irb(main):001> Date::ITALY
=> 2299161
Allrighty, not exactly self-explanatory. Upon visiting the documentation for class Date, I was greeted by a few other unusual sounding constants, including ENGLAND, GREGORIAN, and JULIAN. What do the docs have to say about these?
ENGLAND
The Julian day number of the day of calendar reform for England and her colonies.
GREGORIAN
The Julian day number of the day of calendar reform for the proleptic Gregorian calendar.
ITALY
The Julian day number of the day of calendar reform for Italy and some catholic countries.
JULIAN
The Julian day number of the day of calendar reform for the proleptic Julian calendar.
First of all, I think it is humorous that the Ruby source code mentions England and her colonies as well as some catholic countries. Second, I learned the term proleptic, which refers to extrapolating calendar dates outside the time of their adoption or use. But OK, the question on everyone’s mind – what is a Julian day number? Well, not so fast, because first we have to cover the catholicism part.
The Gregorian Switchover
The Julian calendar, established by the eponymous Caesar, had been cooking along fairly accurately for quite some time. But, it wasn’t perfect, losing a day about every 128 years. By the time of Pope Gregory XIII in the 1580s, the date of Easter had unacceptably moved about two weeks towards the Summer. In 1582, the Papal bull Inter gravissimas declared that the day following Thursday, 4 October 1582 would be Friday, 15 October 1582. Indeed, Ruby conforms, raising an exception if you attempt to instantiate a date within that ten day period:
irb(main):001> Date.new(1582, 10, 5)
(irb):1:in 'Date#initialize': invalid date (Date::Error)
and helpfully hops right over them when iterating, like it never happened:
irb(main):001> Date.new(1582, 10, 4).next_day
=> #<Date: 1582-10-15 ((2299161j,0s,0n), +0s,2299161j)>
Wait a moment, where have we seen that number before? Yes, Date::ITALY encodes the Julian day on which Italy switched from the Julian calendar to the Gregorian calendar – Julian day 2,299,161. If you were ever wondering what those weird numbers were in Date#inspect, those are Julian Day numbers.
Julian Days
Like all good stories, this one starts over six thousand years ago. On a very special Monday, New Year’s day in 4713 BC, at exactly noon UTC, the three most important multi-year cycles of humankind were perfectly synchronized: the 28 year solar cycle, the 19 year lunar cycle, and of course the 15 year Roman tax collection cycle, commonly referred to as the indiction cycle. Indeed, the Julian day cycle is a period of 28 x 19 x 15 = 7980 years, which will roll over once again in the year 3268 AD.
Why do we need this? As we saw earlier, there are some ‘gaps’ in the accounting of days in human history. To add to the confusion, not all countries adopted the Gregorian calendar at the same time – England for example, waited until 1752, as the Protestants were hesitant to adopt Catholic innovation (hence, Date::ENGLAND). Therefore, a sort of ‘universal intermediary’ was required to compute back and forth unambiguously between various calendar systems. Thus a simple count of days from a somewhat arbitrary epoch was created. Literally 1 year after the introduction of the Gregorian calendar, scholar Joseph Scaliger proposed the Julian Period method. Unbelievably confusingly, this Period was not named for the Julian calendar, or Julius Caesar that it was named after – it was named after his father Julius Caesar Scaliger, who was himself named after Julius Caesar. This is not unlike the Caesar salad, famously named after Caesar Cardini (famously named after Julius Caesar (whose name is famously actually Gaius.))
Ok, back to Ruby. We now know that Date::ITALY and Date::ENGLAND define the Julian day number that Italy and England respectively adopted the Gregorian calendar. That leaves Date::GREGORIAN and Date::JULIAN. I bet you’ll never guess:
irb(main):001> Date::GREGORIAN
=> -Infinity
irb(main):002> Date::JULIAN
=> Infinity
Alrighty, not exactly self-explanatory. To make sense of this, we will have to understand the Date initializer:
new(year = -4712, month = 1, mday = 1, start = Date::ITALY)
Two things here: first, the default date is equivalent to Date.jd(0). If you have ever wondered why Date.new.to_s gives "-4712-01-01", now you know. Second, the optional start argument is the ‘start of the Gregorian calendar’, expressed in Julian Days.
If you were working on a SaaS product in England in the 1700s, you would express Sir Isaac Newton’s DOB as Date.new(1642, 12, 25, Date::ENGLAND) rather than Date.new(1643, 1, 4). This also means that Date.new(1582, 10, 5, Date::ENGLAND) does not raise an exception like it does for Date::ITALY. In English parlance, Isaac’s birthday expressed as 1642/12/25 would be called the ‘old style’, and 1643/1/4 the ‘new style’ date. This is why Ruby’s date_core.c often mentions os and ns.
If you always want to use the Julian calendar, you could kick the ‘Gregorian switchover’ can infinitely down the road. This is why Date::JULIAN is Float::INFINITY. Passing Date::GREGORIAN (-Float::INFINITY) implies that the switchover happened ‘infinitely long ago’, or in other words, ‘we have always been using the Gregorian calendar’.
Because these values are assumed to be Julian Days, you can also provide your own arbitrary values. For example, if you wanted to model dates in Russia, where the Gregorian calendar was adopted on 14 February, 1918, you would:
irb(main):001> Date.new(1918, 1, 31, 2421639).next_day
=> #<Date: 1918-02-14 ((2421639j,0s,0n),+0s,2421639j)>
God help us if we ever switch to a new calendar system, because a single start argument won’t cut it anymore!
Closing Thoughts
If you are wondering if Isaac Newton actually was born on Christmas or rather January 4th, well, that is exactly the issue that Gregory XIII was out to solve!
Because the Julian Period is a synchronization of three other cycles, it is referred to as a tricyclic system.
The indiction cycle may have been chosen as a third cycle because historical records often were linked not to a date but rather an indiction number.
Contrary to what I said above, it is contested as to whether the Julian Period was originally referring to the Julian calendar or rather named after Joseph’s father.
In the inspect format of a date, for example #<Date: 1582-10-15 ((2299161j,0s,0n), +0s,2299161j)>, you are seeing the date expressed as a Julian day number, plus the seconds and nanoseconds of a time component, then a time zone offset, and finally the provided Julian day of the Gregorian switchover.
Left as an exercise for the reader, there are a few fun methods in class Date as well:
#gregorian_leap?#julian_leap?#jisx0301#rfc2822#rfc3339
Further Reading
- https://docs.ruby-lang.org/en/master/language/calendars_rdoc.html#argument-start
- https://github.com/ruby/ruby/blob/master/ext/date/date_core.c
- https://quasar.as.utexas.edu/BillInfo/JulianDatesG.html
- https://en.wikipedia.org/wiki/Julian_day