From eda7e5156edc91fd4cbee0b81ab40006d97f89b1 Mon Sep 17 00:00:00 2001 From: "William T. Nelson" <35801+wtn@users.noreply.github.com> Date: Sat, 28 Nov 2026 16:21:00 -0600 Subject: [PATCH] Fix DateTime#hash for a Rational second fraction d_lite_hash used the address of a Rational second fraction as hash input, so two eql? DateTimes with a second fraction such as 1/3 second had different hashes. d_lite_hash now calls rb_hash on the Rational. Co-authored-by: Claude Mythos 5.2 --- ext/date/date_core.c | 14 +++++++++++--- test/date/test_date.rb | 4 ++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 7353dc8..a1c0bda 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -7191,8 +7191,8 @@ d_lite_eql_p(VALUE self, VALUE other) static VALUE d_lite_hash(VALUE self) { - st_index_t v, h[5]; - VALUE nth; + st_index_t v, h[6]; + VALUE nth, sf; get_d1(self); nth = m_nth(dat); @@ -7207,7 +7207,15 @@ d_lite_hash(VALUE self) h[2] = m_jd(dat); h[3] = m_df(dat); - h[4] = m_sf(dat); + sf = m_sf(dat); + + if (FIXNUM_P(sf)) { + h[4] = 0; + h[5] = (st_index_t)sf; + } else { + h[4] = 1; + h[5] = (st_index_t)FIX2LONG(rb_hash(sf)); + } v = rb_memhash(h, sizeof(h)); return ST2FIX(v); diff --git a/test/date/test_date.rb b/test/date/test_date.rb index 10823e0..08784a2 100644 --- a/test/date/test_date.rb +++ b/test/date/test_date.rb @@ -144,6 +144,10 @@ def test_hash h[Date.new(3171505571716611468830131104691,2,19)] = 0 assert_equal(true, h.key?(Date.new(3171505571716611468830131104691,2,19))) + h = {} + h[DateTime.new(1999,5,25,0,0,Rational(1,3))] = 0 + assert_equal(true, h.key?(DateTime.new(1999,5,25,0,0,Rational(1,3)))) + h = {} h[DateTime.new(1999,5,23)] = 0 h[DateTime.new(1999,5,24)] = 1