-
Notifications
You must be signed in to change notification settings - Fork 359
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 first weekly occurrences with non-Sunday week start #383
Changes from all commits
5a742cb
c4d0bd8
b2a6867
5d16b5f
26fa680
9eba907
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,39 @@ class WeeklyRule < ValidatedRule | |
|
||
include Validations::WeeklyInterval | ||
|
||
attr_reader :week_start | ||
|
||
def initialize(interval = 1, week_start = :sunday) | ||
super | ||
super(interval) | ||
interval(interval, week_start) | ||
schedule_lock(:wday, :hour, :min, :sec) | ||
reset | ||
end | ||
|
||
# Calculate the effective start time for when the given start time is later | ||
# in the week than one of the weekday validations, such that times could be | ||
# missed by a 7-day jump using the weekly interval, or when selecting from a | ||
# date that is misaligned from the schedule interval. | ||
# | ||
def realign(step_time, start_time) | ||
time = TimeUtil::TimeWrapper.new(start_time) | ||
offset = wday_offset(step_time, start_time) | ||
time.add(:day, offset) if offset | ||
time.to_time | ||
end | ||
|
||
def wday_offset(step_time, start_time) | ||
wday_validations = other_interval_validations.select { |v| v.type == :wday } | ||
return if wday_validations.none? | ||
|
||
days = (step_time - start_time).to_i / ONE_DAY | ||
interval = base_interval_validation.validate(step_time, start_time).to_i | ||
min_wday = TimeUtil.normalize_wday(wday_validations.min_by(&:day).day, week_start) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe here this is the equivalent of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did it this way because (However, this method is only called once per query, and it's a tiny array of 7 so it's not going to be significant either way.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely - sounds good! |
||
step_wday = TimeUtil.normalize_wday(step_time.wday, week_start) | ||
|
||
days + interval - step_wday + min_wday | ||
end | ||
|
||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,8 @@ def dst_adjust? | |
false | ||
end | ||
|
||
def validate(step_time, schedule) | ||
t0, t1 = schedule.start_time.to_i, step_time.to_i | ||
def validate(step_time, start_time) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
t0, t1 = start_time.to_i, step_time.to_i | ||
sec = (t1 - t1 % ONE_HOUR) - | ||
(t0 - t0 % ONE_HOUR) | ||
hours = sec / ONE_HOUR | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always thought these were a little clunky so I'm happy to see them go