[ruby/date] [Bug #21437] Date#hash for large years

Addresses https://bugs.ruby-lang.org/issues/21437

Signed-off-by: Dmitry Dygalo <dmitry.dygalo@workato.com>

31f07bc576
This commit is contained in:
Dmitry Dygalo 2025-06-15 09:11:01 -07:00 committed by git
parent 9a840fd2d4
commit c1877d431e
2 changed files with 20 additions and 5 deletions

View file

@ -6936,13 +6936,24 @@ d_lite_eql_p(VALUE self, VALUE other)
static VALUE static VALUE
d_lite_hash(VALUE self) d_lite_hash(VALUE self)
{ {
st_index_t v, h[4]; st_index_t v, h[5];
VALUE nth;
get_d1(self); get_d1(self);
h[0] = m_nth(dat); nth = m_nth(dat);
h[1] = m_jd(dat);
h[2] = m_df(dat); if (FIXNUM_P(nth)) {
h[3] = m_sf(dat); h[0] = 0;
h[1] = (st_index_t)nth;
} else {
h[0] = 1;
h[1] = (st_index_t)FIX2LONG(rb_hash(nth));
}
h[2] = m_jd(dat);
h[3] = m_df(dat);
h[4] = m_sf(dat);
v = rb_memhash(h, sizeof(h)); v = rb_memhash(h, sizeof(h));
return ST2FIX(v); return ST2FIX(v);
} }

View file

@ -134,6 +134,10 @@ class TestDate < Test::Unit::TestCase
assert_equal(9, h[Date.new(1999,5,25)]) assert_equal(9, h[Date.new(1999,5,25)])
assert_equal(9, h[DateTime.new(1999,5,25)]) assert_equal(9, h[DateTime.new(1999,5,25)])
h = {}
h[Date.new(3171505571716611468830131104691,2,19)] = 0
assert_equal(true, h.key?(Date.new(3171505571716611468830131104691,2,19)))
h = {} h = {}
h[DateTime.new(1999,5,23)] = 0 h[DateTime.new(1999,5,23)] = 0
h[DateTime.new(1999,5,24)] = 1 h[DateTime.new(1999,5,24)] = 1