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 first weekly occurrences with non-Sunday week start #383

Merged

Conversation

avit
Copy link
Collaborator

@avit avit commented Mar 20, 2017

This change corrects the wday offset to account for the week start option in weekly rules.

Some refactoring was done to avoid adding conditionals and more complexity into the Schedule class which was coordinating this:

  • Rules no longer depend on a Schedule object to validate: to simplify, they only require Time objects and calculate the next time coordinate from them.
  • Rules no longer have a second argument for week_start except for the Weekly rule that uses it. This is to avoid confusion about where this option is actually used.

Much thanks to #360 and #284 for spotting the core issue here.

@avit avit requested a review from seejohnrun March 20, 2017 07:26
@avit avit force-pushed the issues/360-wday_weekly_schedule_problem branch from 3b2f2cf to e4f2d29 Compare March 20, 2017 07:44
@avit avit force-pushed the issues/360-wday_weekly_schedule_problem branch 2 times, most recently from f251c57 to c922ce1 Compare March 20, 2017 16:26
@seejohnrun
Copy link
Collaborator

Sorry for the delay - will have this back to you tonight!

avit added 5 commits March 27, 2017 00:53
Instead of requiring a Schedule instance, rules only need a start time to align
themselves to. This will allow the validate methods to be used in different
contexts.
This option only applies to weekly rules and should not be listed everywhere
to avoid confusion.
Simplify the Schedule and only handle realigning the start wday for weekly rules
where it's needed.
@avit avit force-pushed the issues/360-wday_weekly_schedule_problem branch from c922ce1 to 7fe1719 Compare March 27, 2017 07:54
Realignment for start time doesn't apply to any rule types other than
WeeklyRule. It also must be kept separate from the schedule's own start time
because it's possible for it to move backwards to the start of the week.
@avit avit force-pushed the issues/360-wday_weekly_schedule_problem branch from 7fe1719 to 9eba907 Compare March 27, 2017 08:11
@avit
Copy link
Collaborator Author

avit commented Mar 27, 2017

Thanks, I'd just like a second pair of eyes on this since it's pretty big. (I hope it should make sense to review per-commit.)

Copy link
Collaborator

@seejohnrun seejohnrun left a comment

Choose a reason for hiding this comment

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

👍 and test coverage looks good too

left a few general comments but nothing that should block this.

thank you!

@@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

@@ -4,7 +4,7 @@ class HourlyRule < ValidatedRule

include Validations::HourlyInterval

def initialize(interval = 1, week_start = :sunday)
def initialize(interval = 1)
Copy link
Collaborator

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


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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe here this is the equivalent of map(&:day).min which may read nicer

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Did it this way because min_by takes one pass over the array and map.min takes two.

(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.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Definitely - sounds good!

@avit avit merged commit e960b98 into ice-cube-ruby:master Mar 28, 2017
@avit avit deleted the issues/360-wday_weekly_schedule_problem branch March 28, 2017 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants