From 9fe4a35e67e46b84049e6c3951f4720d550322cf Mon Sep 17 00:00:00 2001 From: tadf Date: Tue, 26 Apr 2011 16:02:25 +0000 Subject: [PATCH] * ext/date/date_core.c: modified validation methods. * ext/date/lib/date.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31358 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 + ext/date/date_core.c | 302 +++++++++++++++++++++++++----------- ext/date/lib/date.rb | 116 +++++--------- test/date/test_date_attr.rb | 1 + test/date/test_date_base.rb | 49 +++--- test/date/test_date_new.rb | 2 + 6 files changed, 283 insertions(+), 192 deletions(-) diff --git a/ChangeLog b/ChangeLog index a5678af5c3..609bc7b0e3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,6 +17,11 @@ Wed Apr 27 00:51:01 2011 Nobuyoshi Nakada * include/ruby/win32.h (ftruncate, truncate, ftello, fseeko): non-64 versions on mingw are useless because they use int32_t. fixes #4564 +Wed Apr 27 00:50:33 2011 Tadayoshi Funaba + + * ext/date/date_core.c: modified validation methods. + * ext/date/lib/date.rb: ditto. + Wed Apr 27 00:00:37 2011 Tadayoshi Funaba * ext/date/date_core.c (dt_lite_set_tmx): should get df value. diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 96e8c5455a..6c435a869f 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -36,6 +36,9 @@ #define LIGHTABLE_CWYEAR(y) LIGHTABLE_YEAR(y) #define ITALY 2299161 +#define ENGLAND 2361222 +#define JULIAN (NUM2DBL(rb_const_get(rb_cFloat, rb_intern("INFINITY")))) +#define GREGORIAN (-NUM2DBL(rb_const_get(rb_cFloat, rb_intern("INFINITY")))) #define DAY_IN_SECONDS 86400 #define SECOND_IN_NANOSECONDS 1000000000 @@ -785,151 +788,263 @@ k_numeric_p(VALUE x) } static VALUE -date_s_valid_jd_p(int argc, VALUE *argv, VALUE klass) +valid_jd_sub(int argc, VALUE *argv, VALUE klass) +{ + return argv[0]; +} + +#ifndef NDEBUG +static VALUE +date_s__valid_jd_p(int argc, VALUE *argv, VALUE klass) { VALUE vjd, vsg; + VALUE argv2[2]; rb_scan_args(argc, argv, "11", &vjd, &vsg); + argv2[0] = vjd; + if (argc < 2) + argv[1] = DBL2NUM(GREGORIAN); + else + argv[1] = vsg; + + return valid_jd_sub(2, argv2, klass); +} +#endif + +static VALUE +date_s_valid_jd_p(int argc, VALUE *argv, VALUE klass) +{ + VALUE vjd, vsg; + VALUE argv2[2]; + + rb_scan_args(argc, argv, "11", &vjd, &vsg); + + argv2[0] = vjd; + if (argc < 2) + argv[1] = INT2FIX(ITALY); + else + argv[1] = vsg; + + if (NIL_P(valid_jd_sub(2, argv2, klass))) + return Qfalse; return Qtrue; } static VALUE -date_s_valid_civil_p(int argc, VALUE *argv, VALUE klass) +valid_civil_sub(int argc, VALUE *argv, VALUE klass, int need_jd) { - VALUE vy, vm, vd, vsg; int y, m, d, rm, rd; double sg; - rb_scan_args(argc, argv, "31", &vy, &vm, &vd, &vsg); + if (!(FIXNUM_P(argv[0]) && + FIXNUM_P(argv[1]) && + FIXNUM_P(argv[2]))) + return cforwardv("_valid_civil_r?"); - if (!(FIXNUM_P(vy) && - FIXNUM_P(vm) && - FIXNUM_P(vd))) - return cforwardv("valid_civil_r?"); + y = NUM2INT(argv[0]); + if (!LIGHTABLE_YEAR(y)) + return cforwardv("_valid_civil_r?"); - if (!NIL_P(vsg)) - sg = NUM2DBL(vsg); - else - sg = ITALY; + m = NUM2INT(argv[1]); + d = NUM2INT(argv[2]); + sg = NUM2DBL(argv[3]); - y = -4712; - m = 1; - d = 1; - - switch (argc) { - case 4: - case 3: - d = NUM2INT(vd); - case 2: - m = NUM2INT(vm); - case 1: - y = NUM2INT(vy); - if (!LIGHTABLE_YEAR(y)) - return cforwardv("valid_civil_r?"); - } - - if (isinf(sg) && sg < 0) { + if (!need_jd && isinf(sg) && sg < 0) { if (!valid_gregorian_p(y, m, d, &rm, &rd)) - return Qfalse; - return Qtrue; + return Qnil; + return INT2FIX(0); /* dummy */ } else { long jd; int ns; if (!valid_civil_p(y, m, d, sg, &rm, &rd, &jd, &ns)) - return Qfalse; - return Qtrue; + return Qnil; + return LONG2NUM(jd); } } +#ifndef NDEBUG static VALUE -date_s_valid_ordinal_p(int argc, VALUE *argv, VALUE klass) +date_s__valid_civil_p(int argc, VALUE *argv, VALUE klass) +{ + VALUE vy, vm, vd, vsg; + VALUE argv2[4]; + + rb_scan_args(argc, argv, "31", &vy, &vm, &vd, &vsg); + + argv2[0] = vy; + argv2[1] = vm; + argv2[2] = vd; + if (argc < 4) + argv2[3] = DBL2NUM(GREGORIAN); + else + argv2[3] = vsg; + + return valid_civil_sub(4, argv2, klass, 1); +} +#endif + +static VALUE +date_s_valid_civil_p(int argc, VALUE *argv, VALUE klass) +{ + VALUE vy, vm, vd, vsg; + VALUE argv2[4]; + + rb_scan_args(argc, argv, "31", &vy, &vm, &vd, &vsg); + + argv2[0] = vy; + argv2[1] = vm; + argv2[2] = vd; + if (argc < 4) + argv2[3] = INT2FIX(ITALY); + else + argv2[3] = vsg; + + if (NIL_P(valid_civil_sub(4, argv2, klass, 0))) + return Qfalse; + return Qtrue; +} + +static VALUE +valid_ordinal_sub(int argc, VALUE *argv, VALUE klass) { - VALUE vy, vd, vsg; int y, d, rd; double sg; - rb_scan_args(argc, argv, "21", &vy, &vd, &vsg); + if (!(FIXNUM_P(argv[0]) && + FIXNUM_P(argv[1]))) + return cforwardv("_valid_ordinal_r?"); - if (!(FIXNUM_P(vy) && - FIXNUM_P(vd))) - return cforwardv("valid_ordinal_r?"); + y = NUM2INT(argv[0]); + if (!LIGHTABLE_YEAR(y)) + return cforwardv("_valid_ordinal_r?"); - if (!NIL_P(vsg)) - sg = NUM2DBL(vsg); - else - sg = ITALY; - - y = -4712; - d = 1; - - switch (argc) { - case 3: - case 2: - d = NUM2INT(vd); - case 1: - y = NUM2INT(vy); - if (!LIGHTABLE_YEAR(y)) - return cforwardv("valid_ordinal_r?"); - } + d = NUM2INT(argv[1]); + sg = NUM2DBL(argv[2]); { long jd; int ns; if (!valid_ordinal_p(y, d, sg, &rd, &jd, &ns)) - return Qfalse; - return Qtrue; + return Qnil; + return LONG2NUM(jd); } } +#ifndef NDEBUG static VALUE -date_s_valid_commercial_p(int argc, VALUE *argv, VALUE klass) +date_s__valid_ordinal_p(int argc, VALUE *argv, VALUE klass) +{ + VALUE vy, vd, vsg; + VALUE argv2[3]; + + rb_scan_args(argc, argv, "21", &vy, &vd, &vsg); + + argv2[0] = vy; + argv2[1] = vd; + if (argc < 3) + argv2[2] = DBL2NUM(GREGORIAN); + else + argv2[2] = vsg; + + return valid_ordinal_sub(3, argv2, klass); +} +#endif + +static VALUE +date_s_valid_ordinal_p(int argc, VALUE *argv, VALUE klass) +{ + VALUE vy, vd, vsg; + VALUE argv2[3]; + + rb_scan_args(argc, argv, "21", &vy, &vd, &vsg); + + argv2[0] = vy; + argv2[1] = vd; + if (argc < 3) + argv2[2] = INT2FIX(ITALY); + else + argv2[2] = vsg; + + if (NIL_P(valid_ordinal_sub(3, argv2, klass))) + return Qfalse; + return Qtrue; +} + +static VALUE +valid_commercial_sub(int argc, VALUE *argv, VALUE klass) { - VALUE vy, vw, vd, vsg; int y, w, d, rw, rd; double sg; - rb_scan_args(argc, argv, "31", &vy, &vw, &vd, &vsg); + if (!(FIXNUM_P(argv[0]) && + FIXNUM_P(argv[1]) && + FIXNUM_P(argv[2]))) + return cforwardv("_valid_commercial_r?"); - if (!(FIXNUM_P(vy) && - FIXNUM_P(vw) && - FIXNUM_P(vd))) - return cforwardv("valid_commercial_r?"); + y = NUM2INT(argv[0]); + if (!LIGHTABLE_CWYEAR(y)) + return cforwardv("_valid_commercial_r?"); - if (!NIL_P(vsg)) - sg = NUM2DBL(vsg); - else - sg = ITALY; - - y = -4712; - w = 1; - d = 1; - - switch (argc) { - case 4: - case 3: - d = NUM2INT(vd); - case 2: - w = NUM2INT(vw); - case 1: - y = NUM2INT(vy); - if (!LIGHTABLE_CWYEAR(y)) - return cforwardv("valid_commercial_r?"); - } + w = NUM2INT(argv[1]); + d = NUM2INT(argv[2]); + sg = NUM2DBL(argv[3]); { long jd; int ns; if (!valid_commercial_p(y, w, d, sg, &rw, &rd, &jd, &ns)) - return Qfalse; - return Qtrue; + return Qnil; + return LONG2NUM(jd); } } +#ifndef NDEBUG +static VALUE +date_s__valid_commercial_p(int argc, VALUE *argv, VALUE klass) +{ + VALUE vy, vw, vd, vsg; + VALUE argv2[4]; + + rb_scan_args(argc, argv, "31", &vy, &vw, &vd, &vsg); + + argv2[0] = vy; + argv2[1] = vw; + argv2[2] = vd; + if (argc < 4) + argv2[3] = DBL2NUM(GREGORIAN); + else + argv2[3] = vsg; + + return valid_commercial_sub(4, argv2, klass); +} +#endif + +static VALUE +date_s_valid_commercial_p(int argc, VALUE *argv, VALUE klass) +{ + VALUE vy, vw, vd, vsg; + VALUE argv2[4]; + + rb_scan_args(argc, argv, "31", &vy, &vw, &vd, &vsg); + + argv2[0] = vy; + argv2[1] = vw; + argv2[2] = vd; + if (argc < 4) + argv2[3] = INT2FIX(ITALY); + else + argv2[3] = vsg; + + if (NIL_P(valid_commercial_sub(4, argv2, klass))) + return Qfalse; + return Qtrue; +} + static void d_right_gc_mark(union DateData *dat) { @@ -4482,7 +4597,18 @@ Init_date_core(void) rb_define_alloc_func(cDate, d_lite_s_alloc); rb_define_singleton_method(cDate, "new_r!", date_s_new_r_bang, -1); rb_define_singleton_method(cDate, "new_l!", date_s_new_l_bang, -1); - +#ifndef NDEBUG + rb_define_private_method(CLASS_OF(cDate), "_valid_jd?", + date_s__valid_jd_p, -1); + rb_define_private_method(CLASS_OF(cDate), "_valid_ordinal?", + date_s__valid_ordinal_p, -1); + rb_define_private_method(CLASS_OF(cDate), "_valid_civil?", + date_s__valid_civil_p, -1); + rb_define_private_method(CLASS_OF(cDate), "_valid_date?", + date_s__valid_civil_p, -1); + rb_define_private_method(CLASS_OF(cDate), "_valid_commercial?", + date_s__valid_commercial_p, -1); +#endif rb_define_singleton_method(cDate, "valid_jd?", date_s_valid_jd_p, -1); rb_define_singleton_method(cDate, "valid_ordinal?", date_s_valid_ordinal_p, -1); diff --git a/ext/date/lib/date.rb b/ext/date/lib/date.rb index ed24532d51..c6448017f7 100644 --- a/ext/date/lib/date.rb +++ b/ext/date/lib/date.rb @@ -325,7 +325,7 @@ class Date def find_fdoy(y, sg) # :nodoc: j = nil 1.upto(31) do |d| - break if j = _valid_civil?(y, 1, d, sg) + break if j = _valid_civil_r?(y, 1, d, sg) end j end @@ -333,7 +333,7 @@ class Date def find_ldoy(y, sg) # :nodoc: j = nil 31.downto(1) do |d| - break if j = _valid_civil?(y, 12, d, sg) + break if j = _valid_civil_r?(y, 12, d, sg) end j end @@ -341,7 +341,7 @@ class Date def find_fdom(y, m, sg) # :nodoc: j = nil 1.upto(31) do |d| - break if j = _valid_civil?(y, m, d, sg) + break if j = _valid_civil_r?(y, m, d, sg) end j end @@ -349,7 +349,7 @@ class Date def find_ldom(y, m, sg) # :nodoc: j = nil 31.downto(1) do |d| - break if j = _valid_civil?(y, m, d, sg) + break if j = _valid_civil_r?(y, m, d, sg) end j end @@ -576,7 +576,7 @@ class Date # # If it is, returns it. In fact, any value is treated as a valid # Julian Day Number. - def _valid_jd? (jd, sg=GREGORIAN) jd end # :nodoc: + def _valid_jd_r? (jd, sg=GREGORIAN) jd end # :nodoc: # Do the year +y+ and day-of-year +d+ make a valid Ordinal Date? # Returns the corresponding Julian Day Number if they do, or @@ -591,7 +591,7 @@ class Date # adjustment is not valid. # # +sg+ specifies the Day of Calendar Reform. - def _valid_ordinal? (y, d, sg=GREGORIAN) # :nodoc: + def _valid_ordinal_r? (y, d, sg=GREGORIAN) # :nodoc: if d < 0 return unless j = find_ldoy(y, sg) ny, nd = jd_to_ordinal(j + d + 1, sg) @@ -615,7 +615,7 @@ class Date # Reform adjustment is not valid. # # +sg+ specifies the Day of Calendar Reform. - def _valid_civil? (y, m, d, sg=GREGORIAN) # :nodoc: + def _valid_civil_r? (y, m, d, sg=GREGORIAN) # :nodoc: if m < 0 m += 13 end @@ -644,7 +644,7 @@ class Date # Reform adjustment is not valid. # # +sg+ specifies the Day of Calendar Reform. - def _valid_commercial? (y, w, d, sg=GREGORIAN) # :nodoc: + def _valid_commercial_r? (y, w, d, sg=GREGORIAN) # :nodoc: if d < 0 d += 8 end @@ -659,7 +659,7 @@ class Date jd end - def _valid_weeknum? (y, w, d, f, sg=GREGORIAN) # :nodoc: + def _valid_weeknum_r? (y, w, d, f, sg=GREGORIAN) # :nodoc: if d < 0 d += 7 end @@ -674,7 +674,7 @@ class Date jd end - def _valid_nth_kday? (y, m, n, k, sg=GREGORIAN) # :nodoc: + def _valid_nth_kday_r? (y, m, n, k, sg=GREGORIAN) # :nodoc: if k < 0 k += 7 end @@ -700,7 +700,7 @@ class Date # +sec+ are treating as counting backwards from the end of the # next larger unit (e.g. a +min+ of -2 is treated as 58). No # wraparound is performed. - def _valid_time? (h, min, s) # :nodoc: + def _valid_time_r? (h, min, s) # :nodoc: h += 24 if h < 0 min += 60 if min < 0 s += 60 if s < 0 @@ -731,48 +731,6 @@ class Date class << self; alias_method :leap?, :gregorian_leap? end - def self.valid_jd_r? (jd, sg=ITALY) - !!_valid_jd?(jd, sg) - end - - private_class_method :valid_jd_r? - - def self.valid_ordinal_r? (y, d, sg=ITALY) - !!_valid_ordinal?(y, d, sg) - end - - private_class_method :valid_ordinal_r? - - def self.valid_civil_r? (y, m, d, sg=ITALY) - !!_valid_civil?(y, m, d, sg) - end - - private_class_method :valid_civil_r? - - def self.valid_commercial_r? (y, w, d, sg=ITALY) - !!_valid_commercial?(y, w, d, sg) - end - - private_class_method :valid_commercial_r? - - def self.valid_weeknum? (y, w, d, f, sg=ITALY) # :nodoc: - !!_valid_weeknum?(y, w, d, f, sg) - end - - private_class_method :valid_weeknum? - - def self.valid_nth_kday? (y, m, n, k, sg=ITALY) # :nodoc: - !!_valid_nth_kday?(y, m, n, k, sg) - end - - private_class_method :valid_nth_kday? - - def self.valid_time? (h, min, s) # :nodoc: - !!_valid_time?(h, min, s) - end - - private_class_method :valid_time? - def self.new!(ajd=0, of=0, sg=ITALY) jd, df = ajd_to_jd(ajd, 0) if !(Fixnum === jd) || @@ -784,14 +742,14 @@ class Date end def self.jd_r(jd=0, sg=ITALY) # :nodoc: - jd = _valid_jd?(jd, sg) + jd = _valid_jd_r?(jd, sg) new_r!(jd_to_ajd(jd, 0, 0), 0, sg) end private_class_method :jd_r def self.ordinal_r(y=-4712, d=1, sg=ITALY) # :nodoc: - unless jd = _valid_ordinal?(y, d, sg) + unless jd = _valid_ordinal_r?(y, d, sg) raise ArgumentError, 'invalid date' end new_r!(jd_to_ajd(jd, 0, 0), 0, sg) @@ -800,7 +758,7 @@ class Date private_class_method :ordinal_r def self.civil_r(y=-4712, m=1, d=1, sg=ITALY) # :nodoc: - unless jd = _valid_civil?(y, m, d, sg) + unless jd = _valid_civil_r?(y, m, d, sg) raise ArgumentError, 'invalid date' end new_r!(jd_to_ajd(jd, 0, 0), 0, sg) @@ -809,7 +767,7 @@ class Date private_class_method :civil_r def self.commercial_r(y=-4712, w=1, d=1, sg=ITALY) # :nodoc: - unless jd = _valid_commercial?(y, w, d, sg) + unless jd = _valid_commercial_r?(y, w, d, sg) raise ArgumentError, 'invalid date' end new_r!(jd_to_ajd(jd, 0, 0), 0, sg) @@ -818,7 +776,7 @@ class Date private_class_method :commercial_r def self.weeknum(y=-4712, w=0, d=1, f=0, sg=ITALY) - unless jd = _valid_weeknum?(y, w, d, f, sg) + unless jd = _valid_weeknum_r?(y, w, d, f, sg) raise ArgumentError, 'invalid date' end new!(jd_to_ajd(jd, 0, 0), 0, sg) @@ -827,7 +785,7 @@ class Date private_class_method :weeknum def self.nth_kday(y=-4712, m=1, n=1, k=1, sg=ITALY) - unless jd = _valid_nth_kday?(y, m, n, k, sg) + unless jd = _valid_nth_kday_r?(y, m, n, k, sg) raise ArgumentError, 'invalid date' end new!(jd_to_ajd(jd, 0, 0), 0, sg) @@ -935,21 +893,21 @@ class Date catch :jd do a = elem.values_at(:jd) if a.all? - if jd = _valid_jd?(*(a << sg)) + if jd = _valid_jd_r?(*(a << sg)) throw :jd, jd end end a = elem.values_at(:year, :yday) if a.all? - if jd = _valid_ordinal?(*(a << sg)) + if jd = _valid_ordinal_r?(*(a << sg)) throw :jd, jd end end a = elem.values_at(:year, :mon, :mday) if a.all? - if jd = _valid_civil?(*(a << sg)) + if jd = _valid_civil_r?(*(a << sg)) throw :jd, jd end end @@ -959,7 +917,7 @@ class Date a[2] = elem[:wday].nonzero? || 7 end if a.all? - if jd = _valid_commercial?(*(a << sg)) + if jd = _valid_commercial_r?(*(a << sg)) throw :jd, jd end end @@ -969,7 +927,7 @@ class Date a[2] = elem[:cwday] % 7 end if a.all? - if jd = _valid_weeknum?(*(a << 0 << sg)) + if jd = _valid_weeknum_r?(*(a << 0 << sg)) throw :jd, jd end end @@ -982,7 +940,7 @@ class Date a[2] = (elem[:cwday] - 1) % 7 end if a.all? - if jd = _valid_weeknum?(*(a << 1 << sg)) + if jd = _valid_weeknum_r?(*(a << 1 << sg)) throw :jd, jd end end @@ -993,7 +951,7 @@ class Date def self.valid_time_frags? (elem) # :nodoc: h, min, s = elem.values_at(:hour, :min, :sec) - _valid_time?(h, min, s) + _valid_time_r?(h, min, s) end private_class_method :valid_time_frags? @@ -1323,7 +1281,7 @@ class Date y, m = (year * 12 + (mon - 1) + n).divmod(12) m, = (m + 1) .divmod(1) d = mday - until jd2 = _valid_civil?(y, m, d, start) + until jd2 = _valid_civil_r?(y, m, d, start) d -= 1 raise ArgumentError, 'invalid date' unless d > 0 end @@ -1464,8 +1422,8 @@ class DateTime < Date end def self.jd_r(jd=0, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc: - unless (jd = _valid_jd?(jd, sg)) && - (fr = _valid_time?(h, min, s)) + unless (jd = _valid_jd_r?(jd, sg)) && + (fr = _valid_time_r?(h, min, s)) raise ArgumentError, 'invalid date' end if String === of @@ -1479,8 +1437,8 @@ class DateTime < Date private_class_method :jd_r def self.ordinal_r(y=-4712, d=1, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc: - unless (jd = _valid_ordinal?(y, d, sg)) && - (fr = _valid_time?(h, min, s)) + unless (jd = _valid_ordinal_r?(y, d, sg)) && + (fr = _valid_time_r?(h, min, s)) raise ArgumentError, 'invalid date' end if String === of @@ -1494,8 +1452,8 @@ class DateTime < Date private_class_method :ordinal_r def self.civil_r(y=-4712, m=1, d=1, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc: - unless (jd = _valid_civil?(y, m, d, sg)) && - (fr = _valid_time?(h, min, s)) + unless (jd = _valid_civil_r?(y, m, d, sg)) && + (fr = _valid_time_r?(h, min, s)) raise ArgumentError, 'invalid date' end if String === of @@ -1509,8 +1467,8 @@ class DateTime < Date private_class_method :civil_r def self.commercial_r(y=-4712, w=1, d=1, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc: - unless (jd = _valid_commercial?(y, w, d, sg)) && - (fr = _valid_time?(h, min, s)) + unless (jd = _valid_commercial_r?(y, w, d, sg)) && + (fr = _valid_time_r?(h, min, s)) raise ArgumentError, 'invalid date' end if String === of @@ -1524,8 +1482,8 @@ class DateTime < Date private_class_method :commercial_r def self.weeknum(y=-4712, w=0, d=1, f=0, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc: - unless (jd = _valid_weeknum?(y, w, d, f, sg)) && - (fr = _valid_time?(h, min, s)) + unless (jd = _valid_weeknum_r?(y, w, d, f, sg)) && + (fr = _valid_time_r?(h, min, s)) raise ArgumentError, 'invalid date' end if String === of @@ -1539,8 +1497,8 @@ class DateTime < Date private_class_method :weeknum def self.nth_kday(y=-4712, m=1, n=1, k=1, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc: - unless (jd = _valid_nth_kday?(y, m, n, k, sg)) && - (fr = _valid_time?(h, min, s)) + unless (jd = _valid_nth_kday_r?(y, m, n, k, sg)) && + (fr = _valid_time_r?(h, min, s)) raise ArgumentError, 'invalid date' end if String === of diff --git a/test/date/test_date_attr.rb b/test/date/test_date_attr.rb index 9224dc1340..88a54d7461 100644 --- a/test/date/test_date_attr.rb +++ b/test/date/test_date_attr.rb @@ -96,6 +96,7 @@ class TestDateAttr < Test::Unit::TestCase end def test_nth_kday + skip unless Date.new.respond_to?(:nth_kday?, true) assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, 1,0)) assert_equal(true, Date.new(2001,1,14).__send__(:nth_kday?, 2,0)) assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, 3,0)) diff --git a/test/date/test_date_base.rb b/test/date/test_date_base.rb index c4639633be..9f76bac8a3 100644 --- a/test/date/test_date_base.rb +++ b/test/date/test_date_base.rb @@ -53,9 +53,7 @@ class TestDateBase < Test::Unit::TestCase end def test_ordinal__julian - unless defined?(Calendar) - return - end + skip unless defined?(Calendar) for j in @from..@to m, d, y = julian_from_absolute(absolute_from_julian_day_number(j)) j0 = julian_day_number_from_absolute(absolute_from_julian(12, 31, y - 1)) @@ -70,9 +68,7 @@ class TestDateBase < Test::Unit::TestCase end def test_ordinal__gregorian - unless defined?(Calendar) - return - end + skip unless defined?(Calendar) for j in @from..@to m, d, y = gregorian_from_absolute(absolute_from_julian_day_number(j)) j0 = @@ -88,9 +84,7 @@ class TestDateBase < Test::Unit::TestCase end def test_civil__julian - unless defined?(Calendar) - return - end + skip unless defined?(Calendar) for j in @from..@to m, d, y = julian_from_absolute(absolute_from_julian_day_number(j)) j2 = julian_day_number_from_absolute(absolute_from_julian(m, d, y)) @@ -105,9 +99,7 @@ class TestDateBase < Test::Unit::TestCase end def test_civil__gregorian - unless defined?(Calendar) - return - end + skip unless defined?(Calendar) for j in @from..@to m, d, y = gregorian_from_absolute(absolute_from_julian_day_number(j)) j2 = julian_day_number_from_absolute(absolute_from_gregorian(m, d, y)) @@ -122,9 +114,7 @@ class TestDateBase < Test::Unit::TestCase end def test_commercial__gregorian - unless defined?(Calendar) - return - end + skip unless defined?(Calendar) for j in @from..@to w, d, y = iso_from_absolute(absolute_from_julian_day_number(j)) j2 = julian_day_number_from_absolute(absolute_from_iso(w, d, y)) @@ -139,9 +129,7 @@ class TestDateBase < Test::Unit::TestCase end def test_weeknum - unless defined?(Calendar) - return - end + skip unless defined?(Calendar) for j in @from..@to for k in 0..1 wy, ww, wd = Date.__send__(:jd_to_weeknum, j, k, Date::GREGORIAN) @@ -152,9 +140,7 @@ class TestDateBase < Test::Unit::TestCase end def test_weeknum__2 - unless defined?(Calendar) - return - end + skip unless defined?(Calendar) for j in @from4t..@to4t d = Date.jd(j) t = Time.mktime(d.year, d.mon, d.mday) @@ -172,9 +158,9 @@ class TestDateBase < Test::Unit::TestCase end def test_nth_kday - unless defined?(Calendar) - return - end + skip unless defined?(Calendar) + skip unless (Date.respond_to?(:nth_kday_to_jd, true) && + Date.respond_to?(:jd_to_nth_kday, true)) for y in 1601..2401 for m in 1..12 for n in -5..5 @@ -194,7 +180,7 @@ class TestDateBase < Test::Unit::TestCase end def test_jd - assert_equal(1<<33, Date.jd(1<<33).jd) + assert_equal(1 << 33, Date.jd(1 << 33).jd) end def test_mjd @@ -235,6 +221,7 @@ class TestDateBase < Test::Unit::TestCase def test_valid_jd valid_jd_p = :_valid_jd? + skip unless Date.respond_to?(valid_jd_p, true) assert_equal(-1, Date.__send__(valid_jd_p, -1)) assert_equal(0, Date.__send__(valid_jd_p, 0)) assert_equal(1, Date.__send__(valid_jd_p, 1)) @@ -243,6 +230,7 @@ class TestDateBase < Test::Unit::TestCase def test_valid_ordinal valid_ordinal_p = :_valid_ordinal? + skip unless Date.respond_to?(valid_ordinal_p, true) assert_nil(Date.__send__(valid_ordinal_p, 1999,366)) assert_equal(2451910, Date.__send__(valid_ordinal_p, 2000,366)) assert_nil(Date.__send__(valid_ordinal_p, 1999,-366)) @@ -257,6 +245,7 @@ class TestDateBase < Test::Unit::TestCase def test_valid_ordinal__edge valid_ordinal_p = :_valid_ordinal? + skip unless Date.respond_to?(valid_ordinal_p, true) (1601..2400).each do |y| d = if Date.leap?(y) then 366 else 365 end assert_not_nil(Date.__send__(valid_ordinal_p, y,d)) @@ -282,6 +271,7 @@ class TestDateBase < Test::Unit::TestCase def test_valid_ordinal__italy valid_ordinal_p = :_valid_ordinal? + skip unless Date.respond_to?(valid_ordinal_p, true) (1..355).each do |d| assert_not_nil(Date.__send__(valid_ordinal_p, 1582,d,Date::ITALY)) end @@ -298,6 +288,7 @@ class TestDateBase < Test::Unit::TestCase def test_valid_ordinal__england valid_ordinal_p = :_valid_ordinal? + skip unless Date.respond_to?(valid_ordinal_p, true) (1..355).each do |d| assert_not_nil(Date.__send__(valid_ordinal_p, 1752,d,Date::ENGLAND)) end @@ -308,6 +299,7 @@ class TestDateBase < Test::Unit::TestCase def test_valid_civil valid_civil_p = :_valid_civil? + skip unless Date.respond_to?(valid_civil_p, true) assert_nil(Date.__send__(valid_civil_p, 1999,2,29)) assert_equal(2451604, Date.__send__(valid_civil_p, 2000,2,29)) assert_nil(Date.__send__(valid_civil_p, 1999,2,-29)) @@ -323,6 +315,7 @@ class TestDateBase < Test::Unit::TestCase def test_valid_civil__edge valid_civil_p = :_valid_civil? + skip unless Date.respond_to?(valid_civil_p, true) (1601..2400).each do |y| d = if Date.leap?(y) then 29 else 28 end assert_not_nil(Date.__send__(valid_civil_p, y,2,d)) @@ -341,6 +334,7 @@ class TestDateBase < Test::Unit::TestCase def test_valid_civil__italy valid_civil_p = :_valid_civil? + skip unless Date.respond_to?(valid_civil_p, true) (1..4).each do |d| assert_not_nil(Date.__send__(valid_civil_p, 1582,10,d,Date::ITALY)) end @@ -369,6 +363,7 @@ class TestDateBase < Test::Unit::TestCase def test_valid_civil__england valid_civil_p = :_valid_civil? + skip unless Date.respond_to?(valid_civil_p, true) (1..2).each do |d| assert_not_nil(Date.__send__(valid_civil_p, 1752,9,d,Date::ENGLAND)) end @@ -391,6 +386,7 @@ class TestDateBase < Test::Unit::TestCase def test_valid_commercial valid_commercial_p = :_valid_commercial? + skip unless Date.respond_to?(valid_commercial_p, true) assert_nil(Date.__send__(valid_commercial_p, 1999,53,1)) assert_equal(2453367, Date.__send__(valid_commercial_p, 2004,53,1)) assert_nil(Date.__send__(valid_commercial_p, 1999,-53,-1)) @@ -400,6 +396,7 @@ class TestDateBase < Test::Unit::TestCase def test_valid_weeknum valid_weeknum_p = :_valid_weeknum? + skip unless Date.respond_to?(valid_weeknum_p, true) assert_nil(Date.__send__(valid_weeknum_p, 1999,53,0, 0)) assert_equal(2454101, Date.__send__(valid_weeknum_p, 2006,53,0, 0)) assert_nil(Date.__send__(valid_weeknum_p, 1999,-53,-1, 0)) @@ -414,6 +411,7 @@ class TestDateBase < Test::Unit::TestCase def test_valid_nth_kday valid_nth_kday_p = :_valid_nth_kday? + skip unless Date.respond_to?(valid_nth_kday_p, true) assert_nil(Date.__send__(valid_nth_kday_p, 1992,2, 5,0)) assert_equal(2448682, Date.__send__(valid_nth_kday_p, 1992,2, 5,6)) assert_equal(2448682, Date.__send__(valid_nth_kday_p, 1992,2, 5,-1)) @@ -423,6 +421,7 @@ class TestDateBase < Test::Unit::TestCase def test_valid_time valid_time_p = :_valid_time? + skip unless Date.respond_to?(valid_time_p, true) assert_equal(Rational(0), DateTime.__send__(valid_time_p, 0,0,0)) assert_nil(DateTime.__send__(valid_time_p, 25,59,59)) assert_nil(DateTime.__send__(valid_time_p, 23,60,59)) diff --git a/test/date/test_date_new.rb b/test/date/test_date_new.rb index 14ff247294..da63c2248e 100644 --- a/test/date/test_date_new.rb +++ b/test/date/test_date_new.rb @@ -211,6 +211,7 @@ class TestDateNew < Test::Unit::TestCase end def test_weeknum + skip unless Date.respond_to?(:weeknum, true) d = Date.__send__(:weeknum) dt = DateTime.__send__(:weeknum) assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday]) @@ -233,6 +234,7 @@ class TestDateNew < Test::Unit::TestCase end def test_nth_kday + skip unless Date.respond_to?(:nth_kday, true) d = Date.__send__(:nth_kday) dt = DateTime.__send__(:nth_kday) assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])