From 96dabc8288ef5b1128e2480fc0118fff13a225c0 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Thu, 21 Mar 2013 17:30:46 +0900 Subject: [PATCH] Add 1.8 compatibility. --- lib/http/cookie/scanner.rb | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/http/cookie/scanner.rb b/lib/http/cookie/scanner.rb index 742c4e3..2961636 100644 --- a/lib/http/cookie/scanner.rb +++ b/lib/http/cookie/scanner.rb @@ -79,6 +79,26 @@ class HTTP::Cookie::Scanner < StringScanner [name, value] end + if Time.respond_to?(:strptime) + def tuple_to_time(day_of_month, month, year, time) + Time.strptime( + '%02d %s %04d %02d:%02d:%02d UTC' % [day_of_month, month, year, *time], + '%d %b %Y %T %Z' + ).tap { |date| + date.day == day_of_month or return nil + } + end + else + def tuple_to_time(day_of_month, month, year, time) + Time.parse( + '%02d %s %04d %02d:%02d:%02d UTC' % [day_of_month, month, year, *time] + ).tap { |date| + date.day == day_of_month or return nil + } + end + end + private :tuple_to_time + def parse_cookie_date(s) # RFC 6265 5.1.1 time = day_of_month = month = year = nil @@ -127,12 +147,7 @@ class HTTP::Cookie::Scanner < StringScanner return nil end - Time.strptime( - '%02d %s %04d %02d:%02d:%02d UTC' % [day_of_month, month, year, *time], - '%d %b %Y %T %Z' - ).tap { |date| - date.day == day_of_month or return nil - } + tuple_to_time(day_of_month, month, year, time) end def scan_cookie