Fixing time parsing issue for times without zone

This commit is contained in:
Sam Lown 2010-08-18 19:36:01 +02:00
parent 626f997ff1
commit dad386d8c9
7 changed files with 30 additions and 10 deletions

View file

@ -1,19 +1,23 @@
class Time
# returns a local time value much faster than Time.parse
def self.mktime_with_offset(string)
string =~ /(\d{4})[\-|\/](\d{2})[\-|\/](\d{2})[T|\s](\d{2}):(\d{2}):(\d{2})([\+|\s|\-])*(\d{2}):?(\d{2})/
string =~ /(\d{4})[\-|\/](\d{2})[\-|\/](\d{2})[T|\s](\d{2}):(\d{2}):(\d{2})(([\+|\s|\-])*(\d{2}):?(\d{2}))?/
# $1 = year
# $2 = month
# $3 = day
# $4 = hours
# $5 = minutes
# $6 = seconds
# $7 = time zone direction
# $8 = tz difference
# $8 = time zone direction
# $9 = tz difference
# utc time with wrong TZ info:
time = mktime($1, RFC2822_MONTH_NAME[$2.to_i - 1], $3, $4, $5, $6, $7)
tz_difference = ("#{$7 == '-' ? '+' : '-'}#{$8}".to_i * 3600)
time + tz_difference + zone_offset(time.zone)
time = mktime($1, RFC2822_MONTH_NAME[$2.to_i - 1], $3, $4, $5, $6)
if ($7)
tz_difference = ("#{$8 == '-' ? '+' : '-'}#{$9}".to_i * 3600)
time + tz_difference + zone_offset(time.zone)
else
time
end
end
end
@ -128,6 +132,7 @@ module CouchRest
rescue ArgumentError
value
rescue TypeError
# After failures, resort to normal time parse
value
end