Skip to content

Commit 31f07bc

Browse files
authored
[Bug #21437] Date#hash for large years
Addresses https://bugs.ruby-lang.org/issues/21437 Signed-off-by: Dmitry Dygalo <dmitry.dygalo@workato.com>
1 parent dbf4e95 commit 31f07bc

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

ext/date/date_core.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6936,13 +6936,24 @@ d_lite_eql_p(VALUE self, VALUE other)
69366936
static VALUE
69376937
d_lite_hash(VALUE self)
69386938
{
6939-
st_index_t v, h[4];
6939+
st_index_t v, h[5];
6940+
VALUE nth;
69406941

69416942
get_d1(self);
6942-
h[0] = m_nth(dat);
6943-
h[1] = m_jd(dat);
6944-
h[2] = m_df(dat);
6945-
h[3] = m_sf(dat);
6943+
nth = m_nth(dat);
6944+
6945+
if (FIXNUM_P(nth)) {
6946+
h[0] = 0;
6947+
h[1] = (st_index_t)nth;
6948+
} else {
6949+
h[0] = 1;
6950+
h[1] = (st_index_t)FIX2LONG(rb_hash(nth));
6951+
}
6952+
6953+
h[2] = m_jd(dat);
6954+
h[3] = m_df(dat);
6955+
h[4] = m_sf(dat);
6956+
69466957
v = rb_memhash(h, sizeof(h));
69476958
return ST2FIX(v);
69486959
}

test/date/test_date.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ def test_hash
134134
assert_equal(9, h[Date.new(1999,5,25)])
135135
assert_equal(9, h[DateTime.new(1999,5,25)])
136136

137+
h = {}
138+
h[Date.new(3171505571716611468830131104691,2,19)] = 0
139+
assert_equal(true, h.key?(Date.new(3171505571716611468830131104691,2,19)))
140+
137141
h = {}
138142
h[DateTime.new(1999,5,23)] = 0
139143
h[DateTime.new(1999,5,24)] = 1

0 commit comments

Comments
 (0)