mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
15e6d26049
commit
5aea2ad18c
4 changed files with 90 additions and 21 deletions
|
@ -1,3 +1,8 @@
|
|||
Tue Dec 20 21:00:30 2011 Tadayoshi Funaba <tadf@dotrb.org>
|
||||
|
||||
* 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@ruby-lang.org>
|
||||
|
||||
* Make sure to clear $! when ignoring an exception
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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],
|
||||
|
|
Loading…
Add table
Reference in a new issue