diff --git a/ChangeLog b/ChangeLog index 27050cf5d5..1f7ada7176 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Sun Sep 28 19:04:03 2008 Tadayoshi Funaba + + * test/date/*.rb: imported a date test suite ruby 1.9 limited ed. + Sun Sep 28 18:40:49 2008 Nobuyoshi Nakada * lib/mini/test.rb (MINI_DIR): loaded path is expanded. diff --git a/test/date/test_date.rb b/test/date/test_date.rb new file mode 100644 index 0000000000..3ae22633ec --- /dev/null +++ b/test/date/test_date.rb @@ -0,0 +1,138 @@ +require 'test/unit' +require 'date' + +class DateSub < Date; end +class DateTimeSub < DateTime; end + +class TestDate < Test::Unit::TestCase + + def test__const + assert_nil(Date::MONTHNAMES[0]) + assert_equal('January', Date::MONTHNAMES[1]) + assert_equal(13, Date::MONTHNAMES.size) + assert_equal('Sunday', Date::DAYNAMES[0]) + assert_equal(7, Date::DAYNAMES.size) + + assert_nil(Date::ABBR_MONTHNAMES[0]) + assert_equal('Jan', Date::ABBR_MONTHNAMES[1]) + assert_equal(13, Date::ABBR_MONTHNAMES.size) + assert_equal('Sun', Date::ABBR_DAYNAMES[0]) + assert_equal(7, Date::ABBR_DAYNAMES.size) + + assert(Date::MONTHNAMES.frozen?) + assert(!Date::MONTHNAMES[0].frozen?) + assert(Date::MONTHNAMES[1].frozen?) + assert(Date::DAYNAMES.frozen?) + assert(Date::DAYNAMES[0].frozen?) + + assert(Date::ABBR_MONTHNAMES.frozen?) + assert(Date::ABBR_MONTHNAMES[1].frozen?) + assert(Date::ABBR_DAYNAMES.frozen?) + assert(Date::ABBR_DAYNAMES[0].frozen?) + end + + def test_sub + d = DateSub.new + dt = DateTimeSub.new + + assert_instance_of(DateSub, d) + assert_equal('#', d.inspect) + assert_equal('-4712-01-01', d.to_s) + assert_equal('-4712-01-01T00:00:00+00:00', dt.to_s) + + d2 = d + 1 + assert_instance_of(DateSub, d2) + d2 = d - 1 + assert_instance_of(DateSub, d2) + d2 = d >> 1 + assert_instance_of(DateSub, d2) + d2 = d << 1 + assert_instance_of(DateSub, d2) + d2 = d.succ + assert_instance_of(DateSub, d2) + d2 = d.next + assert_instance_of(DateSub, d2) + d2 = d.italy + assert_instance_of(DateSub, d2) + d2 = d.england + assert_instance_of(DateSub, d2) + d2 = d.julian + assert_instance_of(DateSub, d2) + d2 = d.gregorian + assert_instance_of(DateSub, d2) + s = Marshal.dump(d) + d2 = Marshal.load(s) + assert_equal(d2, d) + assert_instance_of(DateSub, d2) + + dt2 = dt + 1 + assert_instance_of(DateTimeSub, dt2) + dt2 = dt - 1 + assert_instance_of(DateTimeSub, dt2) + dt2 = dt >> 1 + assert_instance_of(DateTimeSub, dt2) + dt2 = dt << 1 + assert_instance_of(DateTimeSub, dt2) + dt2 = dt.succ + assert_instance_of(DateTimeSub, dt2) + dt2 = dt.next + assert_instance_of(DateTimeSub, dt2) + dt2 = dt.italy + assert_instance_of(DateTimeSub, dt2) + dt2 = dt.england + assert_instance_of(DateTimeSub, dt2) + dt2 = dt.julian + assert_instance_of(DateTimeSub, dt2) + dt2 = dt.gregorian + assert_instance_of(DateTimeSub, dt2) + s = Marshal.dump(dt) + dt2 = Marshal.load(s) + assert_equal(dt2, dt) + assert_instance_of(DateTimeSub, dt2) + end + + def test_eql_p + d = Date.jd(0) + d2 = Date.jd(0) + dt = DateTime.jd(0) + dt2 = DateTime.jd(0) + + assert_equal(d, d2) + assert_not_equal(d, 0) + + assert_equal(dt, dt2) + assert_not_equal(dt, 0) + + assert_equal(d, dt) + assert_equal(d2, dt2) + end + + def test_hash + h = {} + h[Date.new(1999,5,23)] = 0 + h[Date.new(1999,5,24)] = 1 + h[Date.new(1999,5,25)] = 2 + h[Date.new(1999,5,25)] = 9 + assert_equal(3, h.size) + assert_equal(9, h[Date.new(1999,5,25)]) + assert_equal(9, h[DateTime.new(1999,5,25)]) + + h = {} + h[DateTime.new(1999,5,23)] = 0 + h[DateTime.new(1999,5,24)] = 1 + h[DateTime.new(1999,5,25)] = 2 + h[DateTime.new(1999,5,25)] = 9 + assert_equal(3, h.size) + assert_equal(9, h[Date.new(1999,5,25)]) + assert_equal(9, h[DateTime.new(1999,5,25)]) + end + + def test_freeze + d = Date.new + d.freeze + assert_equal(true, d.frozen?) + assert_instance_of(Fixnum, d.yday) + assert_instance_of(String, d.to_s) + end + +end diff --git a/test/date/test_date_arith.rb b/test/date/test_date_arith.rb new file mode 100644 index 0000000000..745ddbd16c --- /dev/null +++ b/test/date/test_date_arith.rb @@ -0,0 +1,286 @@ +require 'test/unit' +require 'date' + +class TestDateArith < Test::Unit::TestCase + + def new_offset + d = DateTime.new(2002, 3, 14) + assert_equal(Rational(9, 24), d.new_offset(Rational(9, 24)).offset) + assert_equal(Rational(9, 24), d.new_offset('+0900').offset) + end + + def test__plus + d = Date.new(2000,2,29) + -1 + assert_equal([2000, 2, 28], [d.year, d.mon, d.mday]) + d = Date.new(2000,2,29) + 0 + assert_equal([2000, 2, 29], [d.year, d.mon, d.mday]) + d = Date.new(2000,2,29) + 1 + assert_equal([2000, 3, 1], [d.year, d.mon, d.mday]) + + d = DateTime.new(2000,2,29) + 1.to_r/2 + assert_equal([2000, 2, 29, 12, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + end + + def test__plus__ex + e = TypeError + assert_raise(e) do + Date.new(2000,2,29) + 'foo' + end + assert_raise(e) do + DateTime.new(2000,2,29) + 'foo' + end + assert_raise(e) do + Date.new(2000,2,29) + Time.mktime(2000,2,29) + end + assert_raise(e) do + DateTime.new(2000,2,29) + Time.mktime(2000,2,29) + end + end + + def test__minus + d = Date.new(2000,3,1) - -1 + assert_equal([2000, 3, 2], [d.year, d.mon, d.mday]) + d = Date.new(2000,3,1) - 0 + assert_equal([2000, 3, 1], [d.year, d.mon, d.mday]) + d = Date.new(2000,3,1) - 1 + assert_equal([2000, 2, 29], [d.year, d.mon, d.mday]) + + d = Date.new(2000,3,1) - Date.new(2000,2,29) + assert_equal(1, d) + d = Date.new(2000,2,29) - Date.new(2000,3,1) + assert_equal(-1, d) + + d = DateTime.new(2000,3,1) - 1.to_r/2 + assert_equal([2000, 2, 29, 12, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + end + + def test__minus__ex + e = TypeError + assert_raise(e) do + Date.new(2000,2,29) - 'foo' + end + assert_raise(e) do + DateTime.new(2000,2,29) - 'foo' + end + assert_raise(e) do + Date.new(2000,2,29) - Time.mktime(2000,2,29) + end + assert_raise(e) do + DateTime.new(2000,2,29) - Time.mktime(2000,2,29) + end + end + + def test__compare + assert_equal(0, (Date.new(2000,1,1) <=> Date.new(2000,1,1))) + assert_equal(-1, (Date.new(2000,1,1) <=> Date.new(2000,1,2))) + assert_equal(1, (Date.new(2000,1,2) <=> Date.new(2000,1,1))) + assert_equal(0, (Date.new(2001,1,4,Date::JULIAN) <=> + Date.new(2001,1,17, Date::GREGORIAN))) + assert_equal(0, (DateTime.new(2001,1,4,0,0,0,0,Date::JULIAN) <=> + DateTime.new(2001,1,17,0,0,0,0,Date::GREGORIAN))) + end + + def test_prev + d = Date.new(2000,1,1) + assert_raise(NoMethodError) do + d.prev + end + end + + def test_prev_day + d = Date.new(2001,1,1).prev_day + assert_equal([2000, 12, 31], [d.year, d.mon, d.mday]) + d = Date.new(2001,1,1).prev_day(2) + assert_equal([2000, 12, 30], [d.year, d.mon, d.mday]) + d = Date.new(2000,12,31).prev_day(-2) + assert_equal([2001, 1, 2], [d.year, d.mon, d.mday]) + + d = DateTime.new(2000,3,1).prev_day(1.to_r/2) + assert_equal([2000, 2, 29, 12, 0, 0], [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + end + + def test_prev_month + d = Date.new(2000,1,31) << -1 + assert_equal([2000, 2, 29], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31) << 1 + assert_equal([1999, 12, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31) << 12 + assert_equal([1999, 1, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31) << 14 + assert_equal([1998, 11, 30], [d.year, d.mon, d.mday]) + + end + + def test_prev_month__2 + d = Date.new(2000,1,31).prev_month(-1) + assert_equal([2000, 2, 29], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31).prev_month + assert_equal([1999, 12, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31).prev_month(12) + assert_equal([1999, 1, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31).prev_month(14) + assert_equal([1998, 11, 30], [d.year, d.mon, d.mday]) + end + + def test_prev_year + d = Date.new(2000,1,31).prev_year(-1) + assert_equal([2001, 1, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31).prev_year + assert_equal([1999, 1, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31).prev_year(10) + assert_equal([1990, 1, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31).prev_year(100) + assert_equal([1900, 1, 31], [d.year, d.mon, d.mday]) + end + + def test_next + d = Date.new(2000,12,31).next + assert_equal([2001, 1, 1], [d.year, d.mon, d.mday]) + d = Date.new(2000,12,31).succ + assert_equal([2001, 1, 1], [d.year, d.mon, d.mday]) + + d = Date.today + d2 = d.next + assert_equal(d, (d2 - 1)) + d = Date.today + d2 = d.succ + assert_equal(d, (d2 - 1)) + + d = DateTime.now + d2 = d.next + assert_equal(d, (d2 - 1)) + d = DateTime.now + d2 = d.succ + assert_equal(d, (d2 - 1)) + end + + def test_next_day + d = Date.new(2000,12,31).next_day + assert_equal([2001, 1, 1], [d.year, d.mon, d.mday]) + d = Date.new(2000,12,31).next_day(2) + assert_equal([2001, 1, 2], [d.year, d.mon, d.mday]) + d = Date.new(2001,1,1).next_day(-2) + assert_equal([2000, 12, 30], [d.year, d.mon, d.mday]) + + d = DateTime.new(2000,2,29).next_day(1.to_r/2) + assert_equal([2000, 2, 29, 12, 0, 0], [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + end + + def test_next_month + d = Date.new(2000,1,31) >> -1 + assert_equal([1999, 12, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31) >> 1 + assert_equal([2000, 2, 29], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31) >> 12 + assert_equal([2001, 1, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31) >> 13 + assert_equal([2001, 2, 28], [d.year, d.mon, d.mday]) + end + + def test_next_month__2 + d = Date.new(2000,1,31).next_month(-1) + assert_equal([1999, 12, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31).next_month + assert_equal([2000, 2, 29], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31).next_month(12) + assert_equal([2001, 1, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31).next_month(13) + assert_equal([2001, 2, 28], [d.year, d.mon, d.mday]) + end + + def test_next_year + d = Date.new(2000,1,31).next_year(-1) + assert_equal([1999, 1, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31).next_year + assert_equal([2001, 1, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31).next_year(10) + assert_equal([2010, 1, 31], [d.year, d.mon, d.mday]) + d = Date.new(2000,1,31).next_year(100) + assert_equal([2100, 1, 31], [d.year, d.mon, d.mday]) + end + + def test_downto + p = Date.new(2001,1,14) + q = Date.new(2001,1,7) + i = 0 + p.downto(q) do |d| + i += 1 + end + assert_equal(8, i) + end + + def test_downto__noblock + p = Date.new(2001,1,14) + q = Date.new(2001,1,7) + e = p.downto(q) + assert_equal(8, e.to_a.size) + end + + def test_upto + p = Date.new(2001,1,14) + q = Date.new(2001,1,21) + i = 0 + p.upto(q) do |d| + i += 1 + end + assert_equal(8, i) + end + + def test_upto__noblock + p = Date.new(2001,1,14) + q = Date.new(2001,1,21) + e = p.upto(q) + assert_equal(8, e.to_a.size) + end + + def test_step + p = Date.new(2001,1,14) + q = Date.new(2001,1,21) + i = 0 + p.step(q, 2) do |d| + i += 1 + end + assert_equal(4, i) + + i = 0 + p.step(q) do |d| + i += 1 + end + assert_equal(8, i) + end + + def test_step__noblock + p = Date.new(2001,1,14) + q = Date.new(2001,1,21) + e = p.step(q, 2) + assert_equal(4, e.to_a.size) + + e = p.step(q) + assert_equal(8, e.to_a.size) + end + +=begin + def test_step__inf + p = Date.new(2001,1,14) + q = Date.new(2001,1,21) + inf = 1.0/0 + + if inf.infinite? + [p, q].each do |a| + [p, q].each do |b| + [inf, -inf].each do |c| + i = 0 + a.step(b, c) do |d| + i += 1 + end + assert_equal(0, i) + end + end + end + end + end +=end + +end diff --git a/test/date/test_date_attr.rb b/test/date/test_date_attr.rb new file mode 100644 index 0000000000..2e71b5f501 --- /dev/null +++ b/test/date/test_date_attr.rb @@ -0,0 +1,109 @@ +require 'test/unit' +require 'date' + +class TestDateAttr < Test::Unit::TestCase + + def test__attr + date = Date.new(1965, 5, 23) + datetime = DateTime.new(1965, 5, 23, 22, 31, 59) + + [date, datetime].each_with_index do |d, i| + if i == 0 + assert_match(/\#/, + d.inspect) + else + assert_match(/\#/, + d.inspect) + end + + if i == 0 + assert_equal('1965-05-23', d.to_s) + else + assert_equal('1965-05-23T22:31:59+00:00', d.to_s) + end + + assert_equal('', d.inspect.gsub!(/./,'')) + assert_equal('', d.to_s.gsub!(/./,'')) + + assert_equal(2438904, d.jd) + + if i == 0 + assert_equal(0, d.day_fraction) + else + assert_equal(22.to_r/24 + 31.to_r/1440 + 59.to_r/86400, d.day_fraction) + end + + assert_equal(38903, d.mjd) + assert_equal(139744, d.ld) + + assert_equal(1965, d.year) + assert_equal(143, d.yday) + assert_equal(5, d.mon) + assert_equal(d.mon, d.month) + assert_equal(23, d.mday) + assert_equal(d.mday, d.day) + + if i == 0 + assert_equal(false, d.respond_to?(:hour)) + assert_equal(false, d.respond_to?(:min)) + assert_equal(false, d.respond_to?(:sec)) + assert_equal(false, d.respond_to?(:sec_fraction)) + assert_equal(false, d.respond_to?(:zone)) + assert_equal(false, d.respond_to?(:offset)) + else + assert_equal(22, d.hour) + assert_equal(31, d.min) + assert_equal(59, d.sec) + assert_equal(0, d.sec_fraction) + assert_equal('+00:00', d.zone) + assert_equal(0, d.offset) + end + + assert_equal(1965, d.cwyear) + assert_equal(20, d.cweek) + assert_equal(7, d.cwday) + + assert_equal(0, d.wday) + assert_equal(false, d.leap?) + assert_equal(false, d.julian?) + assert_equal(true, d.gregorian?) + + assert_equal(Date::ITALY, d.start) + assert_equal(d.start, d.start) + end + + d = DateTime.new(1965, 5, 23, 22, 31, 59) + 1.to_r/(86400*2) + assert_equal(1.to_r/2, d.sec_fraction) + end + + def test__wday_predicate + d = Date.new(2005, 10, 23) + assert_equal(true, d.sunday?) + assert_equal(false, d.monday?) + assert_equal(false, d.tuesday?) + assert_equal(false, d.wednesday?) + assert_equal(false, d.thursday?) + assert_equal(false, d.friday?) + assert_equal(false, d.saturday?) + + d = Date.new(2005, 10, 30) + 14.times do |i| + assert((d + i).__send__(%w(sunday? monday? tuesday? wednesday? + thursday? friday? saturday?)[i % 7])) + end + end + + def test_nth_kday + 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)) + assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, 4,0)) + assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, 5,0)) + assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, -1,0)) + assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, -2,0)) + assert_equal(true, Date.new(2001,1,14).__send__(:nth_kday?, -3,0)) + assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, -4,0)) + assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, -5,0)) + end + +end diff --git a/test/date/test_date_base.rb b/test/date/test_date_base.rb new file mode 100644 index 0000000000..4b9bcf2aa4 --- /dev/null +++ b/test/date/test_date_base.rb @@ -0,0 +1,435 @@ +require 'test/unit' +require 'date' + +begin + require 'calendar' + include Calendar +rescue LoadError +end + +class TestDateBase < Test::Unit::TestCase + + def setup + if defined?(Calendar) + @from ||= julian_day_number_from_absolute(absolute_from_julian(1, 1, 1601)) + @to ||= julian_day_number_from_absolute(absolute_from_julian(12, 31, 2400)) + @from4t ||= julian_day_number_from_absolute(absolute_from_julian(1, 1, 1970)) + @to4t ||= julian_day_number_from_absolute(absolute_from_julian(12, 31, 2037)) + end + end + + def test__inf + assert_equal(0, Date::Infinity.new(-1) <=> Date::Infinity.new(-1)) + assert_equal(-1, Date::Infinity.new(-1) <=> Date::Infinity.new(+1)) + assert_equal(-1, Date::Infinity.new(-1) <=> 0) + + assert_equal(1, Date::Infinity.new(+1) <=> Date::Infinity.new(-1)) + assert_equal(0, Date::Infinity.new(+1) <=> Date::Infinity.new(+1)) + assert_equal(1, Date::Infinity.new(+1) <=> 0) + + assert_equal(1, 0 <=> Date::Infinity.new(-1)) + assert_equal(-1, 0 <=> Date::Infinity.new(+1)) + assert_equal(0, 0 <=> 0) + + assert_equal(0, Date::ITALY <=> Date::ITALY) + assert_equal(-1, Date::ITALY <=> Date::ENGLAND) + assert_equal(-1, Date::ITALY <=> Date::JULIAN) + assert_equal(1, Date::ITALY <=> Date::GREGORIAN) + + assert_equal(1, Date::ENGLAND <=> Date::ITALY) + assert_equal(0, Date::ENGLAND <=> Date::ENGLAND) + assert_equal(-1, Date::ENGLAND <=> Date::JULIAN) + assert_equal(1, Date::ENGLAND <=> Date::GREGORIAN) + + assert_equal(1, Date::JULIAN <=> Date::ITALY) + assert_equal(1, Date::JULIAN <=> Date::ENGLAND) + assert_equal(0, Date::JULIAN <=> Date::JULIAN) + assert_equal(1, Date::JULIAN <=> Date::GREGORIAN) + + assert_equal(-1, Date::GREGORIAN <=> Date::ITALY) + assert_equal(-1, Date::GREGORIAN <=> Date::ENGLAND) + assert_equal(-1, Date::GREGORIAN <=> Date::JULIAN) + assert_equal(0, Date::GREGORIAN <=> Date::GREGORIAN) + end + + def test_ordinal__julian + unless defined?(Calendar) + return + end + 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)) + j2 = julian_day_number_from_absolute(absolute_from_julian(m, d, y)) + assert_equal(j, j2) + oy, od = Date.__send__(:jd_to_ordinal, j, Date::JULIAN) + assert_equal(y, oy) + assert_equal(j2 - j0, od) + oj = Date.__send__(:ordinal_to_jd, oy, od, Date::JULIAN) + assert_equal(j, oj) + end + end + + def test_ordinal__gregorian + unless defined?(Calendar) + return + end + for j in @from..@to + m, d, y = gregorian_from_absolute(absolute_from_julian_day_number(j)) + j0 = + julian_day_number_from_absolute(absolute_from_gregorian(12, 31, y - 1)) + j2 = julian_day_number_from_absolute(absolute_from_gregorian(m, d, y)) + assert_equal(j, j2) + oy, od = Date.__send__(:jd_to_ordinal, j, Date::GREGORIAN) + assert_equal(y, oy) + assert_equal(j2 - j0, od) + oj = Date.__send__(:ordinal_to_jd, oy, od, Date::GREGORIAN) + assert_equal(j, oj) + end + end + + def test_civil__julian + unless defined?(Calendar) + return + end + 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)) + assert_equal(j2, j) + cy, cm, cd = Date.__send__(:jd_to_civil, j, Date::JULIAN) + assert_equal(y, cy) + assert_equal(m, cm) + assert_equal(d, cd) + cj = Date.__send__(:civil_to_jd, cy, cm, cd, Date::JULIAN) + assert_equal(j, cj) + end + end + + def test_civil__gregorian + unless defined?(Calendar) + return + end + 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)) + assert_equal(j2, j) + cy, cm, cd = Date.__send__(:jd_to_civil, j, Date::GREGORIAN) + assert_equal(y, cy) + assert_equal(m, cm) + assert_equal(d, cd) + cj = Date.__send__(:civil_to_jd, cy, cm, cd, Date::GREGORIAN) + assert_equal(j, cj) + end + end + + def test_commercial__gregorian + unless defined?(Calendar) + return + end + 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)) + assert_equal(j2, j) + cy, cw, cd = Date.__send__(:jd_to_commercial, j, Date::GREGORIAN) + assert_equal(y, cy) + assert_equal(w, cw) + assert_equal(d, cd) + cj = Date.__send__(:commercial_to_jd, cy, cw, cd, Date::GREGORIAN) + assert_equal(j, cj) + end + end + + def test_weeknum + unless defined?(Calendar) + return + end + for j in @from..@to + for k in 0..1 + wy, ww, wd = Date.__send__(:jd_to_weeknum, j, k, Date::GREGORIAN) + wj = Date.__send__(:weeknum_to_jd, wy, ww, wd, k, Date::GREGORIAN) + assert_equal(j, wj) + end + end + end + + def test_weeknum__2 + unless defined?(Calendar) + return + end + for j in @from4t..@to4t + d = Date.jd(j) + t = Time.mktime(d.year, d.mon, d.mday) + [ + '%Y %U %w', + '%Y %U %u', + '%Y %W %w', + '%Y %W %u' + ].each do |fmt| + s = t.strftime(fmt) + d2 = Date.strptime(s, fmt) + assert_equal(j, d2.jd) + end + end + end + + def test_nth_kday + unless defined?(Calendar) + return + end + for y in 1601..2401 + for m in 1..12 + for n in -5..5 + next if n == 0 + for k in 0..6 + j = julian_day_number_from_absolute(Nth_Kday(n, k, m, y)) + j2 = Date.__send__(:nth_kday_to_jd, y, m, n, k, Date::GREGORIAN) + assert_equal(j, j2) + + d1 = Date.__send__(:jd_to_nth_kday, j2, Date::GREGORIAN) + j3 = Date.__send__(:nth_kday_to_jd, *d1) + assert_equal(j, j3) + end + end + end + end + end + + def test_mjd + jd = Date.__send__(:mjd_to_jd, 51321) + mjd = Date.__send__(:jd_to_mjd, jd) + assert_equal(51321, mjd) + end + + def test_ld + jd = Date.__send__(:ld_to_jd, 152162) + ld = Date.__send__(:jd_to_ld, jd) + assert_equal(152162, ld) + end + + def test_wday + assert_equal(4, Date.__send__(:jd_to_wday, 3)) + assert_equal(3, Date.__send__(:jd_to_wday, 2)) + assert_equal(2, Date.__send__(:jd_to_wday, 1)) + assert_equal(1, Date.__send__(:jd_to_wday, 0)) + assert_equal(0, Date.__send__(:jd_to_wday, -1)) + assert_equal(6, Date.__send__(:jd_to_wday, -2)) + assert_equal(5, Date.__send__(:jd_to_wday, -3)) + end + + def test_leap? + assert_equal(true, Date.julian_leap?(1900)) + assert_equal(false, Date.julian_leap?(1999)) + assert_equal(true, Date.julian_leap?(2000)) + + assert_equal(false, Date.gregorian_leap?(1900)) + assert_equal(false, Date.gregorian_leap?(1999)) + assert_equal(true, Date.gregorian_leap?(2000)) + + assert_equal(Date.leap?(1990), Date.gregorian_leap?(1900)) + assert_equal(Date.leap?(1999), Date.gregorian_leap?(1999)) + assert_equal(Date.leap?(2000), Date.gregorian_leap?(2000)) + end + + def test_valid_jd + valid_jd_p = :_valid_jd? + 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)) + assert_equal(2452348, Date.__send__(valid_jd_p, 2452348)) + end + + def test_valid_ordinal + valid_ordinal_p = :_valid_ordinal? + 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)) + assert_equal(2451545, Date.__send__(valid_ordinal_p, 2000,-366)) + assert_equal(2452275, Date.__send__(valid_ordinal_p, 2001,365)) + assert_nil(Date.__send__(valid_ordinal_p, 2001,366)) + assert_equal(Date.__send__(valid_ordinal_p, 2001,1), + Date.__send__(valid_ordinal_p, 2001,-365)) + assert_nil(Date.__send__(valid_ordinal_p, 2001,-366)) + assert_equal(2452348, Date.__send__(valid_ordinal_p, 2002,73)) + end + + def test_valid_ordinal__edge + valid_ordinal_p = :_valid_ordinal? + (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)) + assert_nil(Date.__send__(valid_ordinal_p, y,d + 1)) + assert_not_nil(Date.__send__(valid_ordinal_p, y,-d)) + assert_nil(Date.__send__(valid_ordinal_p, y,-(d + 1))) + end + end + + # October 1582 + # S M Tu W Th F S + # 274 275 276 277 288 289 + # 290 291 292 293 294 295 296 + # 297 298 299 300 301 302 303 + # 304 + + # October 1582 + # S M Tu W Th F S + # -92 -91 -90 -89 -78 -77 + # -76 -75 -74 -73 -72 -71 -70 + # -69 -68 -67 -66 -65 -64 -63 + # -62 + + def test_valid_ordinal__italy + valid_ordinal_p = :_valid_ordinal? + (1..355).each do |d| + assert_not_nil(Date.__send__(valid_ordinal_p, 1582,d,Date::ITALY)) + end + (356..365).each do |d| + assert_nil(Date.__send__(valid_ordinal_p, 1582,d,Date::ITALY)) + end + end + + # September 1752 + # S M Tu W Th F S + # 245 246 258 259 260 + # 261 262 263 264 265 266 267 + # 268 269 270 271 272 273 274 + + def test_valid_ordinal__england + valid_ordinal_p = :_valid_ordinal? + (1..355).each do |d| + assert_not_nil(Date.__send__(valid_ordinal_p, 1752,d,Date::ENGLAND)) + end + (356..366).each do |d| + assert_nil(Date.__send__(valid_ordinal_p, 1752,d,Date::ENGLAND)) + end + end + + def test_valid_civil + valid_civil_p = :_valid_civil? + 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)) + assert_equal(2451576, Date.__send__(valid_civil_p, 2000,2,-29)) + assert_equal(2451941, Date.__send__(valid_civil_p, 2001,1,31)) + assert_nil(Date.__send__(valid_civil_p, 2001,1,32)) + assert_equal(Date.__send__(valid_civil_p, 2001,1,1), + Date.__send__(valid_civil_p, 2001,1,-31)) + assert_nil(Date.__send__(valid_civil_p, 2001,1,-32)) + assert_equal(2452348, Date.__send__(valid_civil_p, 2002,3,14)) + end + + def test_valid_civil__edge + valid_civil_p = :_valid_civil? + (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)) + assert_nil(Date.__send__(valid_civil_p, y,2,d + 1)) + assert_not_nil(Date.__send__(valid_civil_p, y,2,-d)) + assert_nil(Date.__send__(valid_civil_p, y,2,-(d + 1))) + end + end + + # October 1582 + # S M Tu W Th F S + # 1 2 3 4 15 16 + # 17 18 19 20 21 22 23 + # 24 25 26 27 28 29 30 + # 31 + + def test_valid_civil__italy + valid_civil_p = :_valid_civil? + (1..4).each do |d| + assert_not_nil(Date.__send__(valid_civil_p, 1582,10,d,Date::ITALY)) + end + (5..14).each do |d| + assert_nil(Date.__send__(valid_civil_p, 1582,10,d,Date::ITALY)) + end + (15..31).each do |d| + assert_not_nil(Date.__send__(valid_civil_p, 1582,10,d,Date::ITALY)) + end + (32..100).each do |d| + assert_nil(Date.__send__(valid_civil_p, 1582,10,d,Date::ITALY)) + end + (-31..-22).each do |d| + assert_nil(Date.__send__(valid_civil_p, 1582,10,d,Date::ITALY)) + end + (-21..-1).each do |d| + assert_not_nil(Date.__send__(valid_civil_p, 1582,10,d,Date::ITALY)) + end + end + + # September 1752 + # S M Tu W Th F S + # 1 2 14 15 16 + # 17 18 19 20 21 22 23 + # 24 25 26 27 28 29 30 + + def test_valid_civil__england + valid_civil_p = :_valid_civil? + (1..2).each do |d| + assert_not_nil(Date.__send__(valid_civil_p, 1752,9,d,Date::ENGLAND)) + end + (3..13).each do |d| + assert_nil(Date.__send__(valid_civil_p, 1752,9,d,Date::ENGLAND)) + end + (14..30).each do |d| + assert_not_nil(Date.__send__(valid_civil_p, 1752,9,d,Date::ENGLAND)) + end + (31..100).each do |d| + assert_nil(Date.__send__(valid_civil_p, 1752,9,d,Date::ENGLAND)) + end + (-31..-20).each do |d| + assert_nil(Date.__send__(valid_civil_p, 1752,9,d,Date::ENGLAND)) + end + (-19..-1).each do |d| + assert_not_nil(Date.__send__(valid_civil_p, 1752,9,d,Date::ENGLAND)) + end + end + + def test_valid_commercial + valid_commercial_p = :_valid_commercial? + 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)) + assert_equal(2453009, Date.__send__(valid_commercial_p, 2004,-53,-1)) + assert_equal(2452348, Date.__send__(valid_commercial_p, 2002,11,4)) + end + + def test_valid_weeknum + valid_weeknum_p = :_valid_weeknum? + 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)) + assert_equal(2453743, Date.__send__(valid_weeknum_p, 2006,-53,-1, 0)) + assert_equal(2452355, Date.__send__(valid_weeknum_p, 2002,11,4, 0)) + assert_nil(Date.__send__(valid_weeknum_p, 1999,53,0, 1)) + assert_equal(2454101, Date.__send__(valid_weeknum_p, 2006,52,6, 1)) + assert_nil(Date.__send__(valid_weeknum_p, 1999,-53,-1, 1)) + assert_equal(2453743, Date.__send__(valid_weeknum_p, 2006,-52,-2, 1)) + assert_equal(2452355, Date.__send__(valid_weeknum_p, 2002,11,3, 1)) + end + + def test_valid_nth_kday + valid_nth_kday_p = :_valid_nth_kday? + 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)) + assert_equal(2448682, Date.__send__(valid_nth_kday_p, 1992,2, -1,6)) + assert_equal(2448682, Date.__send__(valid_nth_kday_p, 1992,2, -1,-1)) + end + + def test_valid_time + valid_time_p = :_valid_time? + 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)) + assert_nil(DateTime.__send__(valid_time_p, 23,59,60)) + assert_equal(Rational(86399, 86400), + DateTime.__send__(valid_time_p, 23,59,59)) + assert_equal(Rational(86399, 86400), + DateTime.__send__(valid_time_p, -1,-1,-1)) + assert_equal(Rational(1), DateTime.__send__(valid_time_p, 24,0,0)) + assert_nil(DateTime.__send__(valid_time_p, 24,0,1)) + assert_nil(DateTime.__send__(valid_time_p, 24,1,0)) + assert_nil(DateTime.__send__(valid_time_p, 24,1,1)) + end + +end diff --git a/test/date/test_date_marshal.rb b/test/date/test_date_marshal.rb new file mode 100644 index 0000000000..2f86a8c8c2 --- /dev/null +++ b/test/date/test_date_marshal.rb @@ -0,0 +1,29 @@ +require 'test/unit' +require 'date' + +class TestDateMarshal < Test::Unit::TestCase + + def test_marshal + d = Date.new + m = Marshal.dump(d) + d2 = Marshal.load(m) + assert_equal(d, d2) + assert_equal(d.start, d2.start) + assert_instance_of(String, d2.to_s) + + d = Date.today + m = Marshal.dump(d) + d2 = Marshal.load(m) + assert_equal(d, d2) + assert_equal(d.start, d2.start) + assert_instance_of(String, d2.to_s) + + d = DateTime.now + m = Marshal.dump(d) + d2 = Marshal.load(m) + assert_equal(d, d2) + assert_equal(d.start, d2.start) + assert_instance_of(String, d2.to_s) + end + +end diff --git a/test/date/test_date_new.rb b/test/date/test_date_new.rb new file mode 100644 index 0000000000..6c1e463474 --- /dev/null +++ b/test/date/test_date_new.rb @@ -0,0 +1,276 @@ +require 'test/unit' +require 'date' + +class TestDateNew < Test::Unit::TestCase + + def test_jd + d = Date.jd + dt = DateTime.jd + assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday]) + assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday]) + assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec]) + + d2 = Date.jd + dt2 = DateTime.jd + assert_equal(d, d2) + assert_equal(dt, dt2) + + d = Date.jd(0) + assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday]) + d = DateTime.jd(0, 0,0,0, 0) + assert_equal([-4712, 1, 1, 0, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + d = DateTime.jd(0, 0,0,0, '+0900') + assert_equal([-4712, 1, 1, 0, 0, 0, 9.to_r/24], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + end + + def test_jd__ex + assert_raise(ArgumentError) do + DateTime.jd(0, 23,59,60,0) + end + end + + def test_ordinal + d = Date.ordinal + dt = DateTime.ordinal + assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday]) + assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday]) + assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec]) + + d2 = Date.ordinal + dt2 = DateTime.ordinal + assert_equal(d, d2) + assert_equal(dt, dt2) + + d = Date.ordinal(-4712,1) + assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday]) + + d = Date.ordinal(-4712,1.0) + assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday]) + + d = DateTime.ordinal(-4712,1, 0,0,0, 0) + assert_equal([-4712, 1, 1, 0, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + d = DateTime.ordinal(-4712,1, 0,0,0, '+0900') + assert_equal([-4712, 1, 1, 0, 0, 0, 9.to_r/24], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + end + + def test_ordinal__neg + d = Date.ordinal(-1,-1) + assert_equal([-1, 365], [d.year, d.yday]) + + d = DateTime.ordinal(-1,-1, -1,-1,-1, 0) + assert_equal([-1, 365, 23, 59, 59, 0], + [d.year, d.yday, d.hour, d.min, d.sec, d.offset]) + end + + def test_ordinal__ex + assert_raise(ArgumentError) do + Date.ordinal(2001,1.1) + end + assert_raise(ArgumentError) do + Date.ordinal(2001,366) + end + assert_raise(ArgumentError) do + DateTime.ordinal(2001,365, 23,59,60, 0) + end + end + + def test_civil + d = Date.civil + dt = DateTime.civil + assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday]) + assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday]) + assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec]) + + d2 = Date.civil + dt2 = DateTime.civil + assert_equal(d, d2) + assert_equal(dt, dt2) + + d = Date.civil(-4712,1,1) + assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday]) + + d = Date.civil(-4712,1,1.0) + assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday]) + + d = DateTime.civil(-4712,1,1, 0,0,0, 0) + assert_equal([-4712, 1, 1, 0, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + d = DateTime.civil(-4712,1,1, 0,0,0, '+0900') + assert_equal([-4712, 1, 1, 0, 0, 0, 9.to_r/24], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + + + d = DateTime.civil(2001,2,3 + 1.to_r/2) + assert_equal([2001, 2, 3, 12, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + d = DateTime.civil(2001,2,3, 4 + 1.to_r/2) + assert_equal([2001, 2, 3, 4, 30, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + d = DateTime.civil(2001,2,3, 4,5 + 1.to_r/2) + assert_equal([2001, 2, 3, 4, 5, 30, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + d = DateTime.civil(2001,2,3, 4,5,6 + 1.to_r/2) + assert_equal([2001, 2, 3, 4, 5, 6, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + assert_equal(1.to_r/2, d.sec_fraction) + end + + def test_civil__neg + d = Date.civil(-1,-1,-1) + assert_equal([-1, 12, 31], [d.year, d.mon, d.mday]) + + d = DateTime.civil(-1,-1,-1, -1,-1,-1, 0) + assert_equal([-1, 12, 31, 23, 59, 59, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + end + + def test_civil__ex + assert_raise(ArgumentError) do + Date.civil(2001,2,1.1) + end + assert_raise(ArgumentError) do + Date.civil(2001,2,29) + end + assert_raise(ArgumentError) do + DateTime.civil(2001,2,28, 23,59,60, 0) + end + assert_raise(ArgumentError) do + DateTime.civil(2001,2,28, 24,59,59, 0) + end + end + + def test_civil__reform + d = Date.jd(Date::ENGLAND, Date::ENGLAND) + dt = DateTime.jd(Date::ENGLAND, 0,0,0,0, Date::ENGLAND) + assert_equal([1752, 9, 14], [d.year, d.mon, d.mday]) + assert_equal([1752, 9, 14], [dt.year, dt.mon, dt.mday]) + d -= 1 + dt -= 1 + assert_equal([1752, 9, 2], [d.year, d.mon, d.mday]) + assert_equal([1752, 9, 2], [dt.year, dt.mon, dt.mday]) + + d = Date.jd(Date::ITALY, Date::ITALY) + dt = DateTime.jd(Date::ITALY, 0,0,0,0, Date::ITALY) + assert_equal([1582, 10, 15], [d.year, d.mon, d.mday]) + assert_equal([1582, 10, 15], [dt.year, dt.mon, dt.mday]) + d -= 1 + dt -= 1 + assert_equal([1582, 10, 4], [d.year, d.mon, d.mday]) + assert_equal([1582, 10, 4], [dt.year, dt.mon, dt.mday]) + end + + def test_commercial + d = Date.commercial + dt = DateTime.commercial + assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday]) + assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday]) + assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec]) + + d2 = Date.commercial + dt2 = DateTime.commercial + assert_equal(d, d2) + assert_equal(dt, dt2) + + d = Date.commercial(1582,40,5) + assert_equal([1582, 10, 15], [d.year, d.mon, d.mday]) + + d = Date.commercial(1582,40,5.0) + assert_equal([1582, 10, 15], [d.year, d.mon, d.mday]) + + d = DateTime.commercial(1582,40,5, 0,0,0, 0) + assert_equal([1582, 10, 15, 0, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + d = DateTime.commercial(1582,40,5, 0,0,0, '+0900') + assert_equal([1582, 10, 15, 0, 0, 0, 9.to_r/24], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + end + + def test_commercial__neg + d = Date.commercial(1998,-1,-1) + assert_equal([1999, 1, 3], [d.year, d.mon, d.mday]) + + d = DateTime.commercial(1998,-1,-1, -1,-1,-1, 0) + assert_equal([1999, 1, 3, 23, 59, 59, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) + end + + def test_commercial__ex + assert_raise(ArgumentError) do + Date.commercial(1997,1,1.1) + end + assert_raise(ArgumentError) do + Date.commercial(1997,53,1) + end + assert_raise(ArgumentError) do + DateTime.commercial(1997,52,1, 23,59,60, 0) + end + end + + def test_weeknum + d = Date.__send__(:weeknum) + dt = DateTime.__send__(:weeknum) + assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday]) + assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday]) + assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec]) + + d = Date.__send__(:weeknum, 2002,11,4, 0) + assert_equal(2452355, d.jd) + + d = DateTime.__send__(:weeknum, 2002,11,4, 0, 11,22,33) + assert_equal(2452355, d.jd) + assert_equal([11,22,33], [d.hour, d.min, d.sec]) + + assert_raise(ArgumentError) do + Date.__send__(:weeknum, 1999,53,0, 0) + end + assert_raise(ArgumentError) do + Date.__send__(:weeknum, 1999,-53,-1, 0) + end + end + + def test_nth_kday + d = Date.__send__(:nth_kday) + dt = DateTime.__send__(:nth_kday) + assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday]) + assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday]) + assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec]) + + d = Date.__send__(:nth_kday, 1992,2, 5,6) + assert_equal(2448682, d.jd) + + d = DateTime.__send__(:nth_kday, 1992,2, 5,6, 11,22,33) + assert_equal(2448682, d.jd) + assert_equal([11,22,33], [d.hour, d.min, d.sec]) + + assert_raise(ArgumentError) do + Date.__send__(:nth_kday, 2006,5, 5,0) + end + assert_raise(ArgumentError) do + Date.__send__(:nth_kday, 2006,5, -5,0) + end + end + + def test_today + d = Date.today + t = Time.now + t2 = Time.utc(t.year, t.mon, t.mday) + t3 = Time.utc(d.year, d.mon, d.mday) + assert_in_delta(t2, t3, 10) + + assert_equal(false, DateTime.respond_to?(:today)) + end + + def test_now + assert_equal(false, Date.respond_to?(:now)) + + d = DateTime.now + t = Time.now + t2 = Time.local(d.year, d.mon, d.mday, d.hour, d.min, d.sec) + assert_in_delta(t, t2, 10) + end + +end diff --git a/test/date/test_date_parse.rb b/test/date/test_date_parse.rb new file mode 100644 index 0000000000..a89dc9406e --- /dev/null +++ b/test/date/test_date_parse.rb @@ -0,0 +1,972 @@ +require 'test/unit' +require 'date' + +class TestDateParse < Test::Unit::TestCase + + def test__parse + [ + # ctime(3), asctime(3) + [['Sat Aug 28 02:55:50 1999',false],[1999,8,28,2,55,50,nil,nil,6]], + [['Sat Aug 28 02:55:50 02',false],[2,8,28,2,55,50,nil,nil,6]], + [['Sat Aug 28 02:55:50 02',true],[2002,8,28,2,55,50,nil,nil,6]], + [['Sat Aug 28 02:55:50 0002',false],[2,8,28,2,55,50,nil,nil,6]], + [['Sat Aug 28 02:55:50 0002',true],[2,8,28,2,55,50,nil,nil,6]], + + # date(1) + [['Sat Aug 28 02:29:34 JST 1999',false],[1999,8,28,2,29,34,'JST',9*3600,6]], + [['Sat Aug 28 02:29:34 MET DST 1999',false],[1999,8,28,2,29,34,'MET DST',2*3600,6]], + [['Sat Aug 28 02:29:34 AMT 1999',false],[1999,8,28,2,29,34,'AMT',nil,6]], + [['Sat Aug 28 02:29:34 PMT 1999',false],[1999,8,28,2,29,34,'PMT',nil,6]], + [['Sat Aug 28 02:29:34 PMT -1999',false],[-1999,8,28,2,29,34,'PMT',nil,6]], + + [['Sat Aug 28 02:29:34 JST 02',false],[2,8,28,2,29,34,'JST',9*3600,6]], + [['Sat Aug 28 02:29:34 JST 02',true],[2002,8,28,2,29,34,'JST',9*3600,6]], + [['Sat Aug 28 02:29:34 JST 0002',false],[2,8,28,2,29,34,'JST',9*3600,6]], + [['Sat Aug 28 02:29:34 JST 0002',true],[2,8,28,2,29,34,'JST',9*3600,6]], + + [['Sat Aug 28 02:29:34 GMT+09 0002',false],[2,8,28,2,29,34,'GMT+09',9*3600,6]], + [['Sat Aug 28 02:29:34 GMT+0900 0002',false],[2,8,28,2,29,34,'GMT+0900',9*3600,6]], + [['Sat Aug 28 02:29:34 GMT+09:00 0002',false],[2,8,28,2,29,34,'GMT+09:00',9*3600,6]], + [['Sat Aug 28 02:29:34 GMT-09 0002',false],[2,8,28,2,29,34,'GMT-09',-9*3600,6]], + [['Sat Aug 28 02:29:34 GMT-0900 0002',false],[2,8,28,2,29,34,'GMT-0900',-9*3600,6]], + [['Sat Aug 28 02:29:34 GMT-09:00 0002',false],[2,8,28,2,29,34,'GMT-09:00',-9*3600,6]], + [['Sat Aug 28 02:29:34 GMT-090102 0002',false],[2,8,28,2,29,34,'GMT-090102',-9*3600-60-2,6]], + [['Sat Aug 28 02:29:34 GMT-09:01:02 0002',false],[2,8,28,2,29,34,'GMT-09:01:02',-9*3600-60-2,6]], + + [['Sat Aug 28 02:29:34 GMT Standard Time 2000',false],[2000,8,28,2,29,34,'GMT Standard Time',0*3600,6]], + [['Sat Aug 28 02:29:34 Mountain Standard Time 2000',false],[2000,8,28,2,29,34,'Mountain Standard Time',-7*3600,6]], + [['Sat Aug 28 02:29:34 Mountain Daylight Time 2000',false],[2000,8,28,2,29,34,'Mountain Daylight Time',-6*3600,6]], + [['Sat Aug 28 02:29:34 Mexico Standard Time 2000',false],[2000,8,28,2,29,34,'Mexico Standard Time',-6*3600,6]], +# [['Sat Aug 28 02:29:34 Mexico Standard Time 2 2000',false],[2000,8,28,2,29,34,'Mexico Standard Time 2',-7*3600,6]], # cp + [['Sat Aug 28 02:29:34 E. Australia Standard Time 2000',false],[2000,8,28,2,29,34,'E. Australia Standard Time',10*3600,6]], + + # part of iso 8601 + [['1999-05-23 23:55:21',false],[1999,5,23,23,55,21,nil,nil,nil]], + [['1999-05-23 23:55:21+0900',false],[1999,5,23,23,55,21,'+0900',9*3600,nil]], + [['1999-05-23 23:55:21-0900',false],[1999,5,23,23,55,21,'-0900',-9*3600,nil]], + [['1999-05-23 23:55:21+09:00',false],[1999,5,23,23,55,21,'+09:00',9*3600,nil]], + [['1999-05-23T23:55:21-09:00',false],[1999,5,23,23,55,21,'-09:00',-9*3600,nil]], + [['1999-05-23 23:55:21Z',false],[1999,5,23,23,55,21,'Z',0,nil]], + [['1999-05-23T23:55:21Z',false],[1999,5,23,23,55,21,'Z',0,nil]], + [['-1999-05-23T23:55:21Z',false],[-1999,5,23,23,55,21,'Z',0,nil]], + [['-1999-05-23T23:55:21Z',true],[-1999,5,23,23,55,21,'Z',0,nil]], + [['19990523T23:55:21Z',false],[1999,5,23,23,55,21,'Z',0,nil]], + + [['+011985-04-12',false],[11985,4,12,nil,nil,nil,nil,nil,nil]], + [['+011985-04-12T10:15:30',false],[11985,4,12,10,15,30,nil,nil,nil]], + [['-011985-04-12',false],[-11985,4,12,nil,nil,nil,nil,nil,nil]], + [['-011985-04-12T10:15:30',false],[-11985,4,12,10,15,30,nil,nil,nil]], + + [['02-04-12',false],[2,4,12,nil,nil,nil,nil,nil,nil]], + [['02-04-12',true],[2002,4,12,nil,nil,nil,nil,nil,nil]], + [['0002-04-12',false],[2,4,12,nil,nil,nil,nil,nil,nil]], + [['0002-04-12',true],[2,4,12,nil,nil,nil,nil,nil,nil]], + + [['19990523',true],[1999,5,23,nil,nil,nil,nil,nil,nil]], + [['-19990523',true],[-1999,5,23,nil,nil,nil,nil,nil,nil]], + [['990523',true],[1999,5,23,nil,nil,nil,nil,nil,nil]], + [['0523',false],[nil,5,23,nil,nil,nil,nil,nil,nil]], + [['23',false],[nil,nil,23,nil,nil,nil,nil,nil,nil]], + + [['19990523 235521',true],[1999,5,23,23,55,21,nil,nil,nil]], + [['990523 235521',true],[1999,5,23,23,55,21,nil,nil,nil]], + [['0523 2355',false],[nil,5,23,23,55,nil,nil,nil,nil]], + [['23 2355',false],[nil,nil,23,23,55,nil,nil,nil,nil]], + + [['19990523T235521',true],[1999,5,23,23,55,21,nil,nil,nil]], + [['990523T235521',true],[1999,5,23,23,55,21,nil,nil,nil]], + [['19990523T235521.99',true],[1999,5,23,23,55,21,nil,nil,nil]], + [['990523T235521.99',true],[1999,5,23,23,55,21,nil,nil,nil]], + [['0523T2355',false],[nil,5,23,23,55,nil,nil,nil,nil]], + + [['19990523T235521+0900',true],[1999,5,23,23,55,21,'+0900',9*3600,nil]], + [['990523T235521-0900',true],[1999,5,23,23,55,21,'-0900',-9*3600,nil]], + [['19990523T235521.99+0900',true],[1999,5,23,23,55,21,'+0900',9*3600,nil]], + [['990523T235521.99-0900',true],[1999,5,23,23,55,21,'-0900',-9*3600,nil]], + [['0523T2355Z',false],[nil,5,23,23,55,nil,'Z',0,nil]], + + [['19990523235521.123456+0900',true],[1999,5,23,23,55,21,'+0900',9*3600,nil]], + [['19990523235521.123456-0900',true],[1999,5,23,23,55,21,'-0900',-9*3600,nil]], + [['19990523235521,123456+0900',true],[1999,5,23,23,55,21,'+0900',9*3600,nil]], + [['19990523235521,123456-0900',true],[1999,5,23,23,55,21,'-0900',-9*3600,nil]], + + [['990523235521,123456-0900',false],[99,5,23,23,55,21,'-0900',-9*3600,nil]], + [['0523235521,123456-0900',false],[nil,5,23,23,55,21,'-0900',-9*3600,nil]], + [['23235521,123456-0900',false],[nil,nil,23,23,55,21,'-0900',-9*3600,nil]], + [['235521,123456-0900',false],[nil,nil,nil,23,55,21,'-0900',-9*3600,nil]], + [['5521,123456-0900',false],[nil,nil,nil,nil,55,21,'-0900',-9*3600,nil]], + [['21,123456-0900',false],[nil,nil,nil,nil,nil,21,'-0900',-9*3600,nil]], + + [['3235521,123456-0900',false],[nil,nil,3,23,55,21,'-0900',-9*3600,nil]], + [['35521,123456-0900',false],[nil,nil,nil,3,55,21,'-0900',-9*3600,nil]], + [['521,123456-0900',false],[nil,nil,nil,nil,5,21,'-0900',-9*3600,nil]], + + # reversed iso 8601 (?) + [['23-05-1999',false],[1999,5,23,nil,nil,nil,nil,nil,nil]], + [['23-05-1999 23:55:21',false],[1999,5,23,23,55,21,nil,nil,nil]], + [['23-05--1999 23:55:21',false],[-1999,5,23,23,55,21,nil,nil,nil]], + [["23-05-'99",false],[99,5,23,nil,nil,nil,nil,nil,nil]], + [["23-05-'99",true],[1999,5,23,nil,nil,nil,nil,nil,nil]], + + # broken iso 8601 (?) +# [['1999-05-23T235521Z',false],[1999,5,23,23,55,21,'Z',0,nil]], # cp + [['19990523T23:55:21Z',false],[1999,5,23,23,55,21,'Z',0,nil]], + [['19990523235521.1234-100',true],[1999,5,23,23,55,21,'-100',-1*3600,nil]], + [['19990523235521.1234-10',true],[1999,5,23,23,55,21,'-10',-10*3600,nil]], + + # part of jis x0301 + [['M11.05.23',false],[1878,5,23,nil,nil,nil,nil,nil,nil]], + [['T11.05.23 23:55:21+0900',false],[1922,5,23,23,55,21,'+0900',9*3600,nil]], + [['S11.05.23 23:55:21-0900',false],[1936,5,23,23,55,21,'-0900',-9*3600,nil]], + [['S40.05.23 23:55:21+09:00',false],[1965,5,23,23,55,21,'+09:00',9*3600,nil]], + [['S40.05.23T23:55:21-09:00',false],[1965,5,23,23,55,21,'-09:00',-9*3600,nil]], + [['H11.05.23 23:55:21Z',false],[1999,5,23,23,55,21,'Z',0,nil]], + [['H11.05.23T23:55:21Z',false],[1999,5,23,23,55,21,'Z',0,nil]], + + # ofx date + [['19990523235521',false],[1999,5,23,23,55,21,nil,nil,nil]], + [['19990523235521.123',false],[1999,5,23,23,55,21,nil,nil,nil]], + [['19990523235521.123[-9]',false],[1999,5,23,23,55,21,'-9',-(9*3600),nil]], + [['19990523235521.123[+9]',false],[1999,5,23,23,55,21,'+9',+(9*3600),nil]], + [['19990523235521.123[9]',false],[1999,5,23,23,55,21,'9',+(9*3600),nil]], + [['19990523235521.123[-9.50]',false],[1999,5,23,23,55,21,'-9.50',-(9*3600+30*60),nil]], + [['19990523235521.123[+9.50]',false],[1999,5,23,23,55,21,'+9.50',+(9*3600+30*60),nil]], + [['19990523235521.123[-5:EST]',false],[1999,5,23,23,55,21,'EST',-5*3600,nil]], + [['19990523235521.123[+9:JST]',false],[1999,5,23,23,55,21,'JST',9*3600,nil]], + [['19990523235521.123[+12:XXX YYY ZZZ]',false],[1999,5,23,23,55,21,'XXX YYY ZZZ',12*3600,nil]], +# [['235521',false],[nil,nil,nil,23,55,21,nil,nil,nil]], # cp + [['235521.123',false],[nil,nil,nil,23,55,21,nil,nil,nil]], + [['235521.123[-9]',false],[nil,nil,nil,23,55,21,'-9',-9*3600,nil]], + [['235521.123[+9]',false],[nil,nil,nil,23,55,21,'+9',+9*3600,nil]], + [['235521.123[-5:EST]',false],[nil,nil,nil,23,55,21,'EST',-5*3600,nil]], + [['235521.123[+9:JST]',false],[nil,nil,nil,23,55,21,'JST',+9*3600,nil]], + + # rfc 2822 + [['Sun, 22 Aug 1999 00:45:29 -0400',false],[1999,8,22,0,45,29,'-0400',-4*3600,0]], + [['Sun, 22 Aug 1999 00:45:29 -9959',false],[1999,8,22,0,45,29,'-9959',-(99*3600+59*60),0]], + [['Sun, 22 Aug 1999 00:45:29 +9959',false],[1999,8,22,0,45,29,'+9959',+(99*3600+59*60),0]], + [['Sun, 22 Aug 05 00:45:29 -0400',true],[2005,8,22,0,45,29,'-0400',-4*3600,0]], + [['Sun, 22 Aug 49 00:45:29 -0400',true],[2049,8,22,0,45,29,'-0400',-4*3600,0]], +# [['Sun, 22 Aug 50 00:45:29 -0400',true],[1950,8,22,0,45,29,'-0400',-4*3600,0]], +# [['Sun, 22 Aug 111 00:45:29 -0400',true],[2011,8,22,0,45,29,'-0400',-4*3600,0]], + [['Sun, 22 Aug 1999 00:45:29 GMT',false],[1999,8,22,0,45,29,'GMT',0,0]], + [["Sun,\00022\r\nAug\r\n1999\r\n00:45:29\r\nGMT",false],[1999,8,22,0,45,29,'GMT',0,0]], + [['Sun, 22 Aug 1999 00:45 GMT',false],[1999,8,22,0,45,nil,'GMT',0,0]], + [['Sun, 22 Aug -1999 00:45 GMT',false],[-1999,8,22,0,45,nil,'GMT',0,0]], + [['Sun, 22 Aug 99 00:45:29 UT',true],[1999,8,22,0,45,29,'UT',0,0]], + [['Sun, 22 Aug 0099 00:45:29 UT',true],[99,8,22,0,45,29,'UT',0,0]], + + # rfc 850, obsoleted by rfc 1036 + [['Tuesday, 02-Mar-99 11:20:32 GMT',true],[1999,3,2,11,20,32,'GMT',0,2]], + + # W3C Working Draft - XForms - 4.8 Time + [['2000-01-31 13:20:00-5',false],[2000,1,31,13,20,0,'-5',-5*3600,nil]], + + # [-+]\d+.\d+ + [['2000-01-31 13:20:00-5.5',false],[2000,1,31,13,20,0,'-5.5',-5*3600-30*60,nil]], + [['2000-01-31 13:20:00-5,5',false],[2000,1,31,13,20,0,'-5,5',-5*3600-30*60,nil]], + [['2000-01-31 13:20:00+3.5',false],[2000,1,31,13,20,0,'+3.5',3*3600+30*60,nil]], + [['2000-01-31 13:20:00+3,5',false],[2000,1,31,13,20,0,'+3,5',3*3600+30*60,nil]], + + # mil + [['2000-01-31 13:20:00 Z',false],[2000,1,31,13,20,0,'Z',0*3600,nil]], + [['2000-01-31 13:20:00 H',false],[2000,1,31,13,20,0,'H',8*3600,nil]], + [['2000-01-31 13:20:00 M',false],[2000,1,31,13,20,0,'M',12*3600,nil]], + [['2000-01-31 13:20 M',false],[2000,1,31,13,20,nil,'M',12*3600,nil]], + [['2000-01-31 13:20:00 S',false],[2000,1,31,13,20,0,'S',-6*3600,nil]], + [['2000-01-31 13:20:00 A',false],[2000,1,31,13,20,0,'A',1*3600,nil]], + [['2000-01-31 13:20:00 P',false],[2000,1,31,13,20,0,'P',-3*3600,nil]], + + # dot + [['1999.5.2',false],[1999,5,2,nil,nil,nil,nil,nil,nil]], + [['1999.05.02',false],[1999,5,2,nil,nil,nil,nil,nil,nil]], + [['-1999.05.02',false],[-1999,5,2,nil,nil,nil,nil,nil,nil]], +# [['05.02',false],[nil,5,2,nil,nil,nil,nil,nil,nil]], # not support +# [[' 5. 2',false],[nil,5,2,nil,nil,nil,nil,nil,nil]], # not support + + [['0099.5.2',false],[99,5,2,nil,nil,nil,nil,nil,nil]], + [['0099.5.2',true],[99,5,2,nil,nil,nil,nil,nil,nil]], + + [["'99.5.2",false],[99,5,2,nil,nil,nil,nil,nil,nil]], + [["'99.5.2",true],[1999,5,2,nil,nil,nil,nil,nil,nil]], + + # reversed dot + [['2.5.1999',false],[1999,5,2,nil,nil,nil,nil,nil,nil]], + [['02.05.1999',false],[1999,5,2,nil,nil,nil,nil,nil,nil]], + [['02.05.-1999',false],[-1999,5,2,nil,nil,nil,nil,nil,nil]], + + [['2.5.0099',false],[99,5,2,nil,nil,nil,nil,nil,nil]], + [['2.5.0099',true],[99,5,2,nil,nil,nil,nil,nil,nil]], + + [["2.5.'99",false],[99,5,2,nil,nil,nil,nil,nil,nil]], + [["2.5.'99",true],[1999,5,2,nil,nil,nil,nil,nil,nil]], + + # vms + [['08-DEC-1988',false],[1988,12,8,nil,nil,nil,nil,nil,nil]], + [['31-JAN-1999',false],[1999,1,31,nil,nil,nil,nil,nil,nil]], + [['31-JAN--1999',false],[-1999,1,31,nil,nil,nil,nil,nil,nil]], + + [['08-DEC-88',false],[88,12,8,nil,nil,nil,nil,nil,nil]], + [['08-DEC-88',true],[1988,12,8,nil,nil,nil,nil,nil,nil]], + [['08-DEC-0088',false],[88,12,8,nil,nil,nil,nil,nil,nil]], + [['08-DEC-0088',true],[88,12,8,nil,nil,nil,nil,nil,nil]], + + # swaped vms + [['DEC-08-1988',false],[1988,12,8,nil,nil,nil,nil,nil,nil]], + [['JAN-31-1999',false],[1999,1,31,nil,nil,nil,nil,nil,nil]], + [['JAN-31--1999',false],[-1999,1,31,nil,nil,nil,nil,nil,nil]], + [['JAN-1999',false],[1999,1,nil,nil,nil,nil,nil,nil,nil]], + [['JAN--1999',false],[-1999,1,nil,nil,nil,nil,nil,nil,nil]], + + # reversed vms + [['1988-DEC-08',false],[1988,12,8,nil,nil,nil,nil,nil,nil]], + [['1999-JAN-31',false],[1999,1,31,nil,nil,nil,nil,nil,nil]], + [['-1999-JAN-31',false],[-1999,1,31,nil,nil,nil,nil,nil,nil]], + + [['0088-DEC-08',false],[88,12,8,nil,nil,nil,nil,nil,nil]], + [['0088-DEC-08',true],[88,12,8,nil,nil,nil,nil,nil,nil]], + + [["'88/12/8",false],[88,12,8,nil,nil,nil,nil,nil,nil]], + [["'88/12/8",true],[1988,12,8,nil,nil,nil,nil,nil,nil]], + + # non-spaced eu + [['08/dec/1988',false],[1988,12,8,nil,nil,nil,nil,nil,nil]], + [['31/jan/1999',false],[1999,1,31,nil,nil,nil,nil,nil,nil]], + [['31/jan/-1999',false],[-1999,1,31,nil,nil,nil,nil,nil,nil]], + [['08.dec.1988',false],[1988,12,8,nil,nil,nil,nil,nil,nil]], + [['31.jan.1999',false],[1999,1,31,nil,nil,nil,nil,nil,nil]], + [['31.jan.-1999',false],[-1999,1,31,nil,nil,nil,nil,nil,nil]], + + # non-spaced us + [['dec/08/1988',false],[1988,12,8,nil,nil,nil,nil,nil,nil]], + [['jan/31/1999',false],[1999,1,31,nil,nil,nil,nil,nil,nil]], + [['jan/31/-1999',false],[-1999,1,31,nil,nil,nil,nil,nil,nil]], + [['jan/31',false],[nil,1,31,nil,nil,nil,nil,nil,nil]], + [['jan/1988',false],[1988,1,nil,nil,nil,nil,nil,nil,nil]], + [['dec.08.1988',false],[1988,12,8,nil,nil,nil,nil,nil,nil]], + [['jan.31.1999',false],[1999,1,31,nil,nil,nil,nil,nil,nil]], + [['jan.31.-1999',false],[-1999,1,31,nil,nil,nil,nil,nil,nil]], + [['jan.31',false],[nil,1,31,nil,nil,nil,nil,nil,nil]], + [['jan.1988',false],[1988,1,nil,nil,nil,nil,nil,nil,nil]], + + # month and day of month + [['Jan 1',false],[nil,1,1,nil,nil,nil,nil,nil,nil]], + [['Jul 11',false],[nil,7,11,nil,nil,nil,nil,nil,nil]], + [['July 11',false],[nil,7,11,nil,nil,nil,nil,nil,nil]], + [['Sept 23',false],[nil,9,23,nil,nil,nil,nil,nil,nil]], + [['Sep. 23',false],[nil,9,23,nil,nil,nil,nil,nil,nil]], + [['Sept. 23',false],[nil,9,23,nil,nil,nil,nil,nil,nil]], + [['September 23',false],[nil,9,23,nil,nil,nil,nil,nil,nil]], + [['October 1st',false],[nil,10,1,nil,nil,nil,nil,nil,nil]], + [['October 23rd',false],[nil,10,23,nil,nil,nil,nil,nil,nil]], + [['October 25th 1999',false],[1999,10,25,nil,nil,nil,nil,nil,nil]], + [['October 25th -1999',false],[-1999,10,25,nil,nil,nil,nil,nil,nil]], + [['october 25th 1999',false],[1999,10,25,nil,nil,nil,nil,nil,nil]], + [['OCTOBER 25th 1999',false],[1999,10,25,nil,nil,nil,nil,nil,nil]], + [['oCtoBer 25th 1999',false],[1999,10,25,nil,nil,nil,nil,nil,nil]], + [['aSep 23',false],[nil,nil,23,nil,nil,nil,nil,nil,nil]], + + # month and year + [['Sept 1990',false],[1990,9,nil,nil,nil,nil,nil,nil,nil]], + [["Sept '90",false],[90,9,nil,nil,nil,nil,nil,nil,nil]], + [["Sept '90",true],[1990,9,nil,nil,nil,nil,nil,nil,nil]], + [['1990/09',false],[1990,9,nil,nil,nil,nil,nil,nil,nil]], + [['09/1990',false],[1990,9,nil,nil,nil,nil,nil,nil,nil]], + [["aSep '90",false],[90,nil,nil,nil,nil,nil,nil,nil,nil]], + + # year + [["'90",false],[90,nil,nil,nil,nil,nil,nil,nil,nil]], + [["'90",true],[1990,nil,nil,nil,nil,nil,nil,nil,nil]], + + # month + [['Jun',false],[nil,6,nil,nil,nil,nil,nil,nil,nil]], + [['June',false],[nil,6,nil,nil,nil,nil,nil,nil,nil]], + [['Sep',false],[nil,9,nil,nil,nil,nil,nil,nil,nil]], + [['Sept',false],[nil,9,nil,nil,nil,nil,nil,nil,nil]], + [['September',false],[nil,9,nil,nil,nil,nil,nil,nil,nil]], + [['aSep',false],[nil,nil,nil,nil,nil,nil,nil,nil,nil]], + + # day of month + [['1st',false],[nil,nil,1,nil,nil,nil,nil,nil,nil]], + [['2nd',false],[nil,nil,2,nil,nil,nil,nil,nil,nil]], + [['3rd',false],[nil,nil,3,nil,nil,nil,nil,nil,nil]], + [['4th',false],[nil,nil,4,nil,nil,nil,nil,nil,nil]], + [['29th',false],[nil,nil,29,nil,nil,nil,nil,nil,nil]], + [['31st',false],[nil,nil,31,nil,nil,nil,nil,nil,nil]], + [['1sta',false],[nil,nil,nil,nil,nil,nil,nil,nil,nil]], + + # era + [['Sat Aug 28 02:29:34 GMT CE 2000',false],[2000,8,28,2,29,34,'GMT',0,6]], + [['Sat Aug 28 02:29:34 GMT C.E. 2000',false],[2000,8,28,2,29,34,'GMT',0,6]], + [['Sat Aug 28 02:29:34 GMT BCE 2000',false],[-1999,8,28,2,29,34,'GMT',0,6]], + [['Sat Aug 28 02:29:34 GMT B.C.E. 2000',false],[-1999,8,28,2,29,34,'GMT',0,6]], + [['Sat Aug 28 02:29:34 GMT AD 2000',false],[2000,8,28,2,29,34,'GMT',0,6]], + [['Sat Aug 28 02:29:34 GMT A.D. 2000',false],[2000,8,28,2,29,34,'GMT',0,6]], + [['Sat Aug 28 02:29:34 GMT BC 2000',false],[-1999,8,28,2,29,34,'GMT',0,6]], + [['Sat Aug 28 02:29:34 GMT B.C. 2000',false],[-1999,8,28,2,29,34,'GMT',0,6]], + [['Sat Aug 28 02:29:34 GMT 2000 BC',false],[-1999,8,28,2,29,34,'GMT',0,6]], + [['Sat Aug 28 02:29:34 GMT 2000 BCE',false],[-1999,8,28,2,29,34,'GMT',0,6]], + [['Sat Aug 28 02:29:34 GMT 2000 B.C.',false],[-1999,8,28,2,29,34,'GMT',0,6]], + [['Sat Aug 28 02:29:34 GMT 2000 B.C.E.',false],[-1999,8,28,2,29,34,'GMT',0,6]], + + # collection +# [['le ler juillet 1982',false],[1982,7,1,nil,nil,nil,nil,nil,nil]], # bih 1982 +# [['30 June 1982 , 23h 59m 59s',false],[1982,6,30,23,59,59,nil,nil,nil]], # bih 1982 + [['Tuesday, May 18, 1999 Published at 13:36 GMT 14:36 UK',false],[1999,5,18,13,36,nil,'GMT',0,2]], # bbc.co.uk + [['July 20, 2000 Web posted at: 3:37 p.m. EDT (1937 GMT)',false],[2000,7,20,15,37,nil,'EDT',-4*3600,nil]], # cnn.com + [['12:54 p.m. EDT, September 11, 2006',false],[2006,9,11,12,54,nil,'EDT',-4*3600,nil]], # cnn.com + [['February 04, 2001 at 10:59 AM PST',false],[2001,2,4,10,59,nil,'PST',-8*3600,nil]], # old amazon.com + [['Monday May 08, @01:55PM',false],[nil,5,8,13,55,nil,nil,nil,1]], # slashdot.org + [['06.June 2005',false],[2005,6,6,nil,nil,nil,nil,nil,nil]], # dhl.com + + # etc. + [['8:00 pm lt',false],[nil,nil,nil,20,0,nil,'lt',nil,nil]], + [['4:00 AM, Jan. 12, 1990',false],[1990,1,12,4,0,nil,nil,nil,nil]], + [['Jan. 12 4:00 AM 1990',false],[1990,1,12,4,0,nil,nil,nil,nil]], +# [['Jan. 12 4:00 -1990',false],[-1990,1,12,4,0,nil,nil,nil,nil]], # cp + [['1990-01-12 04:00:00+00',false],[1990,1,12,4,0,0,'+00',0,nil]], + [['1990-01-11 20:00:00-08',false],[1990,1,11,20,0,0,'-08',-8*3600,nil]], + [['1990/01/12 04:00:00',false],[1990,1,12,4,0,0,nil,nil,nil]], +# [['Thu Jan 11 20:00:00 1990 LT',false], [1990,1,11,20,0,0,'LT',nil,4]], # cp + [['Thu Jan 11 20:00:00 PST 1990',false],[1990,1,11,20,0,0,'PST',-8*3600,4]], + [['Fri Jan 12 04:00:00 GMT 1990',false],[1990,1,12,4,0,0,'GMT',0,5]], + [['Thu, 11 Jan 1990 20:00:00 -0800',false],[1990,1,11,20,0,0,'-0800',-8*3600,4]], + [['12-January-1990, 04:00 WET',false],[1990,1,12,4,0,nil,'WET',0*3600,nil]], + [['jan 2 3 am +4 5',false],[5,1,2,3,nil,nil,'+4',4*3600,nil]], + [['jan 2 3 am +4 5',true],[2005,1,2,3,nil,nil,'+4',4*3600,nil]], + [['fri1feb3bc4pm+5',false],[-2,2,1,16,nil,nil,'+5',5*3600,5]], + [['fri1feb3bc4pm+5',true],[-2,2,1,16,nil,nil,'+5',5*3600,5]], + [['03 feb 1st',false],[03,2,1,nil,nil,nil,nil,nil,nil]], + + # apostrophe + [["July 4, '79",true],[1979,7,4,nil,nil,nil,nil,nil,nil]], + [["4th July '79",true],[1979,7,4,nil,nil,nil,nil,nil,nil]], + + # day of week + [['Sunday',false],[nil,nil,nil,nil,nil,nil,nil,nil,0]], + [['Mon',false],[nil,nil,nil,nil,nil,nil,nil,nil,1]], + [['Tue',false],[nil,nil,nil,nil,nil,nil,nil,nil,2]], + [['Wed',false],[nil,nil,nil,nil,nil,nil,nil,nil,3]], + [['Thurs',false],[nil,nil,nil,nil,nil,nil,nil,nil,4]], + [['Friday',false],[nil,nil,nil,nil,nil,nil,nil,nil,5]], + [['Sat.',false],[nil,nil,nil,nil,nil,nil,nil,nil,6]], + [['sat.',false],[nil,nil,nil,nil,nil,nil,nil,nil,6]], + [['SAT.',false],[nil,nil,nil,nil,nil,nil,nil,nil,6]], + [['sAt.',false],[nil,nil,nil,nil,nil,nil,nil,nil,6]], +# [['su',false],[nil,nil,nil,nil,nil,nil,nil,nil,0]], +# [['mo',false],[nil,nil,nil,nil,nil,nil,nil,nil,1]], + + # time + [['09:55',false],[nil,nil,nil,9,55,nil,nil,nil,nil]], + [['09:55:30',false],[nil,nil,nil,9,55,30,nil,nil,nil]], + [['09:55:30am',false],[nil,nil,nil,9,55,30,nil,nil,nil]], + [['09:55:30pm',false],[nil,nil,nil,21,55,30,nil,nil,nil]], + [['09:55:30a.m.',false],[nil,nil,nil,9,55,30,nil,nil,nil]], + [['09:55:30p.m.',false],[nil,nil,nil,21,55,30,nil,nil,nil]], + [['09:55:30pm GMT',false],[nil,nil,nil,21,55,30,'GMT',0,nil]], + [['09:55:30p.m. GMT',false],[nil,nil,nil,21,55,30,'GMT',0,nil]], + [['09:55+0900',false],[nil,nil,nil,9,55,nil,'+0900',9*3600,nil]], + [['09 AM',false],[nil,nil,nil,9,nil,nil,nil,nil,nil]], + [['09am',false],[nil,nil,nil,9,nil,nil,nil,nil,nil]], + [['09 A.M.',false],[nil,nil,nil,9,nil,nil,nil,nil,nil]], + [['09 PM',false],[nil,nil,nil,21,nil,nil,nil,nil,nil]], + [['09pm',false],[nil,nil,nil,21,nil,nil,nil,nil,nil]], + [['09 P.M.',false],[nil,nil,nil,21,nil,nil,nil,nil,nil]], + + [['9h22m23s',false],[nil,nil,nil,9,22,23,nil,nil,nil]], + [['9h 22m 23s',false],[nil,nil,nil,9,22,23,nil,nil,nil]], + [['9h22m',false],[nil,nil,nil,9,22,nil,nil,nil,nil]], + [['9h 22m',false],[nil,nil,nil,9,22,nil,nil,nil,nil]], + [['9h',false],[nil,nil,nil,9,nil,nil,nil,nil,nil]], + [['9h 22m 23s am',false],[nil,nil,nil,9,22,23,nil,nil,nil]], + [['9h 22m 23s pm',false],[nil,nil,nil,21,22,23,nil,nil,nil]], + [['9h 22m am',false],[nil,nil,nil,9,22,nil,nil,nil,nil]], + [['9h 22m pm',false],[nil,nil,nil,21,22,nil,nil,nil,nil]], + [['9h am',false],[nil,nil,nil,9,nil,nil,nil,nil,nil]], + [['9h pm',false],[nil,nil,nil,21,nil,nil,nil,nil,nil]], + + [['00:00',false],[nil,nil,nil,0,0,nil,nil,nil,nil]], + [['01:00',false],[nil,nil,nil,1,0,nil,nil,nil,nil]], + [['11:00',false],[nil,nil,nil,11,0,nil,nil,nil,nil]], + [['12:00',false],[nil,nil,nil,12,0,nil,nil,nil,nil]], + [['13:00',false],[nil,nil,nil,13,0,nil,nil,nil,nil]], + [['23:00',false],[nil,nil,nil,23,0,nil,nil,nil,nil]], + [['24:00',false],[nil,nil,nil,24,0,nil,nil,nil,nil]], + + [['00:00 AM',false],[nil,nil,nil,0,0,nil,nil,nil,nil]], + [['12:00 AM',false],[nil,nil,nil,0,0,nil,nil,nil,nil]], + [['01:00 AM',false],[nil,nil,nil,1,0,nil,nil,nil,nil]], + [['11:00 AM',false],[nil,nil,nil,11,0,nil,nil,nil,nil]], + [['00:00 PM',false],[nil,nil,nil,12,0,nil,nil,nil,nil]], + [['12:00 PM',false],[nil,nil,nil,12,0,nil,nil,nil,nil]], + [['01:00 PM',false],[nil,nil,nil,13,0,nil,nil,nil,nil]], + [['11:00 PM',false],[nil,nil,nil,23,0,nil,nil,nil,nil]], + + # pick up the rest + [['2000-01-02 1',false],[2000,1,2,1,nil,nil,nil,nil,nil]], + [['2000-01-02 23',false],[2000,1,2,23,nil,nil,nil,nil,nil]], + [['2000-01-02 24',false],[2000,1,2,24,nil,nil,nil,nil,nil]], + [['1 03:04:05',false],[nil,nil,1,3,4,5,nil,nil,nil]], + [['02 03:04:05',false],[nil,nil,2,3,4,5,nil,nil,nil]], + [['31 03:04:05',false],[nil,nil,31,3,4,5,nil,nil,nil]], + + # null, space + [['',false],[nil,nil,nil,nil,nil,nil,nil,nil,nil]], + [["\s",false],[nil,nil,nil,nil,nil,nil,nil,nil,nil]], + [["\s" * 10, true],[nil,nil,nil,nil,nil,nil,nil,nil,nil]], + [["\t",false],[nil,nil,nil,nil,nil,nil,nil,nil,nil]], + [["\n",false],[nil,nil,nil,nil,nil,nil,nil,nil,nil]], + [["\v",false],[nil,nil,nil,nil,nil,nil,nil,nil,nil]], + [["\f",false],[nil,nil,nil,nil,nil,nil,nil,nil,nil]], + [["\r",false],[nil,nil,nil,nil,nil,nil,nil,nil,nil]], + [["\t\n\v\f\r\s",false],[nil,nil,nil,nil,nil,nil,nil,nil,nil]], + [["1999-05-23\t\n\v\f\r\s21:34:56",false],[1999,5,23,21,34,56,nil,nil,nil]], + [["1999-05-23\n\n\n\n\n\n21:34:56",false],[1999,5,23,21,34,56,nil,nil,nil]], + ].each do |x,y| + h = Date._parse(*x) + a = h.values_at(:year,:mon,:mday,:hour,:min,:sec,:zone,:offset,:wday) + if y[1] == -1 + a[1] = -1 + a[2] = h[:yday] + end + assert_equal(y, a, [x, y, a].inspect) + end + end + + def test__parse_slash_exp + [ + [['2/5/1999',false],[1999,5,2,nil,nil,nil,nil,nil,nil]], + [['02/05/1999',false],[1999,5,2,nil,nil,nil,nil,nil,nil]], + [['02/05/-1999',false],[-1999,5,2,nil,nil,nil,nil,nil,nil]], + [['05/02',false],[nil,5,2,nil,nil,nil,nil,nil,nil]], + [[' 5/ 2',false],[nil,5,2,nil,nil,nil,nil,nil,nil]], + + [["2/5/'99",true],[1999,5,2,nil,nil,nil,nil,nil,nil]], + [['2/5/0099',false],[99,5,2,nil,nil,nil,nil,nil,nil]], + [['2/5/0099',true],[99,5,2,nil,nil,nil,nil,nil,nil]], + + [['2/5 1999',false],[1999,5,2,nil,nil,nil,nil,nil,nil]], + [['2/5-1999',false],[1999,5,2,nil,nil,nil,nil,nil,nil]], + [['2/5--1999',false],[-1999,5,2,nil,nil,nil,nil,nil,nil]], + + # big + [['99/5/2',false],[99,5,2,nil,nil,nil,nil,nil,nil]], + [['99/5/2',true],[1999,5,2,nil,nil,nil,nil,nil,nil]], + + [['1999/5/2',false],[1999,5,2,nil,nil,nil,nil,nil,nil]], + [['1999/05/02',false],[1999,5,2,nil,nil,nil,nil,nil,nil]], + [['-1999/05/02',false],[-1999,5,2,nil,nil,nil,nil,nil,nil]], + + [['0099/5/2',false],[99,5,2,nil,nil,nil,nil,nil,nil]], + [['0099/5/2',true],[99,5,2,nil,nil,nil,nil,nil,nil]], + + [["'99/5/2",false],[99,5,2,nil,nil,nil,nil,nil,nil]], + [["'99/5/2",true],[1999,5,2,nil,nil,nil,nil,nil,nil]], + ].each do |x,y| + h = Date._parse(*x) + a = h.values_at(:year,:mon,:mday,:hour,:min,:sec,:zone,:offset,:wday) + if y[1] == -1 + a[1] = -1 + a[2] = h[:yday] + end + assert_equal(y, a, [x, y, a].inspect) + end + end + + def test__parse__2 + h = Date._parse('22:45:59.5') + assert_equal([22, 45, 59, 5.to_r/10**1], h.values_at(:hour, :min, :sec, :sec_fraction)) + h = Date._parse('22:45:59.05') + assert_equal([22, 45, 59, 5.to_r/10**2], h.values_at(:hour, :min, :sec, :sec_fraction)) + h = Date._parse('22:45:59.005') + assert_equal([22, 45, 59, 5.to_r/10**3], h.values_at(:hour, :min, :sec, :sec_fraction)) + h = Date._parse('22:45:59.0123') + assert_equal([22, 45, 59, 123.to_r/10**4], h.values_at(:hour, :min, :sec, :sec_fraction)) + + h = Date._parse('224559.5') + assert_equal([22, 45, 59, 5.to_r/10**1], h.values_at(:hour, :min, :sec, :sec_fraction)) + h = Date._parse('224559.05') + assert_equal([22, 45, 59, 5.to_r/10**2], h.values_at(:hour, :min, :sec, :sec_fraction)) + h = Date._parse('224559.005') + assert_equal([22, 45, 59, 5.to_r/10**3], h.values_at(:hour, :min, :sec, :sec_fraction)) + h = Date._parse('224559.0123') + assert_equal([22, 45, 59, 123.to_r/10**4], h.values_at(:hour, :min, :sec, :sec_fraction)) + + h = Date._parse('2006-w15-5') + assert_equal([2006, 15, 5], h.values_at(:cwyear, :cweek, :cwday)) + h = Date._parse('2006w155') + assert_equal([2006, 15, 5], h.values_at(:cwyear, :cweek, :cwday)) + h = Date._parse('06w155', false) + assert_equal([6, 15, 5], h.values_at(:cwyear, :cweek, :cwday)) + h = Date._parse('06w155', true) + assert_equal([2006, 15, 5], h.values_at(:cwyear, :cweek, :cwday)) + + h = Date._parse('2006-w15') + assert_equal([2006, 15, nil], h.values_at(:cwyear, :cweek, :cwday)) + h = Date._parse('2006w15') + assert_equal([2006, 15, nil], h.values_at(:cwyear, :cweek, :cwday)) + + h = Date._parse('-w15-5') + assert_equal([nil, 15, 5], h.values_at(:cwyear, :cweek, :cwday)) + h = Date._parse('-w155') + assert_equal([nil, 15, 5], h.values_at(:cwyear, :cweek, :cwday)) + + h = Date._parse('-w15') + assert_equal([nil, 15, nil], h.values_at(:cwyear, :cweek, :cwday)) + h = Date._parse('-w15') + assert_equal([nil, 15, nil], h.values_at(:cwyear, :cweek, :cwday)) + + h = Date._parse('-w-5') + assert_equal([nil, nil, 5], h.values_at(:cwyear, :cweek, :cwday)) + + h = Date._parse('--11-29') + assert_equal([nil, 11, 29], h.values_at(:year, :mon, :mday)) + h = Date._parse('--1129') + assert_equal([nil, 11, 29], h.values_at(:year, :mon, :mday)) + h = Date._parse('--11') + assert_equal([nil, 11, nil], h.values_at(:year, :mon, :mday)) + h = Date._parse('---29') + assert_equal([nil, nil, 29], h.values_at(:year, :mon, :mday)) + h = Date._parse('-333') + assert_equal([nil, 333], h.values_at(:year, :yday)) + + h = Date._parse('2006-333') + assert_equal([2006, 333], h.values_at(:year, :yday)) + h = Date._parse('2006333') + assert_equal([2006, 333], h.values_at(:year, :yday)) + h = Date._parse('06333', false) + assert_equal([6, 333], h.values_at(:year, :yday)) + h = Date._parse('06333', true) + assert_equal([2006, 333], h.values_at(:year, :yday)) + h = Date._parse('333') + assert_equal([nil, 333], h.values_at(:year, :yday)) + end + + def test_parse + assert_equal(Date.new, Date.parse) + assert_equal(Date.new(2002,3,14), Date.parse('2002-03-14')) + + assert_equal(DateTime.new(2002,3,14,11,22,33, 0), + DateTime.parse('2002-03-14T11:22:33Z')) + assert_equal(DateTime.new(2002,3,14,11,22,33, 9.to_r/24), + DateTime.parse('2002-03-14T11:22:33+09:00')) + assert_equal(DateTime.new(2002,3,14,11,22,33, -9.to_r/24), + DateTime.parse('2002-03-14T11:22:33-09:00')) + assert_equal(DateTime.new(2002,3,14,11,22,33, -9.to_r/24) + 123456789.to_r/1000000000/86400, + DateTime.parse('2002-03-14T11:22:33.123456789-09:00')) + end + + def test_parse__2 + d1 = DateTime.parse('2004-03-13T22:45:59.5') + d2 = DateTime.parse('2004-03-13T22:45:59') + assert_equal(d2 + 5.to_r/10**1/86400, d1) + d1 = DateTime.parse('2004-03-13T22:45:59.05') + d2 = DateTime.parse('2004-03-13T22:45:59') + assert_equal(d2 + 5.to_r/10**2/86400, d1) + d1 = DateTime.parse('2004-03-13T22:45:59.005') + d2 = DateTime.parse('2004-03-13T22:45:59') + assert_equal(d2 + 5.to_r/10**3/86400, d1) + d1 = DateTime.parse('2004-03-13T22:45:59.0123') + d2 = DateTime.parse('2004-03-13T22:45:59') + assert_equal(d2 + 123.to_r/10**4/86400, d1) + d1 = DateTime.parse('2004-03-13T22:45:59.5') + d1 += 1.to_r/2/86400 + d2 = DateTime.parse('2004-03-13T22:46:00') + assert_equal(d2, d1) + end + + require 'time' + + def test_parse__time + methods = [:to_s, :asctime, :iso8601, :rfc2822, :httpdate, :xmlschema] + + t = Time.utc(2001,2,3,4,5,6) + methods.each do |m| + d = DateTime.parse(t.__send__(m)) + assert_equal([2001, 2, 3, 4, 5, 6], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec], + [m, t.__send__(m)].inspect) + end + + t = Time.mktime(2001,2,3,4,5,6) + methods.each do |m| + next if m == :httpdate + d = DateTime.parse(t.__send__(m)) + assert_equal([2001, 2, 3, 4, 5, 6], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec], + [m, t.__send__(m)].inspect) + end + end + + def test_parse__comp + n = DateTime.now + + d = DateTime.parse('073') + assert_equal([n.year, 73, 0, 0, 0], + [d.year, d.yday, d.hour, d.min, d.sec]) + d = DateTime.parse('13') + assert_equal([n.year, n.mon, 13, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + + d = DateTime.parse('Mar 13') + assert_equal([n.year, 3, 13, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + d = DateTime.parse('Mar 2004') + assert_equal([2004, 3, 1, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + d = DateTime.parse('23:55') + assert_equal([n.year, n.mon, n.mday, 23, 55, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + d = DateTime.parse('23:55:30') + assert_equal([n.year, n.mon, n.mday, 23, 55, 30], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + + d = DateTime.parse('Sun 23:55') + d2 = d - d.wday + assert_equal([d2.year, d2.mon, d2.mday, 23, 55, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + d = DateTime.parse('Aug 23:55') + assert_equal([n.year, 8, 1, 23, 55, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + end + + def test_parse__d_to_s + d = Date.new(2002,3,14) + assert_equal(d, Date.parse(d.to_s)) + + d = DateTime.new(2002,3,14,11,22,33, 9.to_r/24) + assert_equal(d, DateTime.parse(d.to_s)) + end + + def test_parse__ex + assert_raise(ArgumentError) do + Date.parse('') + end + assert_raise(ArgumentError) do + Date.parse('2001-02-29') + end + assert_raise(ArgumentError) do + DateTime.parse('2001-02-29T23:59:60') + end + assert_raise(ArgumentError) do + Date.parse('23:55') + end + end + + def test__iso8601 + h = Date._iso8601('01-02-03') + assert_equal([2001, 2, 3, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('2001-02-03') + assert_equal([2001, 2, 3, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('--02-03') + assert_equal([nil, 2, 3, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._iso8601('2001-02-03T04:05') + assert_equal([2001, 2, 3, 4, 5, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('2001-02-03T04:05:06') + assert_equal([2001, 2, 3, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('2001-02-03T04:05:06,07') + assert_equal([2001, 2, 3, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('2001-02-03T04:05:06Z') + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('2001-02-03T04:05:06.07+01:00') + assert_equal([2001, 2, 3, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._iso8601('010203') + assert_equal([2001, 2, 3, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('20010203') + assert_equal([2001, 2, 3, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('--0203') + assert_equal([nil, 2, 3, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._iso8601('010203T0405') + assert_equal([2001, 2, 3, 4, 5, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('20010203T0405') + assert_equal([2001, 2, 3, 4, 5, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('20010203T040506') + assert_equal([2001, 2, 3, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('20010203T040506,07') + assert_equal([2001, 2, 3, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('20010203T040506Z') + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('20010203T040506.07+0100') + assert_equal([2001, 2, 3, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._iso8601('200102030405') + assert_equal([2001, 2, 3, 4, 5, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('20010203040506') + assert_equal([2001, 2, 3, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('20010203040506,07') + assert_equal([2001, 2, 3, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('20010203040506Z') + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('20010203040506.07+0100') + assert_equal([2001, 2, 3, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._iso8601('01-023') + assert_equal([2001, 23, nil, nil, nil, nil], + h.values_at(:year, :yday, :hour, :min, :sec, :offset)) + h = Date._iso8601('2001-023') + assert_equal([2001, 23, nil, nil, nil, nil], + h.values_at(:year, :yday, :hour, :min, :sec, :offset)) + h = Date._iso8601('-023') + assert_equal([nil, 23, nil, nil, nil, nil], + h.values_at(:year, :yday, :hour, :min, :sec, :offset)) + + h = Date._iso8601('04:05') + assert_equal([nil, nil, nil, 4, 5, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('04:05:06') + assert_equal([nil, nil, nil, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('04:05:06,07') + assert_equal([nil, nil, nil, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('04:05:06Z') + assert_equal([nil, nil, nil, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('04:05:06.07+01:00') + assert_equal([nil, nil, nil, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._iso8601('040506,07') + assert_equal([nil, nil, nil, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._iso8601('040506.07+0100') + assert_equal([nil, nil, nil, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._iso8601('01-w02-3') + assert_equal([2001, 2, 3, nil, nil, nil, nil], + h.values_at(:cwyear, :cweek, :cwday, :hour, :min, :sec, :offset)) + h = Date._iso8601('2001-w02-3') + assert_equal([2001, 2, 3, nil, nil, nil, nil], + h.values_at(:cwyear, :cweek, :cwday, :hour, :min, :sec, :offset)) + h = Date._iso8601('2001w023') + assert_equal([2001, 2, 3, nil, nil, nil, nil], + h.values_at(:cwyear, :cweek, :cwday, :hour, :min, :sec, :offset)) + h = Date._iso8601('-w02-3') + assert_equal([nil, 2, 3, nil, nil, nil, nil], + h.values_at(:cwyear, :cweek, :cwday, :hour, :min, :sec, :offset)) + h = Date._iso8601('-w-3') + assert_equal([nil, nil, 3, nil, nil, nil, nil], + h.values_at(:cwyear, :cweek, :cwday, :hour, :min, :sec, :offset)) + end + + def test__rfc3339 + h = Date._rfc3339('2001-02-03T04:05:06Z') + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._rfc3339('2001-02-03 04:05:06Z') + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._rfc3339('2001-02-03T04:05:06.07+01:00') + assert_equal([2001, 2, 3, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + end + + def test__xmlschema + h = Date._xmlschema('2001-02-03') + assert_equal([2001, 2, 3, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('2001-02-03Z') + assert_equal([2001, 2, 3, nil, nil, nil, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('2001-02-03+01:00') + assert_equal([2001, 2, 3, nil, nil, nil, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._xmlschema('2001-02-03T04:05:06') + assert_equal([2001, 2, 3, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('2001-02-03T04:05:06.07') + assert_equal([2001, 2, 3, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('2001-02-03T04:05:06.07Z') + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('2001-02-03T04:05:06.07+01:00') + assert_equal([2001, 2, 3, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._xmlschema('04:05:06') + assert_equal([nil, nil, nil, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('04:05:06Z') + assert_equal([nil, nil, nil, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('04:05:06+01:00') + assert_equal([nil, nil, nil, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._xmlschema('2001-02') + assert_equal([2001, 2, nil, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('2001-02Z') + assert_equal([2001, 2, nil, nil, nil, nil, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('2001-02+01:00') + assert_equal([2001, 2, nil, nil, nil, nil, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('2001-02-01:00') + assert_equal([2001, 2, nil, nil, nil, nil, -3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._xmlschema('2001') + assert_equal([2001, nil, nil, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('2001Z') + assert_equal([2001, nil, nil, nil, nil, nil, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('2001+01:00') + assert_equal([2001, nil, nil, nil, nil, nil, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('2001-01:00') + assert_equal([2001, nil, nil, nil, nil, nil, -3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._xmlschema('--02') + assert_equal([nil, 2, nil, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('--02Z') + assert_equal([nil, 2, nil, nil, nil, nil, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._xmlschema('--02+01:00') + assert_equal([nil, 2, nil, nil, nil, nil, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._xmlschema('92001-02-03T04:05:06.07+01:00') + assert_equal([92001, 2, 3, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._xmlschema('-92001-02-03T04:05:06.07+01:00') + assert_equal([-92001, 2, 3, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + end + + def test__rfc2822 + h = Date._rfc2822('Sat, 3 Feb 2001 04:05:06 UT') + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._rfc2822('Sat, 3 Feb 2001 04:05:06 EST') + assert_equal([2001, 2, 3, 4, 5, 6, -5*3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._rfc2822('Sat, 3 Feb 2001 04:05:06 +0000') + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._rfc2822('Sat, 3 Feb 2001 04:05:06 +0100') + assert_equal([2001, 2, 3, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._rfc2822('Sat, 03 Feb 50 04:05:06 +0100') + assert_equal([1950, 2, 3, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._rfc2822('Sat, 03 Feb 49 04:05:06 +0100') + assert_equal([2049, 2, 3, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._rfc2822('Sat, 03 Feb 100 04:05:06 +0100') + assert_equal([2000, 2, 3, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h1 = Date._rfc2822('Sat, 3 Feb 2001 04:05:06 UT') + h2 = Date._rfc822('Sat, 3 Feb 2001 04:05:06 UT') + assert_equal(h1, h2) + end + + def test__httpdate + h = Date._httpdate('Sat, 03 Feb 2001 04:05:06 GMT') + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._httpdate('Saturday, 03-Feb-01 04:05:06 GMT') + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._httpdate('Sat Feb 3 04:05:06 2001') + assert_equal([2001, 2, 3, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._httpdate('Sat Feb 03 04:05:06 2001') + assert_equal([2001, 2, 3, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + end + + def test__jisx0301 + h = Date._jisx0301('13.02.03') + assert_equal([2001, 2, 3, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('H13.02.03') + assert_equal([2001, 2, 3, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('S63.02.03') + assert_equal([1988, 2, 3, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._jisx0301('H13.02.03T04:05:06') + assert_equal([2001, 2, 3, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('H13.02.03T04:05:06,07') + assert_equal([2001, 2, 3, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('H13.02.03T04:05:06Z') + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('H13.02.03T04:05:06.07+0100') + assert_equal([2001, 2, 3, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + end + + def test_iso8601 + assert_instance_of(Date, Date.iso8601) + assert_instance_of(DateTime, DateTime.iso8601) + end + + def test_rfc3339 + assert_instance_of(Date, Date.rfc3339) + assert_instance_of(DateTime, DateTime.rfc3339) + end + + def test_xmlschema + assert_instance_of(Date, Date.xmlschema) + assert_instance_of(DateTime, DateTime.xmlschema) + end + + def test_rfc2822 + assert_instance_of(Date, Date.rfc2822) + assert_instance_of(DateTime, DateTime.rfc2822) + assert_instance_of(Date, Date.rfc822) + assert_instance_of(DateTime, DateTime.rfc822) + end + + def test_httpdate + assert_instance_of(Date, Date.httpdate) + assert_instance_of(DateTime, DateTime.httpdate) + end + + def test_jisx0301 + assert_instance_of(Date, Date.jisx0301) + assert_instance_of(DateTime, DateTime.jisx0301) + end + +end diff --git a/test/date/test_date_strftime.rb b/test/date/test_date_strftime.rb new file mode 100644 index 0000000000..42b9734785 --- /dev/null +++ b/test/date/test_date_strftime.rb @@ -0,0 +1,374 @@ +require 'test/unit' +require 'date' + +class TestDateStrftime < Test::Unit::TestCase + + STRFTIME_2001_02_03 = { + '%A'=>['Saturday',{:wday=>6}], + '%a'=>['Sat',{:wday=>6}], + '%B'=>['February',{:mon=>2}], + '%b'=>['Feb',{:mon=>2}], + '%c'=>['Sat Feb 3 00:00:00 2001', + {:wday=>6,:mon=>2,:mday=>3,:hour=>0,:min=>0,:sec=>0,:year=>2001}], + '%d'=>['03',{:mday=>3}], + '%e'=>[' 3',{:mday=>3}], + '%H'=>['00',{:hour=>0}], + '%I'=>['12',{:hour=>0}], + '%j'=>['034',{:yday=>34}], + '%M'=>['00',{:min=>0}], + '%m'=>['02',{:mon=>2}], + '%p'=>['AM',{}], + '%S'=>['00',{:sec=>0}], + '%U'=>['04',{:wnum0=>4}], + '%W'=>['05',{:wnum1=>5}], + '%X'=>['00:00:00',{:hour=>0,:min=>0,:sec=>0}], + '%x'=>['02/03/01',{:mon=>2,:mday=>3,:year=>2001}], + '%Y'=>['2001',{:year=>2001}], + '%y'=>['01',{:year=>2001}], + '%Z'=>['+00:00',{:zone=>'+00:00',:offset=>0}], + '%%'=>['%',{}], + '%C'=>['20',{}], + '%D'=>['02/03/01',{:mon=>2,:mday=>3,:year=>2001}], + '%F'=>['2001-02-03',{:year=>2001,:mon=>2,:mday=>3}], + '%G'=>['2001',{:cwyear=>2001}], + '%g'=>['01',{:cwyear=>2001}], + '%h'=>['Feb',{:mon=>2}], + '%k'=>[' 0',{:hour=>0}], + '%L'=>['000',{:sec_fraction=>0}], + '%l'=>['12',{:hour=>0}], + '%N'=>['000000000',{:sec_fraction=>0}], + '%n'=>["\n",{}], + '%P'=>['am',{}], + '%Q'=>['981158400000',{:seconds=>981158400.to_r}], + '%R'=>['00:00',{:hour=>0,:min=>0}], + '%r'=>['12:00:00 AM',{:hour=>0,:min=>0,:sec=>0}], + '%s'=>['981158400',{:seconds=>981158400}], + '%T'=>['00:00:00',{:hour=>0,:min=>0,:sec=>0}], + '%t'=>["\t",{}], + '%u'=>['6',{:cwday=>6}], + '%V'=>['05',{:cweek=>5}], + '%v'=>[' 3-Feb-2001',{:mday=>3,:mon=>2,:year=>2001}], + '%z'=>['+0000',{:zone=>'+0000',:offset=>0}], + '%+'=>['Sat Feb 3 00:00:00 +00:00 2001', + {:wday=>6,:mon=>2,:mday=>3, + :hour=>0,:min=>0,:sec=>0,:zone=>'+00:00',:offset=>0,:year=>2001}], + } + + STRFTIME_2001_02_03_CVS19 = { + } + + STRFTIME_2001_02_03_GNUext = { + '%:z'=>['+00:00',{:zone=>'+00:00',:offset=>0}], + '%::z'=>['+00:00:00',{:zone=>'+00:00:00',:offset=>0}], + '%:::z'=>['+00',{:zone=>'+00',:offset=>0}], + } + + STRFTIME_2001_02_03.update(STRFTIME_2001_02_03_CVS19) + STRFTIME_2001_02_03.update(STRFTIME_2001_02_03_GNUext) + + def test_strftime + d = Date.new(2001,2,3) + STRFTIME_2001_02_03.each do |f, s| + assert_equal(s[0], d.strftime(f), [f, s].inspect) + case f[-1,1] + when 'c', 'C', 'x', 'X', 'y', 'Y' + f2 = f.sub(/\A%/, '%E') + assert_equal(s[0], d.strftime(f2), [f2, s].inspect) + else + f2 = f.sub(/\A%/, '%E') + assert_equal(f2, d.strftime(f2), [f2, s].inspect) + end + case f[-1,1] + when 'd', 'e', 'H', 'I', 'm', 'M', 'S', 'u', 'U', 'V', 'w', 'W', 'y' + f2 = f.sub(/\A%/, '%O') + assert_equal(s[0], d.strftime(f2), [f2, s].inspect) + else + f2 = f.sub(/\A%/, '%O') + assert_equal(f2, d.strftime(f2), [f2, s].inspect) + end + end + end + + def test_strftime__2 + d = Date.new(2001,2,3) + assert_equal('2001-02-03', d.strftime) + + d = DateTime.new(2001,2,3) + assert_equal('2001-02-03T00:00:00+00:00', d.strftime) + + assert_equal('', d.strftime('')) + assert_equal("\s"*3, d.strftime("\s"*3)) + assert_equal("\tfoo\n\000\r", d.strftime("\tfoo\n\000\r")) + assert_equal("%\n", d.strftime("%\n")) # gnu + assert_equal('Saturday'*1024 + ',', d.strftime('%A'*1024 + ',')) + assert_equal('%%', d.strftime('%%%')) + assert_equal('Anton von Webern', d.strftime('Anton von Webern')) + + d = DateTime.new(2001,2,3, 1,2,3) + assert_equal('2001-02-03T01:02:03+00:00', d.strftime) + assert_equal('AM', d.strftime('%p')) + assert_equal('am', d.strftime('%P')) + d = DateTime.new(2001,2,3, 13,14,15) + assert_equal('2001-02-03T13:14:15+00:00', d.strftime) + assert_equal('PM', d.strftime('%p')) + assert_equal('pm', d.strftime('%P')) + end + + def test_strftime__3_1 + (Date.new(1970,1,1)..Date.new(2037,12,31)).each do |d| + t = Time.utc(d.year,d.mon,d.mday) + assert_equal(t.strftime('%U'), d.strftime('%U')) + assert_equal(t.strftime('%W'), d.strftime('%W')) + end + end + + def test_strftime__3_2 + s = Time.now.strftime('%G') + if s.empty? || s == '%G' + skip_mesg + return + end + (Date.new(1970,1,1)..Date.new(2037,12,31)).each do |d| + t = Time.utc(d.year,d.mon,d.mday) + assert_equal(t.strftime('%G'), d.strftime('%G')) + assert_equal(t.strftime('%g'), d.strftime('%g')) + assert_equal(t.strftime('%V'), d.strftime('%V')) + assert_equal(t.strftime('%u'), d.strftime('%u')) + end + end + + def test_strftime__4 + s = '2006-08-08T23:15:33.123456789' + f = '%FT%T.%N' + d = DateTime.parse(s) + assert_equal(s, d.strftime(f)) + d = DateTime.strptime(s, f) + assert_equal(s, d.strftime(f)) + + s = '2006-08-08T23:15:33.123456789' + f = '%FT%T.%N' + d = DateTime.parse(s + '123456789') + assert_equal(s, d.strftime(f)) + d = DateTime.strptime(s + '123456789', f) + assert_equal(s, d.strftime(f)) + + si = '2006-08-08T23:15:33.9' + so = '2006-08-08T23:15:33.900000000' + f = '%FT%T.%N' + d = DateTime.parse(si) + assert_equal(so, d.strftime(f)) + d = DateTime.strptime(si, f) + assert_equal(so, d.strftime(f)) + + s = '2006-08-08T23:15:33.123' + f = '%FT%T.%L' + d = DateTime.parse(s) + assert_equal(s, d.strftime(f)) + d = DateTime.strptime(s, f) + assert_equal(s, d.strftime(f)) + + s = '2006-08-08T23:15:33.123' + f = '%FT%T.%L' + d = DateTime.parse(s + '123') + assert_equal(s, d.strftime(f)) + d = DateTime.strptime(s + '123', f) + assert_equal(s, d.strftime(f)) + + si = '2006-08-08T23:15:33.9' + so = '2006-08-08T23:15:33.900' + f = '%FT%T.%L' + d = DateTime.parse(si) + assert_equal(so, d.strftime(f)) + d = DateTime.strptime(si, f) + assert_equal(so, d.strftime(f)) + end + + def test_strftime__offset + s = '2006-08-08T23:15:33' + (-24..24).collect{|x| '%+.2d' % x}.each do |hh| + %w(00 30).each do |mm| + d = DateTime.parse(s + hh + mm) + assert_equal(hh + mm, d.strftime('%z')) + end + end + end + + def test_strftime__minus + d = DateTime.new(1969, 12, 31, 23, 59, 59) + assert_equal('-1', d.strftime('%s')) + assert_equal('-1000', d.strftime('%Q')) + end + + def test_strftime__gnuext # coreutils + d = DateTime.new(2006,8,8,23,15,33,9.to_r/24) + + assert_equal('2006', d.strftime('%-Y')) + assert_equal('2006', d.strftime('%-5Y')) + assert_equal('02006', d.strftime('%5Y')) + assert_equal('2006', d.strftime('%_Y')) + assert_equal(' 2006', d.strftime('%_5Y')) + assert_equal('02006', d.strftime('%05Y')) + + assert_equal('8', d.strftime('%-d')) + assert_equal('8', d.strftime('%-3d')) + assert_equal('008', d.strftime('%3d')) + assert_equal(' 8', d.strftime('%_d')) + assert_equal(' 8', d.strftime('%_3d')) + assert_equal('008', d.strftime('%03d')) + + assert_equal('8', d.strftime('%-e')) + assert_equal('8', d.strftime('%-3e')) + assert_equal(' 8', d.strftime('%3e')) + assert_equal(' 8', d.strftime('%_e')) + assert_equal(' 8', d.strftime('%_3e')) + assert_equal('008', d.strftime('%03e')) + + assert_equal('Tuesday', d.strftime('%-10A')) + assert_equal(' Tuesday', d.strftime('%10A')) + assert_equal(' Tuesday', d.strftime('%_10A')) + assert_equal('000Tuesday', d.strftime('%010A')) + assert_equal('TUESDAY', d.strftime('%^A')) + assert_equal('TUESDAY', d.strftime('%#A')) + + assert_equal('Tue', d.strftime('%-6a')) + assert_equal(' Tue', d.strftime('%6a')) + assert_equal(' Tue', d.strftime('%_6a')) + assert_equal('000Tue', d.strftime('%06a')) + assert_equal('TUE', d.strftime('%^a')) + assert_equal('TUE', d.strftime('%#a')) + assert_equal(' TUE', d.strftime('%#6a')) + + assert_equal('August', d.strftime('%-10B')) + assert_equal(' August', d.strftime('%10B')) + assert_equal(' August', d.strftime('%_10B')) + assert_equal('0000August', d.strftime('%010B')) + assert_equal('AUGUST', d.strftime('%^B')) + assert_equal('AUGUST', d.strftime('%#B')) + + assert_equal('Aug', d.strftime('%-6b')) + assert_equal(' Aug', d.strftime('%6b')) + assert_equal(' Aug', d.strftime('%_6b')) + assert_equal('000Aug', d.strftime('%06b')) + assert_equal('AUG', d.strftime('%^b')) + assert_equal('AUG', d.strftime('%#b')) + assert_equal(' AUG', d.strftime('%#6b')) + + assert_equal('Aug', d.strftime('%-6h')) + assert_equal(' Aug', d.strftime('%6h')) + assert_equal(' Aug', d.strftime('%_6h')) + assert_equal('000Aug', d.strftime('%06h')) + assert_equal('AUG', d.strftime('%^h')) + assert_equal('AUG', d.strftime('%#h')) + assert_equal(' AUG', d.strftime('%#6h')) + + assert_equal('PM', d.strftime('%^p')) + assert_equal('pm', d.strftime('%#p')) + assert_equal('PM', d.strftime('%^P')) + assert_equal('PM', d.strftime('%#P')) + + assert_equal('+000900', d.strftime('%7z')) + assert_equal(' +900', d.strftime('%_7z')) + assert_equal('+09:00', d.strftime('%:z')) + assert_equal('+0009:00', d.strftime('%8:z')) + assert_equal(' +9:00', d.strftime('%_8:z')) + assert_equal('+09:00:00', d.strftime('%::z')) + assert_equal('+0009:00:00', d.strftime('%11::z')) + assert_equal(' +9:00:00', d.strftime('%_11::z')) + assert_equal('+09', d.strftime('%:::z')) + assert_equal('+0009', d.strftime('%5:::z')) + assert_equal(' +9', d.strftime('%_5:::z')) + assert_equal('+9', d.strftime('%-:::z')) + + d = DateTime.new(-200,8,8,23,15,33,9.to_r/24) + + assert_equal('-0200', d.strftime('%Y')) + assert_equal('-200', d.strftime('%-Y')) + assert_equal('-200', d.strftime('%-5Y')) + assert_equal('-0200', d.strftime('%5Y')) + assert_equal(' -200', d.strftime('%_Y')) + assert_equal(' -200', d.strftime('%_5Y')) + assert_equal('-0200', d.strftime('%05Y')) + + d = DateTime.new(-2000,8,8,23,15,33,9.to_r/24) + + assert_equal('-2000', d.strftime('%Y')) + assert_equal('-2000', d.strftime('%-Y')) + assert_equal('-2000', d.strftime('%-5Y')) + assert_equal('-2000', d.strftime('%5Y')) + assert_equal('-2000', d.strftime('%_Y')) + assert_equal('-2000', d.strftime('%_5Y')) + assert_equal('-2000', d.strftime('%05Y')) + end + + def test_strftime__gnuext_z # coreutils + d = DateTime.parse('2006-08-08T23:15:33+09:08:07') + assert_equal('+0908', d.strftime('%z')) + assert_equal('+09:08', d.strftime('%:z')) + assert_equal('+09:08:07', d.strftime('%::z')) + assert_equal('+09:08:07', d.strftime('%:::z')) + end + + def test__different_format + d = Date.new(2001,2,3) + + assert_equal('Sat Feb 3 00:00:00 2001', d.ctime) + assert_equal(d.ctime, d.asctime) + + assert_equal('2001-02-03', d.iso8601) + assert_equal(d.rfc3339, d.iso8601) + assert_equal(d.xmlschema, d.iso8601) + assert_equal('Sat, 3 Feb 2001 00:00:00 +0000', d.rfc2822) + assert_equal(d.rfc822, d.rfc2822) + assert_equal('Sat, 03 Feb 2001 00:00:00 GMT', d.httpdate) + assert_equal('H13.02.03', d.jisx0301) + + d = DateTime.new(2001,2,3) + + assert_equal('Sat Feb 3 00:00:00 2001', d.ctime) + assert_equal(d.ctime, d.asctime) + + assert_equal('2001-02-03T00:00:00+00:00', d.iso8601) + assert_equal(d.rfc3339, d.iso8601) + assert_equal(d.xmlschema, d.iso8601) + assert_equal('Sat, 3 Feb 2001 00:00:00 +0000', d.rfc2822) + assert_equal(d.rfc822, d.rfc2822) + assert_equal('Sat, 03 Feb 2001 00:00:00 GMT', d.httpdate) + assert_equal('H13.02.03T00:00:00+00:00', d.jisx0301) + + d2 = DateTime.parse('2001-02-03T04:05:06.123456') + assert_equal('2001-02-03T04:05:06.123+00:00', d2.iso8601(3)) + assert_equal('2001-02-03T04:05:06.123+00:00', d2.rfc3339(3)) + assert_equal('H13.02.03T04:05:06.123+00:00', d2.jisx0301(3)) + assert_equal('2001-02-03T04:05:06.123456000+00:00', d2.iso8601(9)) + assert_equal('2001-02-03T04:05:06.123456000+00:00', d2.rfc3339(9)) + assert_equal('H13.02.03T04:05:06.123456000+00:00', d2.jisx0301(9)) + + assert_equal('1868-01-25', Date.parse('1868-01-25').jisx0301) + assert_equal('1872-12-31', Date.parse('1872-12-31').jisx0301) + + assert_equal('M06.01.01', Date.parse('1873-01-01').jisx0301) + assert_equal('M45.07.29', Date.parse('1912-07-29').jisx0301) + assert_equal('T01.07.30', Date.parse('1912-07-30').jisx0301) + assert_equal('T15.12.24', Date.parse('1926-12-24').jisx0301) + assert_equal('S01.12.25', Date.parse('1926-12-25').jisx0301) + assert_equal('S64.01.07', Date.parse('1989-01-07').jisx0301) + assert_equal('H01.01.08', Date.parse('1989-01-08').jisx0301) + assert_equal('H18.09.01', Date.parse('2006-09-01').jisx0301) + + %w(M06.01.01 + M45.07.29 + T01.07.30 + T15.12.24 + S01.12.25 + S64.01.07 + H01.01.08 + H18.09.01).each do |s| + assert_equal(s, Date.parse(s).jisx0301) + end + + d = Date.new(2001,2,3) + d2 = DateTime.new(2001,2,3,2) + + end + +end diff --git a/test/date/test_date_strptime.rb b/test/date/test_date_strptime.rb new file mode 100644 index 0000000000..572d1ea42a --- /dev/null +++ b/test/date/test_date_strptime.rb @@ -0,0 +1,469 @@ +require 'test/unit' +require 'date' + +class TestDateStrptime < Test::Unit::TestCase + + STRFTIME_2001_02_03 = { + '%A'=>['Saturday',{:wday=>6}], + '%a'=>['Sat',{:wday=>6}], + '%B'=>['February',{:mon=>2}], + '%b'=>['Feb',{:mon=>2}], + '%c'=>['Sat Feb 3 00:00:00 2001', + {:wday=>6,:mon=>2,:mday=>3,:hour=>0,:min=>0,:sec=>0,:year=>2001}], + '%d'=>['03',{:mday=>3}], + '%e'=>[' 3',{:mday=>3}], + '%H'=>['00',{:hour=>0}], + '%I'=>['12',{:hour=>0}], + '%j'=>['034',{:yday=>34}], + '%M'=>['00',{:min=>0}], + '%m'=>['02',{:mon=>2}], + '%p'=>['AM',{}], + '%S'=>['00',{:sec=>0}], + '%U'=>['04',{:wnum0=>4}], + '%W'=>['05',{:wnum1=>5}], + '%X'=>['00:00:00',{:hour=>0,:min=>0,:sec=>0}], + '%x'=>['02/03/01',{:mon=>2,:mday=>3,:year=>2001}], + '%Y'=>['2001',{:year=>2001}], + '%y'=>['01',{:year=>2001}], + '%Z'=>['+00:00',{:zone=>'+00:00',:offset=>0}], + '%%'=>['%',{}], + '%C'=>['20',{}], + '%D'=>['02/03/01',{:mon=>2,:mday=>3,:year=>2001}], + '%F'=>['2001-02-03',{:year=>2001,:mon=>2,:mday=>3}], + '%G'=>['2001',{:cwyear=>2001}], + '%g'=>['01',{:cwyear=>2001}], + '%h'=>['Feb',{:mon=>2}], + '%k'=>[' 0',{:hour=>0}], + '%L'=>['000',{:sec_fraction=>0}], + '%l'=>['12',{:hour=>0}], + '%N'=>['000000000',{:sec_fraction=>0}], + '%n'=>["\n",{}], + '%P'=>['am',{}], + '%Q'=>['981158400000',{:seconds=>981158400.to_r}], + '%R'=>['00:00',{:hour=>0,:min=>0}], + '%r'=>['12:00:00 AM',{:hour=>0,:min=>0,:sec=>0}], + '%s'=>['981158400',{:seconds=>981158400}], + '%T'=>['00:00:00',{:hour=>0,:min=>0,:sec=>0}], + '%t'=>["\t",{}], + '%u'=>['6',{:cwday=>6}], + '%V'=>['05',{:cweek=>5}], + '%v'=>[' 3-Feb-2001',{:mday=>3,:mon=>2,:year=>2001}], + '%z'=>['+0000',{:zone=>'+0000',:offset=>0}], + '%+'=>['Sat Feb 3 00:00:00 +00:00 2001', + {:wday=>6,:mon=>2,:mday=>3, + :hour=>0,:min=>0,:sec=>0,:zone=>'+00:00',:offset=>0,:year=>2001}], + } + + STRFTIME_2001_02_03_CVS19 = { + } + + STRFTIME_2001_02_03_GNUext = { + '%:z'=>['+00:00',{:zone=>'+00:00',:offset=>0}], + '%::z'=>['+00:00:00',{:zone=>'+00:00:00',:offset=>0}], + '%:::z'=>['+00',{:zone=>'+00',:offset=>0}], + } + + STRFTIME_2001_02_03.update(STRFTIME_2001_02_03_CVS19) + STRFTIME_2001_02_03.update(STRFTIME_2001_02_03_GNUext) + + def test__strptime + STRFTIME_2001_02_03.each do |f, s| + if (f == '%I' and s[0] == '12') or + (f == '%l' and s[0] == '12') # hour w/o merid + s[1][:hour] = 12 + end + assert_equal(s[1], Date._strptime(s[0], f), [f, s].inspect) + case f[-1,1] + when 'c', 'C', 'x', 'X', 'y', 'Y' + f2 = f.sub(/\A%/, '%E') + assert_equal(s[1], Date._strptime(s[0], f2), [f2, s].inspect) + else + f2 = f.sub(/\A%/, '%E') + assert_equal(nil, Date._strptime(s[0], f2), [f2, s].inspect) + assert_equal({}, Date._strptime(f2, f2), [f2, s].inspect) + end + case f[-1,1] + when 'd', 'e', 'H', 'I', 'm', 'M', 'S', 'u', 'U', 'V', 'w', 'W', 'y' + f2 = f.sub(/\A%/, '%O') + assert_equal(s[1], Date._strptime(s[0], f2), [f2, s].inspect) + else + f2 = f.sub(/\A%/, '%O') + assert_equal(nil, Date._strptime(s[0], f2), [f2, s].inspect) + assert_equal({}, Date._strptime(f2, f2), [f2, s].inspect) + end + end + end + + def test__strptime__2 + h = Date._strptime('2001-02-03') + assert_equal([2001,2,3], h.values_at(:year,:mon,:mday)) + + h = DateTime._strptime('2001-02-03T12:13:14Z') + assert_equal([2001,2,3,12,13,14], + h.values_at(:year,:mon,:mday,:hour,:min,:sec)) + + assert_equal({}, Date._strptime('', '')) + assert_equal({:leftover=>"\s"*3}, Date._strptime("\s"*3, '')) + assert_equal({:leftover=>'x'}, Date._strptime("\nx", "\n")) + assert_equal({}, Date._strptime('', "\s"*3)) + assert_equal({}, Date._strptime("\s"*3, "\s"*3)) + assert_equal({}, Date._strptime("\tfoo\n\000\r", "\tfoo\n\000\r")) + assert_equal({}, Date._strptime("foo\n\nbar", "foo\sbar")) + assert_equal({}, Date._strptime("%\n", "%\n")) # gnu + assert_equal({}, Date._strptime('%%', '%%%')) + assert_equal({:wday=>6}, Date._strptime('Saturday'*1024 + ',', '%A'*1024 + ',')) + assert_equal({:wday=>6}, Date._strptime('Saturday'*1024 + ',', '%a'*1024 + ',')) + assert_equal({}, Date._strptime('Anton von Webern', 'Anton von Webern')) + end + + def test__strptime__3 + [ + # iso8601 + [['2001-02-03', '%Y-%m-%d'], [2001,2,3,nil,nil,nil,nil,nil,nil]], + [['2001-02-03T23:59:60', '%Y-%m-%dT%H:%M:%S'], [2001,2,3,23,59,60,nil,nil,nil]], + [['2001-02-03T23:59:60+09:00', '%Y-%m-%dT%H:%M:%S%Z'], [2001,2,3,23,59,60,'+09:00',9*3600,nil]], + [['-2001-02-03T23:59:60+09:00', '%Y-%m-%dT%H:%M:%S%Z'], [-2001,2,3,23,59,60,'+09:00',9*3600,nil]], + [['+012345-02-03T23:59:60+09:00', '%Y-%m-%dT%H:%M:%S%Z'], [12345,2,3,23,59,60,'+09:00',9*3600,nil]], + [['-012345-02-03T23:59:60+09:00', '%Y-%m-%dT%H:%M:%S%Z'], [-12345,2,3,23,59,60,'+09:00',9*3600,nil]], + + # ctime(3), asctime(3) + [['Thu Jul 29 14:47:19 1999', '%c'], [1999,7,29,14,47,19,nil,nil,4]], + [['Thu Jul 29 14:47:19 -1999', '%c'], [-1999,7,29,14,47,19,nil,nil,4]], + + # date(1) + [['Thu Jul 29 16:39:41 EST 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'EST',-5*3600,4]], + [['Thu Jul 29 16:39:41 MET DST 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'MET DST',2*3600,4]], + [['Thu Jul 29 16:39:41 AMT 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'AMT',nil,4]], + [['Thu Jul 29 16:39:41 AMT -1999', '%a %b %d %H:%M:%S %Z %Y'], [-1999,7,29,16,39,41,'AMT',nil,4]], + [['Thu Jul 29 16:39:41 GMT+09 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT+09',9*3600,4]], + [['Thu Jul 29 16:39:41 GMT+0908 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT+0908',9*3600+8*60,4]], + [['Thu Jul 29 16:39:41 GMT+090807 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT+090807',9*3600+8*60+7,4]], + [['Thu Jul 29 16:39:41 GMT-09 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT-09',-9*3600,4]], + [['Thu Jul 29 16:39:41 GMT-09:08 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT-09:08',-9*3600-8*60,4]], + [['Thu Jul 29 16:39:41 GMT-09:08:07 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT-09:08:07',-9*3600-8*60-7,4]], + [['Thu Jul 29 16:39:41 GMT-3.5 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT-3.5',-3*3600-30*60,4]], + [['Thu Jul 29 16:39:41 GMT-3,5 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT-3,5',-3*3600-30*60,4]], + [['Thu Jul 29 16:39:41 Mountain Daylight Time 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'Mountain Daylight Time',-6*3600,4]], + [['Thu Jul 29 16:39:41 E. Australia Standard Time 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'E. Australia Standard Time',10*3600,4]], + + # rfc822 + [['Thu, 29 Jul 1999 09:54:21 UT', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'UT',0,4]], + [['Thu, 29 Jul 1999 09:54:21 GMT', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'GMT',0,4]], + [['Thu, 29 Jul 1999 09:54:21 PDT', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'PDT',-7*3600,4]], + [['Thu, 29 Jul 1999 09:54:21 z', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'z',0,4]], + [['Thu, 29 Jul 1999 09:54:21 +0900', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'+0900',9*3600,4]], + [['Thu, 29 Jul 1999 09:54:21 +0430', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'+0430',4*3600+30*60,4]], + [['Thu, 29 Jul 1999 09:54:21 -0430', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'-0430',-4*3600-30*60,4]], + [['Thu, 29 Jul -1999 09:54:21 -0430', '%a, %d %b %Y %H:%M:%S %Z'], [-1999,7,29,9,54,21,'-0430',-4*3600-30*60,4]], + + # etc + [['06-DEC-99', '%d-%b-%y'], [1999,12,6,nil,nil,nil,nil,nil,nil]], + [['sUnDay oCtoBer 31 01', '%A %B %d %y'], [2001,10,31,nil,nil,nil,nil,nil,0]], + [["October\t\n\v\f\r 15,\t\n\v\f\r99", '%B %d, %y'], [1999,10,15,nil,nil,nil,nil,nil,nil]], + [["October\t\n\v\f\r 15,\t\n\v\f\r99", '%B%t%d,%n%y'], [1999,10,15,nil,nil,nil,nil,nil,nil]], + + [['09:02:11 AM', '%I:%M:%S %p'], [nil,nil,nil,9,2,11,nil,nil,nil]], + [['09:02:11 A.M.', '%I:%M:%S %p'], [nil,nil,nil,9,2,11,nil,nil,nil]], + [['09:02:11 PM', '%I:%M:%S %p'], [nil,nil,nil,21,2,11,nil,nil,nil]], + [['09:02:11 P.M.', '%I:%M:%S %p'], [nil,nil,nil,21,2,11,nil,nil,nil]], + + [['12:33:44 AM', '%r'], [nil,nil,nil,0,33,44,nil,nil,nil]], + [['01:33:44 AM', '%r'], [nil,nil,nil,1,33,44,nil,nil,nil]], + [['11:33:44 AM', '%r'], [nil,nil,nil,11,33,44,nil,nil,nil]], + [['12:33:44 PM', '%r'], [nil,nil,nil,12,33,44,nil,nil,nil]], + [['01:33:44 PM', '%r'], [nil,nil,nil,13,33,44,nil,nil,nil]], + [['11:33:44 PM', '%r'], [nil,nil,nil,23,33,44,nil,nil,nil]], + + [['11:33:44 PM AMT', '%I:%M:%S %p %Z'], [nil,nil,nil,23,33,44,'AMT',nil,nil]], + [['11:33:44 P.M. AMT', '%I:%M:%S %p %Z'], [nil,nil,nil,23,33,44,'AMT',nil,nil]], + + [['fri1feb034pm+5', '%a%d%b%y%H%p%Z'], [2003,2,1,16,nil,nil,'+5',5*3600,5]] + ].each do |x, y| + h = Date._strptime(*x) + a = h.values_at(:year,:mon,:mday,:hour,:min,:sec,:zone,:offset,:wday) + if y[1] == -1 + a[1] = -1 + a[2] = h[:yday] + end + assert_equal(y, a, [x, y, a].inspect) + end + end + + def test__strptime__width + [ + [['99', '%y'], [1999,nil,nil,nil,nil,nil,nil,nil,nil]], + [['01', '%y'], [2001,nil,nil,nil,nil,nil,nil,nil,nil]], + [['19 99', '%C %y'], [1999,nil,nil,nil,nil,nil,nil,nil,nil]], + [['20 01', '%C %y'], [2001,nil,nil,nil,nil,nil,nil,nil,nil]], + [['1999', '%C%y'], [1999,nil,nil,nil,nil,nil,nil,nil,nil]], + [['2001', '%C%y'], [2001,nil,nil,nil,nil,nil,nil,nil,nil]], + + [['20060806', '%Y'], [20060806,nil,nil,nil,nil,nil,nil,nil,nil]], + [['20060806', "%Y\s"], [20060806,nil,nil,nil,nil,nil,nil,nil,nil]], + [['20060806', '%Y%m%d'], [2006,8,6,nil,nil,nil,nil,nil,nil]], + [['2006908906', '%Y9%m9%d'], [2006,8,6,nil,nil,nil,nil,nil,nil]], + [['12006 08 06', '%Y %m %d'], [12006,8,6,nil,nil,nil,nil,nil,nil]], + [['12006-08-06', '%Y-%m-%d'], [12006,8,6,nil,nil,nil,nil,nil,nil]], + [['200608 6', '%Y%m%e'], [2006,8,6,nil,nil,nil,nil,nil,nil]], + + [['2006333', '%Y%j'], [2006,-1,333,nil,nil,nil,nil,nil,nil]], + [['20069333', '%Y9%j'], [2006,-1,333,nil,nil,nil,nil,nil,nil]], + [['12006 333', '%Y %j'], [12006,-1,333,nil,nil,nil,nil,nil,nil]], + [['12006-333', '%Y-%j'], [12006,-1,333,nil,nil,nil,nil,nil,nil]], + + [['232425', '%H%M%S'], [nil,nil,nil,23,24,25,nil,nil,nil]], + [['23924925', '%H9%M9%S'], [nil,nil,nil,23,24,25,nil,nil,nil]], + [['23 24 25', '%H %M %S'], [nil,nil,nil,23,24,25,nil,nil,nil]], + [['23:24:25', '%H:%M:%S'], [nil,nil,nil,23,24,25,nil,nil,nil]], + [[' 32425', '%k%M%S'], [nil,nil,nil,3,24,25,nil,nil,nil]], + [[' 32425', '%l%M%S'], [nil,nil,nil,3,24,25,nil,nil,nil]], + + [['FriAug', '%a%b'], [nil,8,nil,nil,nil,nil,nil,nil,5]], + [['FriAug', '%A%B'], [nil,8,nil,nil,nil,nil,nil,nil,5]], + [['FridayAugust', '%A%B'], [nil,8,nil,nil,nil,nil,nil,nil,5]], + [['FridayAugust', '%a%b'], [nil,8,nil,nil,nil,nil,nil,nil,5]] + ].each do |x, y| + h = Date._strptime(*x) + a = h.values_at(:year,:mon,:mday,:hour,:min,:sec,:zone,:offset,:wday) + if y[1] == -1 + a[1] = -1 + a[2] = h[:yday] + end + assert_equal(y, a, [x, y, a].inspect) + end + end + + def test__strptime__fail + assert_not_nil(Date._strptime('2001.', '%Y.')) + assert_not_nil(Date._strptime("2001.\s", '%Y.')) + assert_not_nil(Date._strptime('2001.', "%Y.\s")) + assert_not_nil(Date._strptime("2001.\s", "%Y.\s")) + + assert_nil(Date._strptime('2001', '%Y.')) + assert_nil(Date._strptime("2001\s", '%Y.')) + assert_nil(Date._strptime('2001', "%Y.\s")) + assert_nil(Date._strptime("2001\s", "%Y.\s")) + + assert_nil(Date._strptime('2001-13-31', '%Y-%m-%d')) + assert_nil(Date._strptime('2001-12-00', '%Y-%m-%d')) + assert_nil(Date._strptime('2001-12-32', '%Y-%m-%d')) + assert_nil(Date._strptime('2001-12-00', '%Y-%m-%e')) + assert_nil(Date._strptime('2001-12-32', '%Y-%m-%e')) + assert_nil(Date._strptime('2001-12-31', '%y-%m-%d')) + + assert_nil(Date._strptime('2004-000', '%Y-%j')) + assert_nil(Date._strptime('2004-367', '%Y-%j')) + assert_nil(Date._strptime('2004-366', '%y-%j')) + + assert_not_nil(Date._strptime('24:59:59', '%H:%M:%S')) + assert_not_nil(Date._strptime('24:59:59', '%k:%M:%S')) + assert_not_nil(Date._strptime('24:59:60', '%H:%M:%S')) + assert_not_nil(Date._strptime('24:59:60', '%k:%M:%S')) + + assert_nil(Date._strptime('24:60:59', '%H:%M:%S')) + assert_nil(Date._strptime('24:60:59', '%k:%M:%S')) + assert_nil(Date._strptime('24:59:61', '%H:%M:%S')) + assert_nil(Date._strptime('24:59:61', '%k:%M:%S')) + assert_nil(Date._strptime('00:59:59', '%I:%M:%S')) + assert_nil(Date._strptime('13:59:59', '%I:%M:%S')) + assert_nil(Date._strptime('00:59:59', '%l:%M:%S')) + assert_nil(Date._strptime('13:59:59', '%l:%M:%S')) + + assert_not_nil(Date._strptime('0', '%U')) + assert_nil(Date._strptime('54', '%U')) + assert_not_nil(Date._strptime('0', '%W')) + assert_nil(Date._strptime('54', '%W')) + assert_nil(Date._strptime('0', '%V')) + assert_nil(Date._strptime('54', '%V')) + assert_nil(Date._strptime('0', '%u')) + assert_not_nil(Date._strptime('7', '%u')) + assert_not_nil(Date._strptime('0', '%w')) + assert_nil(Date._strptime('7', '%w')) + + assert_nil(Date._strptime('Sanday', '%A')) + assert_nil(Date._strptime('Jenuary', '%B')) + assert_not_nil(Date._strptime('Sundai', '%A')) + assert_not_nil(Date._strptime('Januari', '%B')) + assert_nil(Date._strptime('Sundai,', '%A,')) + assert_nil(Date._strptime('Januari,', '%B,')) + end + + def test_strptime + assert_equal(Date.new, Date.strptime) + d = Date.new(2002,3,14) + assert_equal(d, Date.strptime(d.to_s)) + assert_equal(Date.new(2002,3,14), Date.strptime('2002-03-14')) + + d = DateTime.new(2002,3,14,11,22,33, 0) + assert_equal(d, DateTime.strptime(d.to_s)) + assert_equal(DateTime.new(2002,3,14,11,22,33, 0), + DateTime.strptime('2002-03-14T11:22:33Z')) + assert_equal(DateTime.new(2002,3,14,11,22,33, 0), + DateTime.strptime('2002-03-14T11:22:33Z', '%Y-%m-%dT%H:%M:%S%Z')) + assert_equal(DateTime.new(2002,3,14,11,22,33, 9.to_r/24), + DateTime.strptime('2002-03-14T11:22:33+09:00', '%Y-%m-%dT%H:%M:%S%Z')) + assert_equal(DateTime.new(2002,3,14,11,22,33, -9.to_r/24), + DateTime.strptime('2002-03-14T11:22:33-09:00', '%FT%T%Z')) + assert_equal(DateTime.new(2002,3,14,11,22,33, -9.to_r/24) + 123456789.to_r/1000000000/86400, + DateTime.strptime('2002-03-14T11:22:33.123456789-09:00', '%FT%T.%N%Z')) + end + + def test_strptime__2 + n = 10**9 + (Date.new(2006,6,1)..Date.new(2007,6,1)).each do |d| + [ + '%Y %m %d', + '%C %y %m %d', + + '%Y %j', + '%C %y %j', + + '%G %V %w', + '%G %V %u', + '%C %g %V %w', + '%C %g %V %u', + + '%Y %U %w', + '%Y %U %u', + '%Y %W %w', + '%Y %W %u', + '%C %y %U %w', + '%C %y %U %u', + '%C %y %W %w', + '%C %y %W %u', + ].each do |fmt| + s = d.strftime(fmt) + d2 = Date.strptime(s, fmt) + assert_equal(d, d2, [fmt, d.to_s, d2.to_s].inspect) + end + + [ + '%Y %m %d %H %M %S', + '%Y %m %d %H %M %S %N', + '%C %y %m %d %H %M %S', + '%C %y %m %d %H %M %S %N', + + '%Y %j %H %M %S', + '%Y %j %H %M %S %N', + '%C %y %j %H %M %S', + '%C %y %j %H %M %S %N', + + '%s', + '%s %N', + '%Q', + '%Q %N', + ].each do |fmt| + s = d.strftime(fmt) + d2 = DateTime.strptime(s, fmt) + assert_equal(d, d2, [fmt, d.to_s, d2.to_s].inspect) + end + end + end + + def test_strptime__minus + d = DateTime.strptime('-1', '%s') + assert_equal([1969, 12, 31, 23, 59, 59], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + d = DateTime.strptime('-86400', '%s') + assert_equal([1969, 12, 31, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + + d = DateTime.strptime('-999', '%Q') + assert_equal([1969, 12, 31, 23, 59, 59, 1.to_r/10**3], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction]) + d = DateTime.strptime('-1000', '%Q') + assert_equal([1969, 12, 31, 23, 59, 59, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction]) + end + + def test_strptime__comp + n = DateTime.now + + d = DateTime.strptime('073', '%j') + assert_equal([n.year, 73, 0, 0, 0], + [d.year, d.yday, d.hour, d.min, d.sec]) + d = DateTime.strptime('13', '%d') + assert_equal([n.year, n.mon, 13, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + + d = DateTime.strptime('Mar', '%b') + assert_equal([n.year, 3, 1, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + d = DateTime.strptime('2004', '%Y') + assert_equal([2004, 1, 1, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + + d = DateTime.strptime('Mar 13', '%b %d') + assert_equal([n.year, 3, 13, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + d = DateTime.strptime('Mar 2004', '%b %Y') + assert_equal([2004, 3, 1, 0, 0, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + d = DateTime.strptime('23:55', '%H:%M') + assert_equal([n.year, n.mon, n.mday, 23, 55, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + d = DateTime.strptime('23:55:30', '%H:%M:%S') + assert_equal([n.year, n.mon, n.mday, 23, 55, 30], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + + d = DateTime.strptime('Sun 23:55', '%a %H:%M') + d2 = d - d.wday + assert_equal([d2.year, d2.mon, d2.mday, 23, 55, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + d = DateTime.strptime('Aug 23:55', '%b %H:%M') + assert_equal([n.year, 8, 1, 23, 55, 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec]) + + d = DateTime.strptime('2004', '%G') + assert_equal([2004, 1, 1, 0, 0, 0], + [d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec]) + d = DateTime.strptime('11', '%V') + assert_equal([n.cwyear, 11, 1, 0, 0, 0], + [d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec]) + d = DateTime.strptime('6', '%u') + assert_equal([n.cwyear, n.cweek, 6, 0, 0, 0], + [d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec]) + + d = DateTime.strptime('11-6', '%V-%u') + assert_equal([n.cwyear, 11, 6, 0, 0, 0], + [d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec]) + d = DateTime.strptime('2004-11', '%G-%V') + assert_equal([2004, 11, 1, 0, 0, 0], + [d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec]) + + d = DateTime.strptime('11-6', '%U-%w') + assert_equal([n.year, 11, 6, 0, 0, 0], + [d.year, d.strftime('%U').to_i, d.wday, d.hour, d.min, d.sec]) + d = DateTime.strptime('2004-11', '%Y-%U') + assert_equal([2004, 11, 0, 0, 0, 0], + [d.year, d.strftime('%U').to_i, d.wday, d.hour, d.min, d.sec]) + + d = DateTime.strptime('11-6', '%W-%w') + assert_equal([n.year, 11, 6, 0, 0, 0], + [d.year, d.strftime('%W').to_i, d.wday, d.hour, d.min, d.sec]) + d = DateTime.strptime('2004-11', '%Y-%W') + assert_equal([2004, 11, 0, 0, 0, 0], + [d.year, d.strftime('%W').to_i, d.wday, d.hour, d.min, d.sec]) + end + + def test_strptime__d_to_s + d = Date.new(2002,3,14) + assert_equal(d, Date.strptime(d.to_s)) + + d = DateTime.new(2002,3,14,11,22,33, 9.to_r/24) + assert_equal(d, DateTime.strptime(d.to_s)) + end + + def test_strptime__ex + assert_raise(ArgumentError) do + Date.strptime('2001-02-29', '%F') + end + assert_raise(ArgumentError) do + DateTime.strptime('2001-02-29T23:59:60', '%FT%T') + end + assert_raise(ArgumentError) do + Date.strptime('23:55', '%H:%M') + end + end + +end