Skip to content
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
7 changes: 7 additions & 0 deletions lib/rrule/frequencies/frequency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ def self.for_options(options)

attr_reader :context

# For sub-daily frequencies BYHOUR, BYMINUTE and BYSECOND limit the
# occurrences instead of expanding them (RFC 5545 section 3.3.10).
def time_allowed?(option, value)
allowed = context.options[option]
allowed.blank? || allowed.include?(value)
end

def same_month(first_date, second_date)
first_date.month == second_date.month && first_date.year == second_date.year
end
Expand Down
2 changes: 2 additions & 0 deletions lib/rrule/frequencies/hourly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module RRule
class Hourly < Frequency
def possible_days
return [] unless time_allowed?(:byhour, current_date.hour)

[current_date.yday - 1] # convert to 0-indexed
end

Expand Down
2 changes: 2 additions & 0 deletions lib/rrule/frequencies/minutely.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module RRule
class Minutely < Frequency
def possible_days
return [] unless time_allowed?(:byhour, current_date.hour) && time_allowed?(:byminute, current_date.min)

[current_date.yday - 1] # convert to 0-indexed
end

Expand Down
4 changes: 4 additions & 0 deletions lib/rrule/frequencies/secondly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
module RRule
class Secondly < Frequency
def possible_days
return [] unless time_allowed?(:byhour, current_date.hour) &&
time_allowed?(:byminute, current_date.min) &&
time_allowed?(:bysecond, current_date.sec)

[current_date.yday - 1] # convert to 0-indexed
end

Expand Down
43 changes: 43 additions & 0 deletions spec/rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,49 @@
])
end

it 'returns the correct result with an rrule of FREQ=HOURLY;BYHOUR=9,17;COUNT=4' do
rrule = 'FREQ=HOURLY;BYHOUR=9,17;COUNT=4'
dtstart = Time.parse('Tue Sep 2 09:00:00 EDT 1997')
timezone = 'America/New_York'

rrule = RRule::Rule.new(rrule, dtstart: dtstart, tzid: timezone)

expect(rrule.all).to match_array([
Time.parse('Tue Sep 2 09:00:00 EDT 1997'),
Time.parse('Tue Sep 2 17:00:00 EDT 1997'),
Time.parse('Wed Sep 3 09:00:00 EDT 1997'),
Time.parse('Wed Sep 3 17:00:00 EDT 1997'),
])
end

it 'returns the correct result with an rrule of FREQ=MINUTELY;BYHOUR=9;BYMINUTE=0,30;COUNT=4' do
rrule = 'FREQ=MINUTELY;BYHOUR=9;BYMINUTE=0,30;COUNT=4'
dtstart = Time.parse('Tue Sep 2 09:00:00 EDT 1997')
timezone = 'America/New_York'

rrule = RRule::Rule.new(rrule, dtstart: dtstart, tzid: timezone)

expect(rrule.all).to match_array([
Time.parse('Tue Sep 2 09:00:00 EDT 1997'),
Time.parse('Tue Sep 2 09:30:00 EDT 1997'),
Time.parse('Wed Sep 3 09:00:00 EDT 1997'),
Time.parse('Wed Sep 3 09:30:00 EDT 1997'),
])
end

it 'returns the correct result with an rrule of FREQ=SECONDLY;BYHOUR=9;BYMINUTE=0;BYSECOND=0,30;COUNT=2' do
rrule = 'FREQ=SECONDLY;BYHOUR=9;BYMINUTE=0;BYSECOND=0,30;COUNT=2'
dtstart = Time.parse('Tue Sep 2 08:59:50 EDT 1997')
timezone = 'America/New_York'

rrule = RRule::Rule.new(rrule, dtstart: dtstart, tzid: timezone)

expect(rrule.all).to match_array([
Time.parse('Tue Sep 2 09:00:00 EDT 1997'),
Time.parse('Tue Sep 2 09:00:30 EDT 1997'),
])
end

it 'returns the correct result with an rrule of FREQ=DAILY;COUNT=10' do
rrule = 'FREQ=DAILY;COUNT=10'
dtstart = Time.parse('Tue Sep 2 06:00:00 PDT 1997')
Expand Down