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

Adds in Spring DST fix for Issue 65 with tests. #83

Open
wants to merge 1 commit 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
10 changes: 9 additions & 1 deletion lib/clockwork/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,15 @@ def execute
end

def elapsed_ready?(t)
@last.nil? || (t - @last.to_i).to_i >= @period
# We only need to calculate this if the period is >= 1 day. The UTC offset check only makes sense
# for periods >= 1.day since these are the shortest period where increasing the date by a day, may not
# exactly match the exact elapsed time.
change_in_utc_offset = if @period >= 1.day
t.utc_offset - (t - @period).utc_offset
else
0
end
@last.nil? || (t - @last.to_i).to_i >= (@period - change_in_utc_offset)
end

def run_at?(t)
Expand Down
31 changes: 31 additions & 0 deletions test/event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,36 @@
assert_equal true, event.run_now?(Time.now)
end
end

describe 'with Pacific Time TZ' do
before do
@manager = Class.new
@manager.stubs(:config).returns({ :tz => 'America/Los_Angeles' })
end

describe 'event non DST to DST transition' do
it 'returns true when it crosses the transition' do
event = Clockwork::Event.new(@manager, 1.day, nil, nil)
starting_time = Time.utc(2022, 3, 13, 6)
assert_equal true, event.run_now?(starting_time)
event.instance_variable_set('@last', starting_time)
# This will return true, since the UTC offset is moved forward by 1 hour. With the 1 hour, it will be
# equal to 1 day since the last time it was run.
assert_equal true, event.run_now?(starting_time + 23.hours)
end
end

describe 'event DST to non DST transition' do
it 'returns true when it crosses the transition' do
event = Clockwork::Event.new(@manager, 1.day, nil, nil)
starting_time = Time.utc(2021, 11, 7, 5)
assert_equal true, event.run_now?(starting_time)
event.instance_variable_set('@last', starting_time)
# This returns false since it hasn't reached a full 'real' day yet.
assert_equal false, event.run_now?(starting_time + 24.hours)
assert_equal true, event.run_now?(starting_time + 25.hours)
end
end
end
end
end
Loading