From 4bf4ddb693b885a5ce9b9ad983c4743627ae29cc Mon Sep 17 00:00:00 2001 From: akr Date: Wed, 13 Jan 2010 23:51:16 +0000 Subject: * time.c (time_to_r): convert to rational if internal representation is not rational. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26322 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ test/ruby/test_time.rb | 5 +++++ time.c | 7 ++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 33e1c062dd..9b9e186a09 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Thu Jan 14 08:49:59 2010 Tanaka Akira + + * time.c (time_to_r): convert to rational if internal representation + is not rational. + Thu Jan 14 04:01:50 2010 Tanaka Akira * time.c (time_mdump): use nano_num and nano_den instead of subnano to diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb index e6d5d31d80..39a8e297bb 100644 --- a/test/ruby/test_time.rb +++ b/test/ruby/test_time.rb @@ -540,4 +540,9 @@ class TestTime < Test::Unit::TestCase assert_equal(-1, d1 <=> d2) assert_equal(1, d2 <=> d1) end + + def test_to_r + assert_kind_of(Rational, Time.new(2000,1,1,0,0,Rational(4,3)).to_r) + assert_kind_of(Rational, Time.utc(1970).to_r) + end end diff --git a/time.c b/time.c index 70f0bb65ae..2d5edbe338 100644 --- a/time.c +++ b/time.c @@ -2425,9 +2425,14 @@ static VALUE time_to_r(VALUE time) { struct time_object *tobj; + VALUE v; GetTimeval(time, tobj); - return rb_time_unmagnify(tobj->timexv); + v = rb_time_unmagnify(tobj->timexv); + if (TYPE(v) != T_RATIONAL) { + v = rb_convert_type(v, T_RATIONAL, "Rational", "to_r"); + } + return v; } /* -- cgit v1.2.3