From dad386d8c9ad7db85024935d36f7197bf4997cfa Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Wed, 18 Aug 2010 19:36:01 +0200 Subject: [PATCH] Fixing time parsing issue for times without zone --- spec/spec.opts => .rspec | 1 - Rakefile | 1 + couchrest_model.gemspec | 5 ++++- history.txt | 2 ++ lib/couchrest/model/typecast.rb | 17 +++++++++++------ spec/couchrest/property_spec.rb | 10 ++++++++++ spec/spec_helper.rb | 4 ++-- 7 files changed, 30 insertions(+), 10 deletions(-) rename spec/spec.opts => .rspec (78%) diff --git a/spec/spec.opts b/.rspec similarity index 78% rename from spec/spec.opts rename to .rspec index 1984b21..5f436d0 100644 --- a/spec/spec.opts +++ b/.rspec @@ -1,5 +1,4 @@ --colour --format progress ---loadby mtime diff --git a/Rakefile b/Rakefile index 6f36f1e..6f2ec02 100644 --- a/Rakefile +++ b/Rakefile @@ -31,6 +31,7 @@ begin gemspec.add_dependency("activesupport", ">= 2.3.5") gemspec.add_dependency("activemodel", ">= 3.0.0.beta4") gemspec.add_dependency("tzinfo", ">= 0.3.22") + gemspec.add_development_dependency('rspec', '>= 2.0.0.beta.19') gemspec.version = CouchRest::Model::VERSION gemspec.date = Time.now.strftime("%Y-%m-%d") gemspec.require_path = "lib" diff --git a/couchrest_model.gemspec b/couchrest_model.gemspec index a1b6c7e..1b4c89e 100644 --- a/couchrest_model.gemspec +++ b/couchrest_model.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version= s.authors = ["J. Chris Anderson", "Matt Aimonetti", "Marcos Tapajos", "Will Leinweber", "Sam Lown"] - s.date = %q{2010-08-11} + s.date = %q{2010-08-18} s.description = %q{CouchRest Model provides aditional features to the standard CouchRest Document class such as properties, view designs, associations, callbacks, typecasting and validations.} s.email = %q{jchris@apache.org} s.extra_rdoc_files = [ @@ -133,12 +133,14 @@ Gem::Specification.new do |s| s.add_runtime_dependency(%q, [">= 2.3.5"]) s.add_runtime_dependency(%q, [">= 3.0.0.beta4"]) s.add_runtime_dependency(%q, [">= 0.3.22"]) + s.add_development_dependency(%q, [">= 2.0.0.beta.19"]) else s.add_dependency(%q, [">= 1.0.0"]) s.add_dependency(%q, [">= 1.15"]) s.add_dependency(%q, [">= 2.3.5"]) s.add_dependency(%q, [">= 3.0.0.beta4"]) s.add_dependency(%q, [">= 0.3.22"]) + s.add_dependency(%q, [">= 2.0.0.beta.19"]) end else s.add_dependency(%q, [">= 1.0.0"]) @@ -146,6 +148,7 @@ Gem::Specification.new do |s| s.add_dependency(%q, [">= 2.3.5"]) s.add_dependency(%q, [">= 3.0.0.beta4"]) s.add_dependency(%q, [">= 0.3.22"]) + s.add_dependency(%q, [">= 2.0.0.beta.19"]) end end diff --git a/history.txt b/history.txt index bdc5b89..f8ee1fe 100644 --- a/history.txt +++ b/history.txt @@ -5,6 +5,8 @@ * Minor enhancements * Raise error on adding objects to "collection_of" without an id * Allow mixing of protected and accessible properties. Any unspecified properties are now assumed to be protected by default + * Parsing times without zone + * Using latest rspec (2.0.0.beta.19) == CouchRest Model 1.0.0.beta7 diff --git a/lib/couchrest/model/typecast.rb b/lib/couchrest/model/typecast.rb index c4aa934..496558d 100644 --- a/lib/couchrest/model/typecast.rb +++ b/lib/couchrest/model/typecast.rb @@ -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 diff --git a/spec/couchrest/property_spec.rb b/spec/couchrest/property_spec.rb index 1d9bebf..95352a9 100644 --- a/spec/couchrest/property_spec.rb +++ b/spec/couchrest/property_spec.rb @@ -571,6 +571,16 @@ describe "Model properties" do @course['ends_at'].min.should eql(t.min) @course['ends_at'].sec.should eql(t.sec) end + it 'parses the string without offset' do + t = Time.now + @course.ends_at = t.strftime("%Y-%m-%d %H:%M:%S") + @course['ends_at'].year.should eql(t.year) + @course['ends_at'].month.should eql(t.month) + @course['ends_at'].day.should eql(t.day) + @course['ends_at'].hour.should eql(t.hour) + @course['ends_at'].min.should eql(t.min) + @course['ends_at'].sec.should eql(t.sec) + end end it 'does not typecast non-time values' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 53d65c1..d26d3d7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,5 @@ require "rubygems" -require "spec" # Satisfies Autotest and anyone else not using the Rake tasks +require "rspec" # Satisfies Autotest and anyone else not using the Rake tasks require File.join(File.dirname(__FILE__), '..','lib','couchrest_model') # check the following file to see how to use the spec'd features. @@ -24,7 +24,7 @@ def reset_test_db! DB end -Spec::Runner.configure do |config| +RSpec.configure do |config| config.before(:all) { reset_test_db! } config.after(:all) do