Add :weeks to the list of variable duration parts

Since 434df00 week durations are no longer converted to days. This means
we need to add :weeks to the parts that ActiveSupport::TimeWithZone will
consider being of variable duration to take account of DST transitions.

Fixes #26039.
This commit is contained in:
Andrew White 2016-08-03 14:45:09 +01:00
parent fac9938b91
commit 0e762ecdc3
3 changed files with 41 additions and 1 deletions

View File

@ -1,3 +1,11 @@
* Since weeks are no longer converted to days, add `:weeks` to the list of
parts that `ActiveSupport::TimeWithZone` will recognize as possibly being
of variable duration to take account of DST transitions.
Fixes #26039.
*Andrew White*
* Defines `Regexp.match?` for Ruby versions prior to 2.4. The predicate
has the same interface, but it does not have the performance boost. Its
purpose is to be able to write 2.4 compatible code.

View File

@ -481,7 +481,7 @@ module ActiveSupport
end
def duration_of_variable_length?(obj)
ActiveSupport::Duration === obj && obj.parts.any? {|p| [:years, :months, :days].include?(p[0]) }
ActiveSupport::Duration === obj && obj.parts.any? {|p| [:years, :months, :weeks, :days].include?(p[0]) }
end
def wrap_with_time_zone(time)

View File

@ -834,6 +834,38 @@ class TimeWithZoneTest < ActiveSupport::TestCase
assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.advance(:hours => -24).inspect
end
def test_advance_1_week_across_spring_dst_transition
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,1,10,30))
assert_equal "Sat, 08 Apr 2006 10:30:00 EDT -04:00", twz.advance(:weeks => 1).inspect
assert_equal "Sat, 08 Apr 2006 10:30:00 EDT -04:00", twz.weeks_since(1).inspect
assert_equal "Sat, 08 Apr 2006 10:30:00 EDT -04:00", twz.since(1.week).inspect
assert_equal "Sat, 08 Apr 2006 10:30:00 EDT -04:00", (twz + 1.week).inspect
end
def test_advance_1_week_across_spring_dst_transition_backwards
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,8,10,30))
assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:weeks => -1).inspect
assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.weeks_ago(1).inspect
assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(1.week).inspect
assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 1.week).inspect
end
def test_advance_1_week_across_fall_dst_transition
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,28,10,30))
assert_equal "Sat, 04 Nov 2006 10:30:00 EST -05:00", twz.advance(:weeks => 1).inspect
assert_equal "Sat, 04 Nov 2006 10:30:00 EST -05:00", twz.weeks_since(1).inspect
assert_equal "Sat, 04 Nov 2006 10:30:00 EST -05:00", twz.since(1.week).inspect
assert_equal "Sat, 04 Nov 2006 10:30:00 EST -05:00", (twz + 1.week).inspect
end
def test_advance_1_week_across_fall_dst_transition_backwards
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,11,4,10,30))
assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.advance(:weeks => -1).inspect
assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.weeks_ago(1).inspect
assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(1.week).inspect
assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 1.week).inspect
end
def test_advance_1_month_across_spring_dst_transition
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,1,10,30))
assert_equal "Mon, 01 May 2006 10:30:00 EDT -04:00", twz.advance(:months => 1).inspect