diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2013-07-09 13:34:24 +0100 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2013-07-09 13:43:56 +0100 |
commit | 1b3873730b96035a238dbff7627bd5942e6dc4e7 (patch) | |
tree | 72259583e746b6eacf773e2db3e84fc31b710be3 /activesupport/lib | |
parent | 484253515c0e05760541dc48946361185c9e6904 (diff) | |
download | rails-1b3873730b96035a238dbff7627bd5942e6dc4e7.tar.gz rails-1b3873730b96035a238dbff7627bd5942e6dc4e7.tar.bz2 rails-1b3873730b96035a238dbff7627bd5942e6dc4e7.zip |
Retain UTC offset when using Time.at_with_coercion
The standard Ruby behavior for Time.at is to return the same type of
time when passing an instance of Time as a single argument. Since the
an ActiveSupport::TimeWithZone instance may be a different timezone than
the system timezone and DateTime just understands offsets the best we
can do is to return an instance of Time with the correct offset.
Fixes #11350.
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/calculations.rb | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 8270093dc7..20e7e0b303 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -33,10 +33,15 @@ class Time # 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_f) + return at_without_coercion(*args) if args.size != 1 + + # Time.at can be called with a time or numerical value + time_or_number = args.first + + if time_or_number.is_a?(ActiveSupport::TimeWithZone) || time_or_number.is_a?(DateTime) + at_without_coercion(time_or_number.to_f).getlocal(time_or_number.utc_offset) else - at_without_coercion(*args) + at_without_coercion(time_or_number) end end alias_method :at_without_coercion, :at |