Skip to content

Commit

Permalink
* lib/time.rb: recognize +00:00 and GMT as a localtime.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8@7734 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
akr committed Jan 6, 2005
1 parent 46b138a commit 7f30e1a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
@@ -1,3 +1,7 @@
Thu Jan 6 13:45:35 2005 Tanaka Akira <akr@m17n.org>

* lib/time.rb: recognize +00:00 and GMT as a localtime.

Thu Jan 6 07:58:28 2005 Dave Thomas <dave@pragprog.com>

* lib/rdoc/usage.rb (RDoc::RDoc.usage_no_exit): Allow for colons
Expand Down
32 changes: 28 additions & 4 deletions lib/time.rb
Expand Up @@ -60,7 +60,7 @@ class << Time
'MST' => -7, 'MDT' => -6,
'PST' => -8, 'PDT' => -7,
# Following definition of military zones is original one.
# See RFC 1123 and RFC 2822 for the error of RFC 822.
# See RFC 1123 and RFC 2822 for the error in RFC 822.
'A' => +1, 'B' => +2, 'C' => +3, 'D' => +4, 'E' => +5, 'F' => +6,
'G' => +7, 'H' => +8, 'I' => +9, 'K' => +10, 'L' => +11, 'M' => +12,
'N' => -1, 'O' => -2, 'P' => -3, 'Q' => -4, 'R' => -5, 'S' => -6,
Expand All @@ -83,6 +83,17 @@ def zone_offset(zone, year=Time.now.year)
off
end

def zone_utc?(zone)
# * +0000 means localtime. [RFC 2822]
# * GMT is a localtime abbreviation in Europe/London, etc.
if /\A(?:-00:00|-0000|-00|UTC|Z|UT)\z/i =~ zone
true
else
false
end
end
private :zone_utc?

#
# Parses +date+ using ParseDate.parsedate and converts it to a Time object.
#
Expand Down Expand Up @@ -160,7 +171,7 @@ def parse(date, now=Time.now)

if off
t = Time.utc(year, mon, day, hour, min, sec) - off
t.localtime if off != 0
t.localtime if !zone_utc?(zone)
t
else
Time.local(year, mon, day, hour, min, sec)
Expand Down Expand Up @@ -212,8 +223,8 @@ def rfc2822(date)
end

t = Time.utc(year, mon, day, hour, min, sec)
offset = zone_offset(zone)
t = (t - offset).localtime if offset != 0 || zone == '+0000'
t -= zone_offset(zone)
t.localtime if !zone_utc?(zone)
t
else
raise ArgumentError.new("not RFC 2822 compliant date: #{date.inspect}")
Expand Down Expand Up @@ -612,6 +623,19 @@ def test_invalid
assert_raise(ArgumentError) { Time.rfc2822("\307\341\314\343\332\311, 30 \344\346\335\343\310\321 2001 10:01:06") }
assert_raise(ArgumentError) { Time.rfc2822("=?iso-8859-1?Q?(=BF=E5),?= 12 =?iso-8859-1?Q?9=B7=EE?= 2001 14:52:41\n+0900 (JST)") }
end

def test_zone_0000
assert_equal(true, Time.parse("2000-01-01T00:00:00Z").utc?)
assert_equal(true, Time.parse("2000-01-01T00:00:00-00:00").utc?)
assert_equal(false, Time.parse("2000-01-01T00:00:00+00:00").utc?)
assert_equal(false, Time.parse("Sat, 01 Jan 2000 00:00:00 GMT").utc?)
assert_equal(true, Time.parse("Sat, 01 Jan 2000 00:00:00 -0000").utc?)
assert_equal(false, Time.parse("Sat, 01 Jan 2000 00:00:00 +0000").utc?)
assert_equal(false, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 GMT").utc?)
assert_equal(true, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 -0000").utc?)
assert_equal(false, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 +0000").utc?)
assert_equal(true, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 UTC").utc?)
end
end

end

0 comments on commit 7f30e1a

Please sign in to comment.