diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2010-03-28 22:51:48 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-03-28 22:52:08 -0700 |
commit | 41e7c68d87903d0596228b6c1ae2c5d87b209280 (patch) | |
tree | c7cee7afcff50be6a1d1381e1b6ea0f336d784be /activesupport/lib/active_support/core_ext | |
parent | 40d90ed7df3fbb730b5a1231445247accbf895f1 (diff) | |
download | rails-41e7c68d87903d0596228b6c1ae2c5d87b209280.tar.gz rails-41e7c68d87903d0596228b6c1ae2c5d87b209280.tar.bz2 rails-41e7c68d87903d0596228b6c1ae2c5d87b209280.zip |
Ruby 1.9.2: marshaling round-trips Time#zone
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/marshal.rb | 56 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/marshal_with_utc_flag.rb | 26 |
2 files changed, 56 insertions, 26 deletions
diff --git a/activesupport/lib/active_support/core_ext/time/marshal.rb b/activesupport/lib/active_support/core_ext/time/marshal.rb new file mode 100644 index 0000000000..1a4d918ce7 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/time/marshal.rb @@ -0,0 +1,56 @@ +# Pre-1.9 versions of Ruby have a bug with marshaling Time instances, where utc instances are +# unmarshalled 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 !Marshal.load(Marshal.dump(Time.now.utc)).utc? + class Time + class << self + alias_method :_load_without_utc_flag, :_load + def _load(marshaled_time) + time = _load_without_utc_flag(marshaled_time) + time.instance_eval do + if defined?(@marshal_with_utc_coercion) + val = remove_instance_variable("@marshal_with_utc_coercion") + end + val ? utc : self + end + end + end + + alias_method :_dump_without_utc_flag, :_dump + def _dump(*args) + obj = dup + obj.instance_variable_set('@marshal_with_utc_coercion', utc?) + obj._dump_without_utc_flag(*args) + end + end +end + +# Ruby 1.9.2 adds utc_offset and zone to Time, but marshaling only +# preserves utc_offset. Preserve zone also, even though it may not +# work in some edge cases. +if Time.local(2010).zone != Marshal.load(Marshal.dump(Time.local(2010))).zone + class Time + class << self + alias_method :_load_without_zone, :_load + def _load(marshaled_time) + time = _load_without_zone(marshaled_time) + time.instance_eval do + if zone = defined?(@_zone) && remove_instance_variable('@_zone') + ary = to_a + ary[-1] = zone + utc? ? Time.utc(*ary) : Time.local(*ary) + else + self + end + end + end + end + + alias_method :_dump_without_zone, :_dump + def _dump(*args) + obj = dup + obj.instance_variable_set('@_zone', zone) + obj._dump_without_zone(*args) + end + end +end diff --git a/activesupport/lib/active_support/core_ext/time/marshal_with_utc_flag.rb b/activesupport/lib/active_support/core_ext/time/marshal_with_utc_flag.rb deleted file mode 100644 index 8d46d80251..0000000000 --- a/activesupport/lib/active_support/core_ext/time/marshal_with_utc_flag.rb +++ /dev/null @@ -1,26 +0,0 @@ -# Pre-1.9 versions of Ruby have a bug with marshaling Time instances, where utc instances are -# unmarshalled 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 Time - class << self - alias_method :_original_load, :_load - def _load(marshaled_time) - time = _original_load(marshaled_time) - time.instance_eval do - if defined?(@marshal_with_utc_coercion) - val = remove_instance_variable("@marshal_with_utc_coercion") - end - val ? utc : self - end - end - end - - alias_method :_original_dump, :_dump - def _dump(*args) - obj = dup - obj.instance_variable_set('@marshal_with_utc_coercion', utc?) - obj._original_dump(*args) - end - end -end |