From 5aea2ad18c622cfa5bb53f1d94e058ac7fc41ad3 Mon Sep 17 00:00:00 2001 From: tadf Date: Tue, 20 Dec 2011 12:03:42 +0000 Subject: * ext/date/date_core.c: uses to_integer instead. * test/date/test_switch_hitter.rb: added a test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34082 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++ ext/date/date_core.c | 30 +++++++++--------- test/date/test_date_base.rb | 7 ----- test/date/test_switch_hitter.rb | 69 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index fbefe7a993..666f8edc2b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Dec 20 21:00:30 2011 Tadayoshi Funaba + + * 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 * Make sure to clear $! when ignoring an exception diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 49e125c3dd..2c72143168 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -1325,8 +1325,10 @@ encode_year(VALUE nth, int y, double style, 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 @@ wholenum_p(VALUE x) } 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 @@ d_trunc(VALUE d, VALUE *fr) 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 @@ h_trunc(VALUE h, VALUE *fr) 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 @@ min_trunc(VALUE min, VALUE *fr) 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 @@ s_trunc(VALUE s, VALUE *fr) 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)); diff --git a/test/date/test_date_base.rb b/test/date/test_date_base.rb index a3d14a763a..1f3d8c0a55 100644 --- a/test/date/test_date_base.rb +++ b/test/date/test_date_base.rb @@ -181,13 +181,6 @@ class TestDateBase < Test::Unit::TestCase 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 diff --git a/test/date/test_switch_hitter.rb b/test/date/test_switch_hitter.rb index 989d939bb2..8431d40a29 100644 --- a/test/date/test_switch_hitter.rb +++ b/test/date/test_switch_hitter.rb @@ -180,6 +180,75 @@ class TestSH < Test::Unit::TestCase [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], -- cgit v1.2.3