[前][次][番号順一覧][スレッド一覧]

ruby-changes:14485

From: akr <ko1@a...>
Date: Thu, 14 Jan 2010 08:52:48 +0900 (JST)
Subject: [ruby-changes:14485] Ruby:r26322 (trunk): * time.c (time_to_r): convert to rational if internal representation

akr	2010-01-14 08:51:16 +0900 (Thu, 14 Jan 2010)

  New Revision: 26322

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=26322

  Log:
    * time.c (time_to_r): convert to rational if internal representation
      is not rational.

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_time.rb
    trunk/time.c

Index: time.c
===================================================================
--- time.c	(revision 26321)
+++ time.c	(revision 26322)
@@ -2425,9 +2425,14 @@
 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;
 }
 
 /*
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 26321)
+++ ChangeLog	(revision 26322)
@@ -1,3 +1,8 @@
+Thu Jan 14 08:49:59 2010  Tanaka Akira  <akr@f...>
+
+	* time.c (time_to_r): convert to rational if internal representation
+	  is not rational.
+
 Thu Jan 14 04:01:50 2010  Tanaka Akira  <akr@f...>
 
 	* time.c (time_mdump): use nano_num and nano_den instead of subnano to
Index: test/ruby/test_time.rb
===================================================================
--- test/ruby/test_time.rb	(revision 26321)
+++ test/ruby/test_time.rb	(revision 26322)
@@ -540,4 +540,9 @@
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]