aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/time.rb23
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb34
3 files changed, 58 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 8d3b136d80..0170b95b1b 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*Edge*
+* Fix Ruby's Time marshaling bug in pre-1.9 versions of Ruby: utc instances are now correctly unmarshaled with a utc zone instead of the system local zone [#900 state:resolved] [Luca Guidi, Geoff Buesing]
+
* Add Array#in_groups which splits or iterates over the array in specified number of groups. #579. [Adrian Mugnolo] Example:
a = (1..10).to_a
diff --git a/activesupport/lib/active_support/core_ext/time.rb b/activesupport/lib/active_support/core_ext/time.rb
index ea50511a96..2006cf4946 100644
--- a/activesupport/lib/active_support/core_ext/time.rb
+++ b/activesupport/lib/active_support/core_ext/time.rb
@@ -1,11 +1,32 @@
require 'date'
require 'time'
-# Ruby 1.8-cvs and 1.9 define private Time#to_date
class Time
+ # Ruby 1.8-cvs and 1.9 define private Time#to_date
%w(to_date to_datetime).each do |method|
public method if private_instance_methods.include?(method)
end
+
+ # Pre-1.9 versions of Ruby have a bug with marshaling Time instances, where utc instances are
+ # unmarshaled in the local zone, instead of utc. We're layering behavior on the _dump and _load
+ # methods so that utc instances can be flagged on dump, and coerced back to utc on load.
+ if RUBY_VERSION < '1.9'
+ class << self
+ alias_method :_original_load, :_load
+ def _load(marshaled_time)
+ time = _original_load(marshaled_time)
+ utc = time.send(:remove_instance_variable, '@marshal_with_utc_coercion')
+ utc ? time.utc : time
+ end
+ end
+
+ alias_method :_original_dump, :_dump
+ def _dump(*args)
+ obj = self.frozen? ? self.dup : self
+ obj.instance_variable_set('@marshal_with_utc_coercion', utc?)
+ obj._original_dump(*args)
+ end
+ end
end
require 'active_support/core_ext/time/behavior'
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index 8740497b3d..4749950f25 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -625,3 +625,37 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end
+
+class TimeExtMarshalingTest < Test::Unit::TestCase
+ def test_marshaling_with_utc_instance
+ t = Time.utc(2000)
+ marshaled = Marshal.dump t
+ unmarshaled = Marshal.load marshaled
+ assert_equal t, unmarshaled
+ assert_equal t.zone, unmarshaled.zone
+ end
+
+ def test_marshaling_with_local_instance
+ t = Time.local(2000)
+ marshaled = Marshal.dump t
+ unmarshaled = Marshal.load marshaled
+ assert_equal t, unmarshaled
+ assert_equal t.zone, unmarshaled.zone
+ end
+
+ def test_marshaling_with_frozen_utc_instance
+ t = Time.utc(2000).freeze
+ marshaled = Marshal.dump t
+ unmarshaled = Marshal.load marshaled
+ assert_equal t, unmarshaled
+ assert_equal t.zone, unmarshaled.zone
+ end
+
+ def test_marshaling_with_frozen_local_instance
+ t = Time.local(2000).freeze
+ marshaled = Marshal.dump t
+ unmarshaled = Marshal.load marshaled
+ assert_equal t, unmarshaled
+ assert_equal t.zone, unmarshaled.zone
+ end
+end