Project

General

Profile

Bug #12297

Ruby stdlib date can parse non-existent date with year 0

Added by globi (t b) over 1 year ago. Updated about 1 year ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:75007]

Description

Ruby date lib can parse date with year 0
$ pry
[1] pry(main)> shitdate=Date.strptime('0000-01-07','%Y-%m-%d')
=> #<Date: 0000-01-07 ((1721064j,0s,0n),+0s,2299161j)>
[2] pry(main)> shitdate.year
=> 0
[3] pry(main)>

There is no year 0 in gregorian and julian calendar between 1 BC and 1 AD
It should raise ArgumentError like it do when month/day number is 0.

no_year_0.patch (1.62 KB) no_year_0.patch noahgibbs (Noah Gibbs), 06/03/2016 05:01 PM

History

#1 [ruby-core:75834] Updated by noahgibbs (Noah Gibbs) about 1 year ago

Ruby has a lot of different time-parsing and date-parsing code. But for Date's strptime specifically, this is an easy fix. Patch is attached.

#2 [ruby-core:75866] Updated by noahgibbs (Noah Gibbs) about 1 year ago

Ah! It would be hard to "fix" Ruby's stdlib to not allow dates to have a 0 year in general, because of code like the Date test that can use a Date as a delta. That is:

class DateSub < Date; end

d2 = d - 1
assert_instance_of(DateSub, d2)

Things like DateSub can clearly have a 0 year, because that implies that the difference in years is 0, not that it's the year 0.

I think it still makes sense to fix strptime with the fix above. But I don't think Date or DateTime should disallow the year 0 in general.

#3 [ruby-core:76422] Updated by matz (Yukihiro Matsumoto) about 1 year ago

According to ruby-dev:10241, it's intentional.
In Gregorian calendar, the year 0 does not exist, but in astronomical year numbering, the year 0 does exist.

https://en.wikipedia.org/wiki/0_(year)

Matz.

Also available in: Atom PDF