runt icon indicating copy to clipboard operation
runt copied to clipboard

REMonth negative range enhancement request

Open salex opened this issue 12 years ago • 0 comments

I discovered Runt this week while trying to implement an iCalender type recurring event in a Rails application. I see were the icalendar tests where included, but many test would not work because of lack support for negative mday range.

I'm not very proficient in Ruby, but I attacked the problem by overwriting the REMonth include? method to support negative ranges (which you accept, but don't work)


module Runt    
  class REMonth
    include TExprUtils

    def include?(date)
      if @range.begin < 0 || @range.end < 0
        include_negative(date)
      else
        @range.include? date.mday
      end
    end

    def include_negative(date)
      eom_mday = max_day_of_month(date)
      range = (@range.begin < 0 ? eom_mday + @range.begin + 1 : @range.begin)..(@range.end < 0 ? eom_mday + @range.end + 1 : @range.end)
      range.include? date.mday
    end
  end
end

I couldn't get the tests to run (didn't clone Runt), but this patch seems to work.

# from icalendar test 18

start_date = DateTime.parse("US-Eastern:19970930T090000") #Sep 30, 1997
end_date   = start_date + 365 #Sep 30, 1998
test_date = DateTime.parse("US-Eastern:19971031T090000") #Oct 31
#rrule = RecurrenceRule.new("FREQ=MONTHLY;COUNT=10;BYMONTHDAY=1,-1")
te = REMonth.new(1) | REMonth.new(-1) #first and last days of the month
results = te.dates(DateRange.new(start_date, end_date), 10)

I did notice that if I used Time instead of DateTime and got an error:

results = te.dates(DateRange.new(start_date, end_date), 10)
TypeError: can't iterate from Time

I just have to watch that when I actually try to do something.

salex avatar Jul 29 '13 11:07 salex