* lib/time.rb (Time.rfc2822): Fix year completion.
Produce fixed-offset time object if appropriate. (Time.xmlschema): Produce fixed-offset time object if appropriate. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45800 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
f5c91e5775
commit
af56a2b6dc
@ -1,3 +1,9 @@
|
|||||||
|
Sun May 4 02:53:17 2014 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* 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@fsij.org>
|
Sat May 3 23:52:20 2014 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* lib/time.rb (make_time): Produce fixed-offset time object if
|
* lib/time.rb (make_time): Produce fixed-offset time object if
|
||||||
|
19
lib/time.rb
19
lib/time.rb
@ -445,24 +445,26 @@ class Time
|
|||||||
day = $1.to_i
|
day = $1.to_i
|
||||||
mon = MonthValue[$2.upcase]
|
mon = MonthValue[$2.upcase]
|
||||||
year = $3.to_i
|
year = $3.to_i
|
||||||
|
short_year_p = $3.length <= 3
|
||||||
hour = $4.to_i
|
hour = $4.to_i
|
||||||
min = $5.to_i
|
min = $5.to_i
|
||||||
sec = $6 ? $6.to_i : 0
|
sec = $6 ? $6.to_i : 0
|
||||||
zone = $7
|
zone = $7
|
||||||
|
|
||||||
|
if short_year_p
|
||||||
# following year completion is compliant with RFC 2822.
|
# following year completion is compliant with RFC 2822.
|
||||||
year = if year < 50
|
year = if year < 50
|
||||||
2000 + year
|
2000 + year
|
||||||
elsif year < 1000
|
|
||||||
1900 + year
|
|
||||||
else
|
else
|
||||||
year
|
1900 + year
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
off = zone_offset(zone)
|
||||||
year, mon, day, hour, min, sec =
|
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 = self.utc(year, mon, day, hour, min, sec)
|
||||||
t.localtime if !zone_utc?(zone)
|
t.localtime(off) if !zone_utc?(zone)
|
||||||
t
|
t
|
||||||
else
|
else
|
||||||
raise ArgumentError.new("not RFC 2822 compliant date: #{date.inspect}")
|
raise ArgumentError.new("not RFC 2822 compliant date: #{date.inspect}")
|
||||||
@ -550,9 +552,12 @@ class Time
|
|||||||
end
|
end
|
||||||
if $8
|
if $8
|
||||||
zone = $8
|
zone = $8
|
||||||
|
off = zone_offset(zone)
|
||||||
year, mon, day, hour, min, sec =
|
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)
|
||||||
self.utc(year, mon, day, hour, min, sec, usec)
|
t = self.utc(year, mon, day, hour, min, sec, usec)
|
||||||
|
t.localtime(off) if !zone_utc?(zone)
|
||||||
|
t
|
||||||
else
|
else
|
||||||
self.local(year, mon, day, hour, min, sec, usec)
|
self.local(year, mon, day, hour, min, sec, usec)
|
||||||
end
|
end
|
||||||
|
@ -4,46 +4,61 @@ require_relative 'ruby/envutil.rb'
|
|||||||
|
|
||||||
class TestTimeExtension < Test::Unit::TestCase # :nodoc:
|
class TestTimeExtension < Test::Unit::TestCase # :nodoc:
|
||||||
def test_rfc822
|
def test_rfc822
|
||||||
assert_equal(Time.utc(1976, 8, 26, 14, 30) + 4 * 3600,
|
t = Time.rfc2822("26 Aug 76 14:30 EDT")
|
||||||
Time.rfc2822("26 Aug 76 14:30 EDT"))
|
assert_equal(Time.utc(1976, 8, 26, 14, 30) + 4 * 3600, t)
|
||||||
assert_equal(Time.utc(1976, 8, 27, 9, 32) + 7 * 3600,
|
assert_equal(-4 * 3600, t.utc_offset)
|
||||||
Time.rfc2822("27 Aug 76 09:32 PDT"))
|
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
|
end
|
||||||
|
|
||||||
def test_rfc2822
|
def test_rfc2822
|
||||||
assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600,
|
t = Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600")
|
||||||
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(Time.utc(2003, 7, 1, 10, 52, 37) - 2 * 3600,
|
assert_equal(-6 * 3600, t.utc_offset)
|
||||||
Time.rfc2822("Tue, 1 Jul 2003 10:52:37 +0200"))
|
t = Time.rfc2822("Tue, 1 Jul 2003 10:52:37 +0200")
|
||||||
assert_equal(Time.utc(1997, 11, 21, 10, 1, 10) + 6 * 3600,
|
assert_equal(Time.utc(2003, 7, 1, 10, 52, 37) - 2 * 3600, t)
|
||||||
Time.rfc2822("Fri, 21 Nov 1997 10:01:10 -0600"))
|
assert_equal(2 * 3600, t.utc_offset)
|
||||||
assert_equal(Time.utc(1997, 11, 21, 11, 0, 0) + 6 * 3600,
|
t = Time.rfc2822("Fri, 21 Nov 1997 10:01:10 -0600")
|
||||||
Time.rfc2822("Fri, 21 Nov 1997 11:00:00 -0600"))
|
assert_equal(Time.utc(1997, 11, 21, 10, 1, 10) + 6 * 3600, t)
|
||||||
assert_equal(Time.utc(1997, 11, 24, 14, 22, 1) + 8 * 3600,
|
assert_equal(-6 * 3600, t.utc_offset)
|
||||||
Time.rfc2822("Mon, 24 Nov 1997 14:22:01 -0800"))
|
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
|
begin
|
||||||
Time.at(-1)
|
Time.at(-1)
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
# ignore
|
# ignore
|
||||||
else
|
else
|
||||||
assert_equal(Time.utc(1969, 2, 13, 23, 32, 54) + 3 * 3600 + 30 * 60,
|
t = Time.rfc2822("Thu, 13 Feb 1969 23:32:54 -0330")
|
||||||
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(Time.utc(1969, 2, 13, 23, 32, 0) + 3 * 3600 + 30 * 60,
|
assert_equal(-3 * 3600 - 30 * 60, t.utc_offset)
|
||||||
Time.rfc2822(" Thu,
|
t = Time.rfc2822(" Thu,
|
||||||
13
|
13
|
||||||
Feb
|
Feb
|
||||||
1969
|
1969
|
||||||
23:32
|
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
|
end
|
||||||
assert_equal(Time.utc(1997, 11, 21, 9, 55, 6),
|
t = Time.rfc2822("21 Nov 97 09:55:06 GMT")
|
||||||
Time.rfc2822("21 Nov 97 09:55:06 GMT"))
|
assert_equal(Time.utc(1997, 11, 21, 9, 55, 6), t)
|
||||||
assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600,
|
assert_equal(0, t.utc_offset)
|
||||||
Time.rfc2822("Fri, 21 Nov 1997 09 : 55 : 06 -0600"))
|
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) {
|
assert_raise(ArgumentError) {
|
||||||
# inner comment is not supported.
|
# inner comment is not supported.
|
||||||
Time.rfc2822("Fri, 21 Nov 1997 09(comment): 55 : 06 -0600")
|
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
|
end
|
||||||
|
|
||||||
def test_encode_rfc2822
|
def test_encode_rfc2822
|
||||||
@ -389,8 +404,13 @@ class TestTimeExtension < Test::Unit::TestCase # :nodoc:
|
|||||||
|
|
||||||
def test_ruby_talk_152866
|
def test_ruby_talk_152866
|
||||||
t = Time::xmlschema('2005-08-30T22:48:00-07:00')
|
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(31, t.day)
|
||||||
assert_equal(8, t.mon)
|
assert_equal(8, t.mon)
|
||||||
|
assert_equal(0, t.utc_offset)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_parse_fraction
|
def test_parse_fraction
|
||||||
|
Loading…
x
Reference in New Issue
Block a user