aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-02-24 13:27:32 -0800
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-02-24 13:27:32 -0800
commita06623609ee37f24b1b741098db52612b6f539ab (patch)
treea2252bd0e280b9ca750b3907bd3d2aa0f2153729
parent70ec7e02458108805784786d2562b43b4c380342 (diff)
parentcb100ca02bf0652e39a61e7687a6ac034b1a9c43 (diff)
downloadrails-a06623609ee37f24b1b741098db52612b6f539ab.tar.gz
rails-a06623609ee37f24b1b741098db52612b6f539ab.tar.bz2
rails-a06623609ee37f24b1b741098db52612b6f539ab.zip
Merge pull request #9403 from stopdropandrew/TimeWithZone#to_r
Added `ActiveSupport::TimeWithZone#to_r` for `Time#at` compatibility. Closes #9404
-rw-r--r--activesupport/CHANGELOG.md4
-rw-r--r--activesupport/lib/active_support/time_with_zone.rb4
-rw-r--r--activesupport/test/core_ext/time_with_zone_test.rb11
3 files changed, 19 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 5f81f45333..86e5b6f781 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##
+* Added `ActiveSupport::TimeWithZone#to_r` for `Time#at` compatibility.
+
+ *stopdropandrew*
+
* `ActiveSupport::NumberHelper#number_to_human` returns the number unaltered when
the given units hash does not contain the needed key, e.g. when the number provided
is less than the largest key provided.
diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb
index 0e6d12a186..98c866ac43 100644
--- a/activesupport/lib/active_support/time_with_zone.rb
+++ b/activesupport/lib/active_support/time_with_zone.rb
@@ -317,6 +317,10 @@ module ActiveSupport
end
alias_method :tv_sec, :to_i
+ def to_r
+ utc.to_r
+ end
+
# Return an instance of Time in the system timezone.
def to_time
utc.to_time
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb
index 21fef88d79..0f5699fd63 100644
--- a/activesupport/test/core_ext/time_with_zone_test.rb
+++ b/activesupport/test/core_ext/time_with_zone_test.rb
@@ -326,6 +326,17 @@ class TimeWithZoneTest < ActiveSupport::TestCase
assert_equal 946684800, twz.to_i
end
+ def test_to_r
+ result = ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1), ActiveSupport::TimeZone['Hawaii']).to_r
+ assert_equal Rational(946684800, 1), result
+ assert_kind_of Rational, result
+ end
+
+ def test_time_at
+ time = ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1), ActiveSupport::TimeZone['Hawaii'])
+ assert_equal time, Time.at(time)
+ end
+
def test_to_time
with_env_tz 'US/Eastern' do
assert_equal Time, @twz.to_time.class