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

ruby-changes:22033

From: tadf <ko1@a...>
Date: Tue, 20 Dec 2011 21:03:55 +0900 (JST)
Subject: [ruby-changes:22033] tadf:r34082 (trunk): * ext/date/date_core.c: uses to_integer instead.

tadf	2011-12-20 21:03:42 +0900 (Tue, 20 Dec 2011)

  New Revision: 34082

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

  Log:
    * ext/date/date_core.c: uses to_integer instead.
    * test/date/test_switch_hitter.rb: added a test.

  Modified files:
    trunk/ChangeLog
    trunk/ext/date/date_core.c
    trunk/test/date/test_date_base.rb
    trunk/test/date/test_switch_hitter.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 34081)
+++ ChangeLog	(revision 34082)
@@ -1,3 +1,8 @@
+Tue Dec 20 21:00:30 2011  Tadayoshi Funaba  <tadf@d...>
+
+	* ext/date/date_core.c: uses to_integer instead.
+	* test/date/test_switch_hitter.rb: added a test.
+
 Tue Dec 20 15:04:18 2011  Hiroshi Nakamura  <nahi@r...>
 
 	* Make sure to clear $! when ignoring an exception
Index: ext/date/date_core.c
===================================================================
--- ext/date/date_core.c	(revision 34081)
+++ ext/date/date_core.c	(revision 34082)
@@ -1325,8 +1325,10 @@
 static void
 decode_jd(VALUE jd, VALUE *nth, int *rjd)
 {
+    assert(FIXNUM_P(jd) || RB_TYPE_P(jd, T_BIGNUM));
     *nth = f_idiv(jd, INT2FIX(CM_PERIOD));
     if (f_zero_p(*nth)) {
+	assert(FIXNUM_P(jd));
 	*rjd = FIX2INT(jd);
 	return;
     }
@@ -3096,14 +3098,10 @@
 }
 
 inline static VALUE
-wholenum(VALUE x)
+to_integer(VALUE x)
 {
-    if (FIXNUM_P(x))
+    if (FIXNUM_P(x) || RB_TYPE_P(x, T_BIGNUM))
 	return x;
-    switch (TYPE(x)) {
-      case T_BIGNUM:
-	return x;
-    }
     return f_to_i(x);
 }
 
@@ -3113,9 +3111,10 @@
     VALUE rd;
 
     if (wholenum_p(d)) {
-	rd = wholenum(d);
+	rd = to_integer(d);
 	*fr = INT2FIX(0);
-    } else {
+    }
+    else {
 	rd = f_idiv(d, INT2FIX(1));
 	*fr = f_mod(d, INT2FIX(1));
     }
