Home > database >  Ruby Date parse method returning odd results around the year 0 and other pre-modern times
Ruby Date parse method returning odd results around the year 0 and other pre-modern times

Time:06-08

I am trying to use the Date.parse method in Ruby but it is returning odd results when I try to parse early historical dates. For example:

Date.parse("16 January 27 BC")
=> #<Date: -0026-01-16 >

This returns the correct year (although it is -26 instead of -27 but that is normal I think).

But when I try:

Date.parse("19 august 1 BC")
=> #<Date: 2000-08-19 >

Here it assumes the year 2000 as a start date. Same thing for later years:

Date.parse("19 August 14")
=> #<Date: 2014-08-19 >

yearninetynine = Date.parse("19 august AD 99")
=> #<Date: 1999-08-19 >

(I have also tried to replace AD with "CE" but that makes no difference)

The year 100 is where it starts to work properly again:

Date.parse("19 august AD 100")
=> #<Date: 0100-08-19 >

To summarize: up to the year 100 it doesn't parse properly and treats it as years occurring in the 1990s and 2000s, negative years are one less than you would intuitively expect, negative years close to 0 are also not parsed properly.

I tried a few other methods like start=Date::ITALY, but these seem to make no difference. Is there a way to make it parse these years properly?

CodePudding user response:

According to the docs that is exactly what the 2nd argument is for.

Date.parse("19 august 99") #=> Thu, 19 Aug 1999
Date.parse("19 august 99", false) #=> Mon, 19 Aug 0099
  • Related