Class: Vpim::Rrule

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vpim/rrule.rb

Overview

Implements the iCalendar recurrence rule syntax. See etc/rrule.txt for the syntax description and examples from RFC 2445. The description is pretty hard to understand, but the examples are more helpful.

The implementation is reasonably complete, but still lacks support for:

Recurrence by date (RDATE) and exclusions (EXDATE, EXRULE).

TODO - BYWEEKNO: rules that are limited to particular weeks in a year.

TODO - BYHOUR, BYMINUTE, BYSECOND: trivial to do, but I don’t have an immediate need for them.

TODO - new API? -> Rrule#infinite?

Examples

  • rrule.txt: utility for printing recurrence rules

Defined Under Namespace

Classes: DaySet, Maker

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dtstart, rrule = nil) ⇒ Rrule

The recurrence rule, rrule, specifies how to generate a set of times from a start time, dtstart (which must the first of the set of recurring times). If rrule is nil, the set contains only dtstart.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/vpim/rrule.rb', line 60

def initialize(dtstart, rrule = nil)
   @dtstart = dtstart.getlocal
   # The getlocal is a hack so that UTC times get converted to local,
   # because yielded times are always local, because we don't support
   # timezones.
   @rrule = rrule

   # Freq is mandatory, but must occur only once.
   @freq = nil

   # Both Until and Count must not occur, neither is OK.
   @until = nil
   @count = nil

   # Interval is optional, but defaults to 1.
   @interval = 1

   # WKST defines what day a week begins on, the default is monday.
   @wkst = 'MO'

   # Recurrence can modified by these.
   @by = {}

   if @rrule
     @rrule.scan(/([^;=]+)=([^;=]+)/) do |key,value|
       key.upcase!
       value.upcase!

       case key
       when 'FREQ'
         @freq = value

       when 'UNTIL'
         if @count
           raise "found UNTIL, but COUNT already specified"
         end
         @until = Rrule.time_from_rfc2425(value)

       when 'COUNT'
         if @until
           raise "found COUNT, but UNTIL already specified"
         end
         @count = value.to_i

       when 'INTERVAL'
         @interval = value.to_i
         if @interval < 1
           raise "interval must be a positive integer"
         end

       when 'WKST'
         # TODO - check value is MO-SU
         @wkst = value

       else
         @by[key] = value
       end
     end

     if !@freq
       # TODO - this shouldn't be an arg error, but a FormatError, its not the
       # caller's fault!
       raise ArgumentError, "recurrence rule lacks a frequency"
     end
   end
end

Class Method Details

.time_from_rfc2425(str) ⇒ Object

:nodoc:



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/vpim/rrule.rb', line 448

def self.time_from_rfc2425(str) #:nodoc:
  # With ruby1.8 we can use DateTime to do this quick-n-easy:
  #  dt = DateTime.parse(str)
  #  Time.local(dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec, 0)

  # The time can be a DATE or a DATE-TIME, the latter always has a 'T' in it.

  if str =~ /T/
    d = Vpim.decode_date_time(str)
    # We get [ year, month, day, hour, min, sec, usec, tz ]
    if(d.pop == "Z")
      t = Time.gm(*d)
    else
      t = Time.local(*d)
    end
  else
    d = Vpim.decode_date(str)
    # We get [ year, month, day ]
    # FIXME - I have to choose gm or local, though neither makes much
    # sense. This is a bit of a hack - what we should really do is return
    # an instance of Date, and Time should allow itself to be compared to
    # Date... This hack will give odd results when comparing times, because
    # it will create a Time on the right date but whos time is 00:00:00.
    t = Time.local(*d)
  end
  if t.month != d[1] || t.day != d[2] || (d[3] && t.hour != d[3])
    raise Vpim::InvalidEncodingError, "Error - datetime does not exist"
  end
  t
end

Instance Method Details

#byday_in_monthly(year, mon, byday) ⇒ Object

:nodoc:



499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/vpim/rrule.rb', line 499

def byday_in_monthly(year, mon, byday) #:nodoc:
  dates = []

  byday.each do |rule|
    if rule[0].empty?
      n = nil
    else
      n = rule[0].to_i
    end
    dates |= DateGen.bywday(year, mon, Date.str2wday(rule[1]), n)
  end
  dates.sort!
  dates
