Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Daylight Savings parsing #396

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions lib/chronic/repeaters/repeater_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,28 @@ def next(pointer)
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.time + offset_fix, midnight + half_day + @type.time + offset_fix, tomorrow_midnight + @type.time].each do |t|
[midnight, midnight + half_day, tomorrow_midnight].each do |base_time|
t = adjust_daylight_savings_offset(midnight, base_time + @type.time)
(@current_time = t; throw :done) if t >= @now
end
else
[midnight + @type.time + offset_fix, tomorrow_midnight + @type.time].each do |t|
[midnight, tomorrow_midnight].each do |base_time|
t = adjust_daylight_savings_offset(midnight, base_time + @type.time)
(@current_time = t; throw :done) if t >= @now
end
end
else # pointer == :past
if @type.ambiguous?
[midnight + half_day + @type.time + offset_fix, midnight + @type.time + offset_fix, yesterday_midnight + @type.time + half_day].each do |t|
[midnight + half_day, midnight, yesterday_midnight + half_day].each do |base_time|
t = adjust_daylight_savings_offset(midnight, base_time + @type.time)
(@current_time = t; throw :done) if t <= @now
end
else
[midnight + @type.time + offset_fix, yesterday_midnight + @type.time].each do |t|
[midnight, yesterday_midnight].each do |base_time|
t = adjust_daylight_savings_offset(midnight, base_time + @type.time)
(@current_time = t; throw :done) if t <= @now
end
end
Expand All @@ -119,6 +120,26 @@ def next(pointer)
Span.new(@current_time, @current_time + width)
end

# Every time a time crosses a Daylight Savings interval we must adjust the
# current time by that amount. For example, if we take midnight of Daylight
# Savings and only add an hour, the offset does not change:
#
# Time.parse('2008-03-09 00:00')
# => 2008-03-09 00:00:00 -0800
# Time.parse('2008-03-09 00:00') + (60 * 60)
# => 2008-03-09 01:00:00 -0800
#
# However, if we add 2 hours, we notice the time advances to 03:00 instead of 02:00:
#
# Time.parse('2008-03-09 00:00') + (60 * 60 * 2)
# => 2008-03-09 03:00:00 -0700
#
# Since we gained an hour and we actually want 02:00, we subtract an hour.
def adjust_daylight_savings_offset(base_time, current_time)
offset_fix = base_time.gmt_offset - current_time.gmt_offset
current_time + offset_fix
end

def this(context = :future)
super

Expand Down
6 changes: 5 additions & 1 deletion test/test_daylight_savings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ def test_end_past
t = Chronic::RepeaterTime.new('1300')
t.start = @end_daylight_savings
assert_equal Time.local(2008, 11, 1, 13), t.next(:past).begin

# unambiguous - resolve to yesterday
t = Chronic::RepeaterTime.new('1300')
t.start = @end_daylight_savings + 60 * 60 * 24 # Advance one day
assert_equal Time.local(2008, 11, 2, 13), t.next(:past).begin
end

def test_end_future
Expand Down Expand Up @@ -114,5 +119,4 @@ def test_end_future
t.start = @end_daylight_savings
assert_equal Time.local(2008, 11, 3, 4), t.next(:future).begin
end

end
4 changes: 4 additions & 0 deletions test/test_parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def test_handle_generic

assert_nil Chronic.parse("1/1/32.1")

time = Chronic.parse("2020-11-01 00:00:00")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests your PR only when system's timezone is one you have, I have different one so for me this test passes either way even without your PR. Need to write such test that would test your specific case with your timezone.

time2 = Time.parse("2020-11-01 00:00:00")
assert_in_delta time, time2, 0.000001

time = Chronic.parse("28th", {:guess => :begin})
assert_equal Time.new(Time.now.year, Time.now.month, 28), time
end
Expand Down