aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--lib/time.rb29
-rw-r--r--test/test_time.rb66
3 files changed, 66 insertions, 35 deletions
diff --git a/ChangeLog b/ChangeLog
index 57bd133144..a9e56d8424 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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>
* lib/time.rb (make_time): Produce fixed-offset time object if
diff --git a/lib/time.rb b/lib/time.rb
index 507bf28daf..cb37c4e562 100644
--- a/lib/time.rb
+++ b/lib/time.rb
@@ -445,24 +445,26 @@ class Time
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
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
diff --git a/test/test_time.rb b/test/test_time.rb
index fe8fb9288e..54c2166ec5 100644
--- a/test/test_time.rb
+++ b/test/test_time.rb
@@ -4,46 +4,61 @@ require_relative 'ruby/envutil.rb'
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::TestCase # :nodoc:
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