end

#byday_in_weekly(year, mon, day, wkst, byday) ⇒ Object

:nodoc:



514
515
516
517
518
519
520
521
522
523
# File 'lib/vpim/rrule.rb', line 514

def byday_in_weekly(year, mon, day, wkst, byday) #:nodoc:
  #    debug ["day", year,mon,day,wkst,byday]
  days = byday.map{ |_, byday| Date.str2wday(byday) }
  week = DateGen.weekofdate(year, mon, day, wkst)
  #    debug [ "week", dates ]
  week.delete_if do |d|
    !days.include?(d.wday)
  end
  week
end

#bymonthday(year, bymday) ⇒ Object

:nodoc:



479
480
481
482
483
484
485
486
487
# File 'lib/vpim/rrule.rb', line 479

def bymonthday(year, bymday) #:nodoc:
  dates = []

  bymday.each do |mday|
    dates |= DateGen.bymonthday(year, nil, mday[0].to_i)
  end
  dates.sort!
  dates
end

#byyearday(year, byyday) ⇒ Object

:nodoc:



489
490
491
492
493
494
495
496
497
# File 'lib/vpim/rrule.rb', line 489

def byyearday(year, byyday) #:nodoc:
  dates = []

  byyday.each do |yday|
    dates << Date.ordinal(year, yday[0].to_i)
  end
  dates.sort!
  dates
end

#each(dountil = nil) ⇒ Object

Yields for each ytime in the recurring set of events.

Warning: the set may be infinite! If you need an upper bound on the number of occurrences, you need to implement a count, or pass a time, dountil, which will not be iterated past (i.e. all times yielded will be less than dountil).

Also, iteration will not currently continue past the limit of a Time object, which is some time in 2037 with the 32-bit time_t common on most systems.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/vpim/rrule.rb', line 143

