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 if the Time
class cannot represent specified date.
# File lib/time.rb, line 470
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
short_year_p = $3.length <= 3
hour = $4.to_i
min = $5.to_i
sec = $6 ? $6.to_i : 0
zone = $7
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, off)
t = self.utc(year, mon, day, hour, min, sec)
force_zone!(t, zone, off)
t
else
raise ArgumentError.new("not RFC 2822 compliant date: #{date.inspect}")
end
end