aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/time
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-03-24 23:03:36 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2009-03-24 23:03:36 -0700
commit2a1ae9e8a1ccd16329e2c3fd1cab13b1fc620ed2 (patch)
tree34837d1192d82301eb39918f759b53fcd4f01f72 /activesupport/lib/active_support/core_ext/time
parentaf4c2375a270c7ddd71269593427a1d9f23be515 (diff)
downloadrails-2a1ae9e8a1ccd16329e2c3fd1cab13b1fc620ed2.tar.gz
rails-2a1ae9e8a1ccd16329e2c3fd1cab13b1fc620ed2.tar.bz2
rails-2a1ae9e8a1ccd16329e2c3fd1cab13b1fc620ed2.zip
Extract time fixes into separate extensions
Diffstat (limited to 'activesupport/lib/active_support/core_ext/time')
-rw-r--r--activesupport/lib/active_support/core_ext/time/marshal_with_utc_flag.rb22
-rw-r--r--activesupport/lib/active_support/core_ext/time/publicize_conversion_methods.rb10
2 files changed, 32 insertions, 0 deletions
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
new file mode 100644
index 0000000000..a1c8ece1d7
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/time/marshal_with_utc_flag.rb
@@ -0,0 +1,22 @@
+# 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 Time
+ class << self
+ alias_method :_original_load, :_load
+ def _load(marshaled_time)
+ time = _original_load(marshaled_time)
+ utc = time.instance_variable_get('@marshal_with_utc_coercion')
+ utc ? time.utc : time
+ end
+ end
+
+ alias_method :_original_dump, :_dump
+ def _dump(*args)
+ obj = frozen? ? dup : self
+ obj.instance_variable_set('@marshal_with_utc_coercion', utc?)
+ obj._original_dump(*args)
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/core_ext/time/publicize_conversion_methods.rb b/activesupport/lib/active_support/core_ext/time/publicize_conversion_methods.rb
new file mode 100644
index 0000000000..fc3f297843
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/time/publicize_conversion_methods.rb
@@ -0,0 +1,10 @@
+require 'date'
+
+class Time
+ # Ruby 1.8-cvs and early 1.9 series define private Time#to_date
+ %w(to_date to_datetime).each do |method|
+ if (m = instance_method(method) rescue nil) && private_instance_methods.include?(m.name)
+ public method
+ end
+ end
+end