mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
wrap time ranges with timezones, closes #6179
This commit is contained in:
parent
a29bc1cf7b
commit
e2e513621d
2 changed files with 20 additions and 3 deletions
|
@ -319,8 +319,7 @@ module ActiveSupport
|
|||
|
||||
# Send the missing method to +time+ instance, and wrap result in a new TimeWithZone with the existing +time_zone+.
|
||||
def method_missing(sym, *args, &block)
|
||||
result = time.__send__(sym, *args, &block)
|
||||
result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result
|
||||
wrap_with_time_zone time.__send__(sym, *args, &block)
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -344,5 +343,15 @@ module ActiveSupport
|
|||
def duration_of_variable_length?(obj)
|
||||
ActiveSupport::Duration === obj && obj.parts.any? {|p| p[0].in?([:years, :months, :days]) }
|
||||
end
|
||||
|
||||
def wrap_with_time_zone(time)
|
||||
if time.acts_like?(:time)
|
||||
self.class.new(nil, time_zone, time)
|
||||
elsif time.is_a?(Range)
|
||||
wrap_with_time_zone(time.begin)..wrap_with_time_zone(time.end)
|
||||
else
|
||||
time
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -517,7 +517,7 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
|
|||
assert_equal Time.local(2006,11,15), Time.local(2006,11,23,0,0,0).prev_week(:wednesday)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def test_last_week
|
||||
with_env_tz 'US/Eastern' do
|
||||
assert_equal Time.local(2005,2,21), Time.local(2005,3,1,15,15,10).last_week
|
||||
|
@ -831,6 +831,14 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
|
|||
assert_equal Time.local(2011,6,7,0,0,0)..Time.local(2011,6,7,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).all_day
|
||||
end
|
||||
|
||||
def test_all_day_with_timezone
|
||||
beginning_of_day = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Hawaii"], Time.local(2011,6,7,0,0,0))
|
||||
end_of_day = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Hawaii"], Time.local(2011,6,7,23,59,59,999999.999))
|
||||
|
||||
assert_equal beginning_of_day.inspect, ActiveSupport::TimeWithZone.new(Time.local(2011,6,7,10,10,10), ActiveSupport::TimeZone["Hawaii"]).all_day.begin.inspect
|
||||
assert_equal end_of_day.inspect, ActiveSupport::TimeWithZone.new(Time.local(2011,6,7,10,10,10), ActiveSupport::TimeZone["Hawaii"]).all_day.end.inspect
|
||||
end
|
||||
|
||||
def test_all_week
|
||||
assert_equal Time.local(2011,6,6,0,0,0)..Time.local(2011,6,12,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).all_week
|
||||
assert_equal Time.local(2011,6,5,0,0,0)..Time.local(2011,6,11,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).all_week(:sunday)
|
||||
|
|
Loading…
Reference in a new issue