From fd8e3725f8140944743f76b5a9f9929277eddf6a Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Wed, 29 Oct 2025 18:12:32 +0900 Subject: [PATCH 1/2] Prefer `method_defined?` over `allocate.respond_to?` --- test/date/test_date_conv.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/date/test_date_conv.rb b/test/date/test_date_conv.rb index 772fbee..c54a118 100644 --- a/test/date/test_date_conv.rb +++ b/test/date/test_date_conv.rb @@ -82,7 +82,7 @@ def test_to_time__from_datetime assert_equal([1582, 10, 13, 1, 2, 3, 456789], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.usec]) - if Time.allocate.respond_to?(:nsec) + if Time.method_defined?(:nsec) d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789123.to_r/86400000000000 t = d.to_time.utc assert_equal([2004, 9, 19, 1, 2, 3, 456789123], @@ -91,7 +91,7 @@ def test_to_time__from_datetime # TruffleRuby does not support more than nanoseconds unless RUBY_ENGINE == 'truffleruby' - if Time.allocate.respond_to?(:subsec) + if Time.method_defined?(:subsec) d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789123456789123.to_r/86400000000000000000000 t = d.to_time.utc assert_equal([2004, 9, 19, 1, 2, 3, Rational(456789123456789123,1000000000000000000)], From 2c310d9f5c68a63c09df56556818d8e02541b5c0 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Wed, 29 Oct 2025 18:40:25 +0900 Subject: [PATCH 2/2] Remove archaic conditions `Time#nsec` and `Time#subsec` were both introduced in Ruby 1.9. --- test/date/test_date_conv.rb | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/test/date/test_date_conv.rb b/test/date/test_date_conv.rb index c54a118..8d81084 100644 --- a/test/date/test_date_conv.rb +++ b/test/date/test_date_conv.rb @@ -82,21 +82,17 @@ def test_to_time__from_datetime assert_equal([1582, 10, 13, 1, 2, 3, 456789], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.usec]) - if Time.method_defined?(:nsec) - d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789123.to_r/86400000000000 - t = d.to_time.utc - assert_equal([2004, 9, 19, 1, 2, 3, 456789123], - [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.nsec]) - end + d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789123.to_r/86400000000000 + t = d.to_time.utc + assert_equal([2004, 9, 19, 1, 2, 3, 456789123], + [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.nsec]) # TruffleRuby does not support more than nanoseconds unless RUBY_ENGINE == 'truffleruby' - if Time.method_defined?(:subsec) - d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789123456789123.to_r/86400000000000000000000 - t = d.to_time.utc - assert_equal([2004, 9, 19, 1, 2, 3, Rational(456789123456789123,1000000000000000000)], - [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.subsec]) - end + d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789123456789123.to_r/86400000000000000000000 + t = d.to_time.utc + assert_equal([2004, 9, 19, 1, 2, 3, Rational(456789123456789123,1000000000000000000)], + [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.subsec]) end end