def each(dountil = nil) #:yield: ytime
  t = @dtstart.clone

  # Time.to_a => [ sec, min, hour, day, month, year, wday, yday, isdst, zone ]

  # Every event occurs at its start time, but only if the start time is
  # earlier than DOUNTIL...
  if !dountil || t < dountil
    yield t
  end
  count = 1

  # With no recurrence, DTSTART is the only occurrence.
  if !@rrule
    return self
  end

  loop do
    # Build the set of times to yield within this interval (and after
    # DTSTART)

    days  = DaySet.new(t)
    hour  = nil
    min   = nil
    sec   = nil

    # Need to make a Dates class, and make month an instance of it, and add
    # the "intersect" operator.

    case @freq
      #when 'YEARLY' then
      # Don't need to keep track of year, all occurrences are within t's
      # year.
    when 'MONTHLY'  then  days.month = t.month
    when 'WEEKLY'   then  #days.month = t.month
      # TODO - WEEKLY
    when 'DAILY'    then  days.mday = t.month, t.mday
    when 'HOURLY'   then  hour  = [t.hour]
    when 'MINUTELY' then  min   = [t.min]
    when 'SECONDLY' then  sec   = [t.sec]
    end

  #      debug [t, days]
    # Process the BY* modifiers in RFC defined order:
    #  BYMONTH,
    #  BYWEEKNO,
    #  BYYEARDAY,
    #  BYMONTHDAY,
    #  BYDAY,
    #  BYHOUR,
    #  BYMINUTE,
    #  BYSECOND,
    #  BYSETPOS

    bymon = [nil]

    if @by['BYMONTH']
      bymon = @by['BYMONTH'].split(',')
      bymon = bymon.map { |m| m.to_i }
  #        debug bymon

      # In yearly, at  this point, month will always be nil. At other
      # frequencies, it will not.
      days.intersect_bymon(bymon)

  #        debug days
    end

    # TODO - BYWEEKNO

    if @by['BYYEARDAY']
      byyday = @by['BYYEARDAY'].scan(/,?([+-]?[1-9]\d*)/)
  #        debug byyday
      dates = byyearday(t.year, byyday)
      days.intersect_dates(dates)
    end

    if @by['BYMONTHDAY']
      bymday = @by['BYMONTHDAY'].scan(/,?([+-]?[1-9]\d*)/)
  #        debug bymday
      # Generate all days matching this for all months. For yearly, this
      # is what we want, for anything of monthly or higher frequency, it
      # is too many days, but that's OK, since  the month will already
      # be specified and intersection will eliminate the out-of-range
      # dates.
      dates = bymonthday(t.year, bymday)
  #        debug dates
      days.intersect_dates(dates)
  #        debug days
    end

    if @by['BYDAY']
      byday = @by['BYDAY'].scan(/,?([+-]?[1-9]?\d*)?(SU|MO|TU|WE|TH|FR|SA)/i)

      # BYDAY means different things in different frequencies. The +n+
      # is only meaningful when freq is yearly or monthly.

      case @freq
        when 'YEARLY'
          dates = bymon.map { |m| byday_in_monthly(t.year, m, byday) }.flatten
        when 'MONTHLY'
          dates = byday_in_monthly(t.year, t.month, byday)
        when 'WEEKLY'
          dates = byday_in_weekly(t.year, t.month, t.mday, @wkst, byday)
        when 'DAILY', 'HOURLY', 'MINUTELY', 'SECONDLY'
          # Reuse the byday_in_monthly. Current day is already specified,
          # so this will just eliminate the current day if its not allowed
          # in BYDAY.
          dates = byday_in_monthly(t.year, t.month, byday)
      end

  #        debug dates
      days.intersect_dates(dates)
  #        debug days
    end

    # TODO - BYHOUR, BYMINUTE, BYSECOND

    hour   = [@dtstart.hour]   if !hour
    min    = [@dtstart.min]    if !min
    sec    = [@dtstart.sec]    if !sec

  #      debug days

    # Generate the yield set so BYSETPOS can be evaluated.
    yset = []

    days.each do |m,d|
      hour.each do |h|
        min.each do |n|
          sec.each do |s|
            y = Time.local(t.year, m, d, h, n, s, 0)

            next if y.hour != h

            yset << y
          end
        end
      end
    end

    if @by['BYSETPOS']
      bysetpos = @by['BYSETPOS'].split(',')
      yset = bysetpos.map do |i|
        i = i.to_i
        case
        when i < 0
          # yset[-1] is last
          yset[i]
        when i > 0
          # yset[1] is first
          yset[i-1]
        else
          # ignore invalid syntax
        end
      end.compact # set positions out of scope will be nil, RFC says ignore them
    end

    # Yield the occurrence, if we haven't gone over COUNT, or past UNTIL, or
    # past the end of representable time.

    yset.each do |y|
      # The generated set can sometimes generate results earlier
      # than the DTSTART, skip them. Also, we already yielded
      # DTSTART, skip it.
      next if y <= @dtstart

      count += 1

      # We are done if current count is past @count.
      if(@count && (count > @count))
        return self
      end

      # We are done if current time is past @until.
      if @until && (y > @until)
        return self
      end
      # We are also done if current time is past the
      # caller-requested until.
      if dountil && (y >= dountil)
        return self
      end
      yield y
    end

    # Add @interval to @freq component

    # Note - when we got past representable time, the error is:
    #   time out of range (ArgumentError)
    # Finish when we see this.
    begin
      case @freq
        when 'YEARLY' then
          t = t.plus_year(@interval)

        when 'MONTHLY' then
          t = t.plus_month(@interval)

        when 'WEEKLY' then
          t = t.plus_day(@interval * 7)

        when 'DAILY' then
          t = t.plus_day(@interval)

        when 'HOURLY' then
          t += @interval * 60 * 60

        when 'MINUTELY' then
          t += @interval * 60

        when 'SECONDLY' then
          t += @interval

        when nil
          return self
      end
    rescue ArgumentError
      return self if $!.message =~ /^time out of range$/

      raise ArgumentError, "#{$!.message} while adding interval to #{t.inspect}"
    end

    return self if dountil && (t > dountil)
  end
end

#each_until(dountil) ⇒ Object

Return an Enumerable, it’s #each() will yield over all occurrences up to (and not including) time dountil.



129
130
131
# File 'lib/vpim/rrule.rb', line 129

def each_until(dountil)
  Vpim::Enumerator.new(self, dountil)
end