Ruby Tuesday – Date#jd

Today’s Ruby Tuesday covers Date#jd.

Date#jd returns the number of days since the beginning of the Julian calendar.

require 'date'
# => true

Date.new.jd
# => 0
Date.new(2015, 04, 14).jd
# => 2457127
Date.new(0, 1, 1).jd
# => 1721058

If you notice above, we get 1721058 for the days since the start of the Julian calendar. One thing to notice is that we can pass an optional “Julian day number which denotes the day of calendar reform” to Date#new. If we pass in the constant Date::GREGORIAN to new, and then call Date#jd on the result, we get a different value coming back for the number of days since the start of the Julian calendar, 1721060 vs 1721058.

Date.new(0, 1, 1, Date::GREGORIAN).jd
=> 1721060

While Ruby does not have a corresponding method for getting the number of days since the start of the Gregorian calendar, we can do a difference between the number of Julian days for a date and the number of Julian days for the date 0000-01-01. But be warned, as we just saw above, to get an accurate number, we need to specify that we want 0000-01-01 to be the Gregorian version of that date.

Date.new(2015, 04, 14).jd - Date.new(0, 1, 1).jd
# => 736069
Date.new(2015, 04, 14).jd - Date.new(0, 1, 1, Date::GREGORIAN).jd
# => 736067
Date.new(2015, 04, 14, Date::GREGORIAN).jd - Date.new(0, 1, 1, Date::GREGORIAN).jd
# => 736067

If we just construct a new Date, and do not specify and Julian start date, we notice that we can see the number of Julian days in the constructor, and if we specify we want the date to be Gregorian calendar based, we can see the number of Julian days is different there as well.

Date.new(0, 1, 1)
# => #<Date: 0000-01-01 ((1721058j,0s,0n),+0s,2299161j)>
Date.new(0, 1, 1, Date::GREGORIAN)
# => #<Date: 0000-01-01 ((1721060j,0s,0n),+0s,-Infj)>

Hope this shined as little bit of knowledge on Ruby Dates for you as it did me.

–Proctor

Leave a Reply

Your email address will not be published. Required fields are marked *