ruby-changes:33719
From: akr <ko1@a...>
Date: Sun, 4 May 2014 03:12:02 +0900 (JST)
Subject: [ruby-changes:33719] akr:r45800 (trunk): * lib/time.rb (Time.rfc2822): Fix year completion.
akr 2014-05-04 03:11:53 +0900 (Sun, 04 May 2014) New Revision: 45800 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45800 Log: * lib/time.rb (Time.rfc2822): Fix year completion. Produce fixed-offset time object if appropriate. (Time.xmlschema): Produce fixed-offset time object if appropriate. Modified files: trunk/ChangeLog trunk/lib/time.rb trunk/test/test_time.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 45799) +++ ChangeLog (revision 45800) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sun May 4 02:53:17 2014 Tanaka Akira <akr@f...> + + * lib/time.rb (Time.rfc2822): Fix year completion. + Produce fixed-offset time object if appropriate. + (Time.xmlschema): Produce fixed-offset time object if appropriate. + Sat May 3 23:52:20 2014 Tanaka Akira <akr@f...> * lib/time.rb (make_time): Produce fixed-offset time object if Index: lib/time.rb =================================================================== --- lib/time.rb (revision 45799) +++ lib/time.rb (revision 45800) @@ -445,24 +445,26 @@ class Time https://github.com/ruby/ruby/blob/trunk/lib/time.rb#L445 day = $1.to_i mon = MonthValue[$2.upcase] year = $3.to_i + short_year_p = $3.length <= 3 hour = $4.to_i min = $5.to_i sec = $6 ? $6.to_i : 0 zone = $7 - # following year completion is compliant with RFC 2822. - year = if year < 50 - 2000 + year - elsif year < 1000 - 1900 + year - else - year - end + if short_year_p + # following year completion is compliant with RFC 2822. + year = if year < 50 + 2000 + year + else + 1900 + year + end + end + off = zone_offset(zone) year, mon, day, hour, min, sec = - apply_offset(year, mon, day, hour, min, sec, zone_offset(zone)) + apply_offset(year, mon, day, hour, min, sec, off) t = self.utc(year, mon, day, hour, min, sec) - t.localtime if !zone_utc?(zone) + t.localtime(off) if !zone_utc?(zone) t else raise ArgumentError.new("not RFC 2822 compliant date: #{date.inspect}") @@ -550,9 +552,12 @@ class Time https://github.com/ruby/ruby/blob/trunk/lib/time.rb#L552 end if $8 zone = $8 + off = zone_offset(zone) year, mon, day, hour, min, sec = - apply_offset(year, mon, day, hour, min, sec, zone_offset(zone)) - self.utc(year, mon, day, hour, min, sec, usec) + apply_offset(year, mon, day, hour, min, sec, off) + t = self.utc(year, mon, day, hour, min, sec, usec) + t.localtime(off) if !zone_utc?(zone) + t else self.local(year, mon, day, hour, min, sec, usec) end Index: test/test_time.rb =================================================================== --- test/test_time.rb (revision 45799) +++ test/test_time.rb (revision 45800) @@ -4,46 +4,61 @@ require_relative 'ruby/envutil.rb' https://github.com/ruby/ruby/blob/trunk/test/test_time.rb#L4 class TestTimeExtension < Test::Unit::TestCase # :nodoc: def test_rfc822 - assert_equal(Time.utc(1976, 8, 26, 14, 30) + 4 * 3600, - Time.rfc2822("26 Aug 76 14:30 EDT")) - assert_equal(Time.utc(1976, 8, 27, 9, 32) + 7 * 3600, - Time.rfc2822("27 Aug 76 09:32 PDT")) + t = Time.rfc2822("26 Aug 76 14:30 EDT") + assert_equal(Time.utc(1976, 8, 26, 14, 30) + 4 * 3600, t) + assert_equal(-4 * 3600, t.utc_offset) + t = Time.rfc2822("27 Aug 76 09:32 PDT") + assert_equal(Time.utc(1976, 8, 27, 9, 32) + 7 * 3600, t) + assert_equal(-7 * 3600, t.utc_offset) end def test_rfc2822 - assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600, - Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600")) - assert_equal(Time.utc(2003, 7, 1, 10, 52, 37) - 2 * 3600, - Time.rfc2822("Tue, 1 Jul 2003 10:52:37 +0200")) - assert_equal(Time.utc(1997, 11, 21, 10, 1, 10) + 6 * 3600, - Time.rfc2822("Fri, 21 Nov 1997 10:01:10 -0600")) - assert_equal(Time.utc(1997, 11, 21, 11, 0, 0) + 6 * 3600, - Time.rfc2822("Fri, 21 Nov 1997 11:00:00 -0600")) - assert_equal(Time.utc(1997, 11, 24, 14, 22, 1) + 8 * 3600, - Time.rfc2822("Mon, 24 Nov 1997 14:22:01 -0800")) + t = Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600") + assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600, t) + assert_equal(-6 * 3600, t.utc_offset) + t = Time.rfc2822("Tue, 1 Jul 2003 10:52:37 +0200") + assert_equal(Time.utc(2003, 7, 1, 10, 52, 37) - 2 * 3600, t) + assert_equal(2 * 3600, t.utc_offset) + t = Time.rfc2822("Fri, 21 Nov 1997 10:01:10 -0600") + assert_equal(Time.utc(1997, 11, 21, 10, 1, 10) + 6 * 3600, t) + assert_equal(-6 * 3600, t.utc_offset) + t = Time.rfc2822("Fri, 21 Nov 1997 11:00:00 -0600") + assert_equal(Time.utc(1997, 11, 21, 11, 0, 0) + 6 * 3600, t) + assert_equal(-6 * 3600, t.utc_offset) + t = Time.rfc2822("Mon, 24 Nov 1997 14:22:01 -0800") + assert_equal(Time.utc(1997, 11, 24, 14, 22, 1) + 8 * 3600, t) + assert_equal(-8 * 3600, t.utc_offset) begin Time.at(-1) rescue ArgumentError # ignore else - assert_equal(Time.utc(1969, 2, 13, 23, 32, 54) + 3 * 3600 + 30 * 60, - Time.rfc2822("Thu, 13 Feb 1969 23:32:54 -0330")) - assert_equal(Time.utc(1969, 2, 13, 23, 32, 0) + 3 * 3600 + 30 * 60, - Time.rfc2822(" Thu, + t = Time.rfc2822("Thu, 13 Feb 1969 23:32:54 -0330") + assert_equal(Time.utc(1969, 2, 13, 23, 32, 54) + 3 * 3600 + 30 * 60, t) + assert_equal(-3 * 3600 - 30 * 60, t.utc_offset) + t = Time.rfc2822(" Thu, 13 Feb 1969 23:32 - -0330 (Newfoundland Time)")) + -0330 (Newfoundland Time)") + assert_equal(Time.utc(1969, 2, 13, 23, 32, 0) + 3 * 3600 + 30 * 60, t) + assert_equal(-3 * 3600 - 30 * 60, t.utc_offset) end - assert_equal(Time.utc(1997, 11, 21, 9, 55, 6), - Time.rfc2822("21 Nov 97 09:55:06 GMT")) - assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600, - Time.rfc2822("Fri, 21 Nov 1997 09 : 55 : 06 -0600")) + t = Time.rfc2822("21 Nov 97 09:55:06 GMT") + assert_equal(Time.utc(1997, 11, 21, 9, 55, 6), t) + assert_equal(0, t.utc_offset) + t = Time.rfc2822("Fri, 21 Nov 1997 09 : 55 : 06 -0600") + assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600, t) + assert_equal(-6 * 3600, t.utc_offset) assert_raise(ArgumentError) { # inner comment is not supported. Time.rfc2822("Fri, 21 Nov 1997 09(comment): 55 : 06 -0600") } + t = Time.rfc2822("Mon, 01 Jan 0001 00:00:00 -0000") + assert_equal(Time.utc(1, 1, 1, 0, 0, 0), t) + assert_equal(0, t.utc_offset) + assert_equal(true, t.utc?) end def test_encode_rfc2822 @@ -389,8 +404,13 @@ class TestTimeExtension < Test::Unit::Te https://github.com/ruby/ruby/blob/trunk/test/test_time.rb#L404 def test_ruby_talk_152866 t = Time::xmlschema('2005-08-30T22:48:00-07:00') + assert_equal(30, t.day) + assert_equal(8, t.mon) + assert_equal(-7*3600, t.utc_offset) + t.utc assert_equal(31, t.day) assert_equal(8, t.mon) + assert_equal(0, t.utc_offset) end def test_parse_fraction -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/