From 3cf6e6474725aa3e4f82aeabdbf283a817994362 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Sat, 3 May 2008 08:44:41 +1000 Subject: [PATCH] fixed up AR 2.0.x timezone support --- lib/validates_timeliness/attribute_methods.rb | 3 +-- spec/attribute_methods_spec.rb | 7 +++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/validates_timeliness/attribute_methods.rb b/lib/validates_timeliness/attribute_methods.rb index 6810c57..a11568f 100644 --- a/lib/validates_timeliness/attribute_methods.rb +++ b/lib/validates_timeliness/attribute_methods.rb @@ -26,7 +26,6 @@ module ValidatesTimeliness evaluate_attribute_method attr_name, method_body end - # TODO rails 2.0 time casting better with timezone def define_write_method_for_time_zone_conversion(attr_name) method_body = <<-EOV def #{attr_name}=(time) @@ -34,7 +33,7 @@ module ValidatesTimeliness time = time.in_time_zone elsif time && time.acts_like?(:time) # Rails 2.0.x compatibility - time = @@default_timezone == :utc ? time.to_time : time.to_time + time = @@default_timezone == :utc ? time.utc : time.localtime end write_attribute(:#{attr_name}, time) end diff --git a/spec/attribute_methods_spec.rb b/spec/attribute_methods_spec.rb index 5275cf5..3527be4 100644 --- a/spec/attribute_methods_spec.rb +++ b/spec/attribute_methods_spec.rb @@ -33,14 +33,17 @@ describe ValidatesTimeliness::AttributeMethods do end if ActiveRecord::VERSION::STRING < '2.1' - it "should return stored time string as Time with correct timezone" do + it "should return stored time string as Time with correct timezone" do @person.birth_date_and_time = "1980-12-25 01:02:03" - @person.birth_date_and_time.zone == Time.new.zone + @person.birth_date_and_time.zone == 'EST' + ActiveRecord::Base.default_timezone = :utc + @person.birth_date_and_time.zone == 'UTC' end end unless ActiveRecord::VERSION::STRING < '2.1' it "should return stored time string as Time with correct timezone" do + Time.zone = TimeZone['Sydney'] # no I'm not from Sydney but there is no Melbourne timezone! @person.birth_date_and_time = "1980-12-25 01:02:03" @person.birth_date_and_time.zone == Time.zone end