Skip to content

Weekly with interval skips the first day if on: is Sunday #42

Description

@d4rky-pl

There's a bug in next_in_recurrence method of Event::Weekly:

      private def next_in_recurrence
        return @date if !initialized? && @options[:on].include?(@date.wday)

        if (next_day = @options[:on].find {|day| day > @date.wday })
          to_add = next_day - @date.wday
        else
          to_add = (7 - @date.wday)                # Move to next week
          to_add += (@options[:interval] - 1) * 7  # Add extra intervals
          to_add += @options[:on].first            # Go to first required day
        end

        new_date = @date.to_date + to_add
        @options[:handler].call(new_date.day, new_date.month, new_date.year)
      end

If @options[:on] is set to Sunday it gets converted to 0 which breaks the check in the if conditional. This in turn causes the code to skip to the else branch and miss one day.

The fix is to map Sunday to be 7 instead of 0, therefore making sure it's compared correctly and no day is skipped:

next_day = @options[:on].map { |day| day == 0 ? 7 : day }.find {|day| day > @date.wday }

I can prep a PR if this gem is still maintained but I saw after last time there was still no release (#40)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions