Parses date as date-time defined by RFC 2822 and converts it to a
Time object. The format is identical to the date
format defined by RFC 822 and updated by RFC 1123.
ArgumentError is raised if date
is not compliant with RFC 2822 or Time class
cannot represent specified date.
time library should be required to use this method as follows.
require'time'
# File lib/time.rb, line 312
def rfc2822(date)
if /\A\s*
(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*,\s*)?
(\d{1,2})\s+
(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+
(\d{2,})\s+
(\d{2})\s*
:\s*(\d{2})\s*
(?::\s*(\d{2}))?\s+
([+-]\d{4}|
UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])/x =~ date
# Since RFC 2822 permit comments, the regexp has no right anchor.
day = $1.to_i
mon = MonthValue[$2.upcase]
year = $3.to_i
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
year, mon, day, hour, min, sec =
apply_offset(year, mon, day, hour, min, sec, zone_offset(zone))
t = self.utc(year, mon, day, hour, min, sec)
t.localtime if !zone_utc?(zone)
t
else
raise ArgumentError.new("not RFC 2822 compliant date: #{date.inspect}")
end
end