@@ -3131,9 +3130,10 @@
     VALUE rh;
 
     if (wholenum_p(h)) {
-	rh = wholenum(h);
+	rh = to_integer(h);
 	*fr = INT2FIX(0);
-    } else {
+    }
+    else {
 	rh = f_idiv(h, INT2FIX(1));
 	*fr = f_mod(h, INT2FIX(1));
 	*fr = f_quo(*fr, INT2FIX(24));
@@ -3147,9 +3147,10 @@
     VALUE rmin;
 
     if (wholenum_p(min)) {
-	rmin = wholenum(min);
+	rmin = to_integer(min);
 	*fr = INT2FIX(0);
-    } else {
+    }
+    else {
 	rmin = f_idiv(min, INT2FIX(1));
 	*fr = f_mod(min, INT2FIX(1));
 	*fr = f_quo(*fr, INT2FIX(1440));
@@ -3163,9 +3164,10 @@
     VALUE rs;
 
     if (wholenum_p(s)) {
-	rs = wholenum(s);
+	rs = to_integer(s);
 	*fr = INT2FIX(0);
-    } else {
+    }
+    else {
 	rs = f_idiv(s, INT2FIX(1));
 	*fr = f_mod(s, INT2FIX(1));
 	*fr = f_quo(*fr, INT2FIX(86400));
Index: test/date/test_switch_hitter.rb
===================================================================
--- test/date/test_switch_hitter.rb	(revision 34081)
+++ test/date/test_switch_hitter.rb	(revision 34082)
@@ -180,6 +180,75 @@
 		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
   end
 
+  def test_fractional
+    d = Date.jd(2451944.0)
+    assert_equal(2451944, d.jd)
+    d = Date.jd(Rational(2451944))
+    assert_equal(2451944, d.jd)
+    d = Date.jd(2451944.5)
+    assert_equal([2451944, 12], [d.jd, d.send('hour')])
+    d = Date.jd(Rational('2451944.5'))
+    assert_equal([2451944, 12], [d.jd, d.send('hour')])
+
+    d = Date.civil(2001, 2, 3.0)
+    assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
+    d = Date.civil(2001, 2, Rational(3))
+    assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
+    d = Date.civil(2001, 2, 3.5)
+    assert_equal([2001, 2, 3, 12], [d.year, d.mon, d.mday, d.send('hour')])
+    d = Date.civil(2001, 2, Rational('3.5'))
+    assert_equal([2001, 2, 3, 12], [d.year, d.mon, d.mday, d.send('hour')])
+
+    d = Date.ordinal(2001, 2.0)
+    assert_equal([2001, 2], [d.year, d.yday])
+    d = Date.ordinal(2001, Rational(2))
+    assert_equal([2001, 2], [d.year, d.yday])
+
+    d = Date.commercial(2001, 2, 3.0)
+    assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
+    d = Date.commercial(2001, 2, Rational(3))
+    assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
+
+    d = DateTime.jd(2451944.0)
+    assert_equal(2451944, d.jd)
+    d = DateTime.jd(Rational(2451944))
+    assert_equal(2451944, d.jd)
+    d = DateTime.jd(2451944.5)
+    assert_equal([2451944, 12], [d.jd, d.hour])
+    d = DateTime.jd(Rational('2451944.5'))
+    assert_equal([2451944, 12], [d.jd, d.hour])
+
+    d = DateTime.civil(2001, 2, 3.0)
+    assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
+    d = DateTime.civil(2001, 2, Rational(3))
+    assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
+    d = DateTime.civil(2001, 2, 3.5)
+    assert_equal([2001, 2, 3, 12], [d.year, d.mon, d.mday, d.hour])
+    d = DateTime.civil(2001, 2, Rational('3.5'))
+    assert_equal([2001, 2, 3, 12], [d.year, d.mon, d.mday, d.hour])
+    d = DateTime.civil(2001, 2, 3, 4.5)
+    assert_equal([2001, 2, 3, 4, 30], [d.year, d.mon, d.mday, d.hour, d.min])
+    d = DateTime.civil(2001, 2, 3, Rational('4.5'))
+    assert_equal([2001, 2, 3, 4, 30], [d.year, d.mon, d.mday, d.hour, d.min])
+    d = DateTime.civil(2001, 2, 3, 4, 5.5)
+    assert_equal([2001, 2, 3, 4, 5, 30],
+		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
+    d = DateTime.civil(2001, 2, 3, 4, Rational('5.5'))
+    assert_equal([2001, 2, 3, 4, 5, 30],
+		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
+
+    d = DateTime.ordinal(2001, 2.0)
+    assert_equal([2001, 2], [d.year, d.yday])
+    d = DateTime.ordinal(2001, Rational(2))
+    assert_equal([2001, 2], [d.year, d.yday])
+
+    d = DateTime.commercial(2001, 2, 3.0)
+    assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
+    d = DateTime.commercial(2001, 2, Rational(3))
+    assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
+
+  end
+
   def test_canon24oc
     d = DateTime.jd(2451943,24)
     assert_equal([2001, 2, 3, 0, 0, 0, 0],
Index: test/date/test_date_base.rb
===================================================================
--- test/date/test_date_base.rb	(revision 34081)
+++ test/date/test_date_base.rb	(revision 34082)
@@ -181,13 +181,6 @@
 
   def test_jd
     assert_equal(1 << 33, Date.jd(1 << 33).jd)
-
-    bug = '[ruby-dev:45008]'
-    d = Date.new(2011,12,20)
-    jd = d.jd
-    assert_equal(d, Date.jd(jd))
-    assert_equal(d, Date.jd(jd.to_f), bug)
-    assert_equal(d, Date.jd(Rational(jd)), bug)
   end
 
   def test_mjd

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

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