Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Override Time.at to work with Time-like values

Time.at allows passing a single Time argument which is then converted
to an integer. The conversion code since 1.9.3r429 explicitly checks
for an instance of Time so we need to override it to allow DateTime
and ActiveSupport::TimeWithZone values.
  • Loading branch information...
commit 214e377a329d8b4df5a8cff4b6a7347f1282b384 1 parent ffaa850
@pixeltrix pixeltrix authored
View
13 activesupport/CHANGELOG.md
@@ -1,12 +1,14 @@
-## unreleased ##
+* Override `Time.at` to support the passing of Time-like values when called with a single argument.
-* Allow Date to be compared with Time (like it was possible to compare Time with Date).
+ *Andrew White*
+
+* Allow Date to be compared with Time (like it was possible to compare Time with Date).
- *DHH*
+ *DHH*
-* Deprecate multiple parameters support of `Object#in?`.
+* Deprecate multiple parameters support of `Object#in?`.
- *Brian Morearty + Carlos Antonio da Silva*
+ *Brian Morearty + Carlos Antonio da Silva*
## Rails 4.0.0.rc1 (April 29, 2013) ##
@@ -39,6 +41,7 @@
*Charles Jones*
+
## Rails 4.0.0.beta1 (February 25, 2013) ##
* Improve singularizing a singular for multiple cases.
View
12 activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -65,6 +65,18 @@ def local_time(*args)
def current
::Time.zone ? ::Time.zone.now : ::Time.now
end
+
+ # Layers additional behavior on Time.at so that ActiveSupport::TimeWithZone and DateTime
+ # instances can be used when called with a single argument
+ def at_with_coercion(*args)
+ if args.size == 1 && args.first.acts_like?(:time)
+ at_without_coercion(args.first.to_i)
+ else
+ at_without_coercion(*args)
+ end
+ end
+ alias_method :at_without_coercion, :at
+ alias_method :at, :at_with_coercion
end
# Seconds since midnight: Time.now.seconds_since_midnight
View
22 activesupport/test/core_ext/time_ext_test.rb
@@ -741,6 +741,28 @@ def test_compare_with_time_with_zone
assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), ActiveSupport::TimeZone['UTC'] ))
end
+ def test_at_with_datetime
+ assert_equal Time.utc(2000, 1, 1, 0, 0, 0), Time.at(DateTime.civil(2000, 1, 1, 0, 0, 0))
+
+ # Only test this if the underlying Time.at raises a TypeError
+ begin
+ Time.at_without_coercion(Time.now, 0)
+ rescue TypeError
+ assert_raise(TypeError) { assert_equal(Time.utc(2000, 1, 1, 0, 0, 0), Time.at(DateTime.civil(2000, 1, 1, 0, 0, 0), 0)) }
+ end
+ end
+
+ def test_at_with_time_with_zone
+ assert_equal Time.utc(2000, 1, 1, 0, 0, 0), Time.at(ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1, 0, 0, 0), ActiveSupport::TimeZone['UTC']))
+
+ # Only test this if the underlying Time.at raises a TypeError
+ begin
+ Time.at_without_coercion(Time.now, 0)
+ rescue TypeError
+ assert_raise(TypeError) { assert_equal(Time.utc(2000, 1, 1, 0, 0, 0), Time.at(ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1, 0, 0, 0), ActiveSupport::TimeZone['UTC']), 0)) }
+ end
+ end
+
def test_eql?
assert_equal true, Time.utc(2000).eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone['UTC']) )
assert_equal true, Time.utc(2000).eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]) )
Please sign in to comment.
Something went wrong with that request. Please try again.