Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes and tests to account for beginning and end of daylight savings …
…when offset changes
  • Loading branch information
Noah Davis committed Nov 3, 2008
1 parent 8f501dc commit a9ad71d
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/chronic/repeaters/repeater_time.rb
Expand Up @@ -64,27 +64,31 @@ def next(pointer)
unless @current_time
first = true
midnight = Chronic.time_class.local(@now.year, @now.month, @now.day)

yesterday_midnight = midnight - full_day
tomorrow_midnight = midnight + full_day

offset_fix = midnight.gmt_offset - tomorrow_midnight.gmt_offset
tomorrow_midnight += offset_fix

catch :done do
if pointer == :future
if @type.ambiguous?
[midnight + @type, midnight + half_day + @type, tomorrow_midnight + @type].each do |t|
[midnight + @type + offset_fix, midnight + half_day + @type + offset_fix, tomorrow_midnight + @type].each do |t|
(@current_time = t; throw :done) if t >= @now
end
else
[midnight + @type, tomorrow_midnight + @type].each do |t|
[midnight + @type + offset_fix, tomorrow_midnight + @type].each do |t|
(@current_time = t; throw :done) if t >= @now
end
end
else # pointer == :past
if @type.ambiguous?
[midnight + half_day + @type, midnight + @type, yesterday_midnight + @type * 2].each do |t|
[midnight + half_day + @type + offset_fix, midnight + @type + offset_fix, yesterday_midnight + @type + half_day].each do |t|
(@current_time = t; throw :done) if t <= @now
end
else
[midnight + @type, yesterday_midnight + @type].each do |t|
[midnight + @type + offset_fix, yesterday_midnight + @type].each do |t|
(@current_time = t; throw :done) if t <= @now
end
end
Expand Down
119 changes: 119 additions & 0 deletions test/test_DaylightSavings.rb
@@ -0,0 +1,119 @@
require 'chronic'
require 'test/unit'

class TestDaylightSavings < Test::Unit::TestCase

def setup
@begin_daylight_savings = Time.local(2008, 3, 9, 5, 0, 0, 0)
@end_daylight_savings = Time.local(2008, 11, 2, 5, 0, 0, 0)
end

def test_begin_past
# ambiguous - resolve to last night
t = Chronic::RepeaterTime.new('900')
t.start = @begin_daylight_savings
assert_equal Time.local(2008, 3, 8, 21), t.next(:past).begin

# ambiguous - resolve to this afternoon
t = Chronic::RepeaterTime.new('900')
t.start = Time.local(2008, 3, 9, 22, 0, 0, 0)
assert_equal Time.local(2008, 3, 9, 21), t.next(:past).begin

# ambiguous - resolve to this morning
t = Chronic::RepeaterTime.new('400')
t.start = @begin_daylight_savings
assert_equal Time.local(2008, 3, 9, 4), t.next(:past).begin

# unambiguous - resolve to today
t = Chronic::RepeaterTime.new('0400')
t.start = @begin_daylight_savings
assert_equal Time.local(2008, 3, 9, 4), t.next(:past).begin

# unambiguous - resolve to yesterday
t = Chronic::RepeaterTime.new('1300')
t.start = @begin_daylight_savings
assert_equal Time.local(2008, 3, 8, 13), t.next(:past).begin
end

def test_begin_future
# ambiguous - resolve to this morning
t = Chronic::RepeaterTime.new('900')
t.start = @begin_daylight_savings
assert_equal Time.local(2008, 3, 9, 9), t.next(:future).begin

# ambiguous - resolve to this afternoon
t = Chronic::RepeaterTime.new('900')
t.start = Time.local(2008, 3, 9, 13, 0, 0, 0)
assert_equal Time.local(2008, 3, 9, 21), t.next(:future).begin

# ambiguous - resolve to tomorrow
t = Chronic::RepeaterTime.new('900')
t.start = Time.local(2008, 3, 9, 22, 0, 0, 0)
assert_equal Time.local(2008, 3, 10, 9), t.next(:future).begin

# unambiguous - resolve to today
t = Chronic::RepeaterTime.new('0900')
t.start = @begin_daylight_savings
assert_equal Time.local(2008, 3, 9, 9), t.next(:future).begin

# unambiguous - resolve to tomorrow
t = Chronic::RepeaterTime.new('0400')
t.start = @begin_daylight_savings
assert_equal Time.local(2008, 3, 10, 4), t.next(:future).begin
end

def test_end_past
# ambiguous - resolve to last night
t = Chronic::RepeaterTime.new('900')
t.start = @end_daylight_savings
assert_equal Time.local(2008, 11, 1, 21), t.next(:past).begin

# ambiguous - resolve to this afternoon
t = Chronic::RepeaterTime.new('900')
t.start = Time.local(2008, 11, 2, 22, 0, 0, 0)
assert_equal Time.local(2008, 11, 2, 21), t.next(:past).begin

# ambiguous - resolve to this morning
t = Chronic::RepeaterTime.new('400')
t.start = @end_daylight_savings
assert_equal Time.local(2008, 11, 2, 4), t.next(:past).begin

# unambiguous - resolve to today
t = Chronic::RepeaterTime.new('0400')
t.start = @end_daylight_savings
assert_equal Time.local(2008, 11, 2, 4), t.next(:past).begin

# unambiguous - resolve to yesterday
t = Chronic::RepeaterTime.new('1300')
t.start = @end_daylight_savings
assert_equal Time.local(2008, 11, 1, 13), t.next(:past).begin
end

def test_end_future
# ambiguous - resolve to this morning
t = Chronic::RepeaterTime.new('900')
t.start = @end_daylight_savings
assert_equal Time.local(2008, 11, 2, 9), t.next(:future).begin

# ambiguous - resolve to this afternoon
t = Chronic::RepeaterTime.new('900')
t.start = Time.local(2008, 11, 2, 13, 0, 0, 0)
assert_equal Time.local(2008, 11, 2, 21), t.next(:future).begin

# ambiguous - resolve to tomorrow
t = Chronic::RepeaterTime.new('900')
t.start = Time.local(2008, 11, 2, 22, 0, 0, 0)
assert_equal Time.local(2008, 11, 3, 9), t.next(:future).begin

# unambiguous - resolve to today
t = Chronic::RepeaterTime.new('0900')
t.start = @end_daylight_savings
assert_equal Time.local(2008, 11, 2, 9), t.next(:future).begin

# unambiguous - resolve to tomorrow
t = Chronic::RepeaterTime.new('0400')
t.start = @end_daylight_savings
assert_equal Time.local(2008, 11, 3, 4), t.next(:future).begin
end

end

0 comments on commit a9ad71d

Please sign in to comment.