lib/say_when/cron_expression.rb
def nth_wday(n, aWday, month, year)
      wday = aWday - 1
      if (!n.between? 1,5) or
         (!wday.between? 0,6) or
         (!month.between? 1,12)
        raise ArgumentError
      end
      t = Time.zone.local year, month, 1
      first = t.wday
      if first == wday
        fwd = 1
      elsif first < wday
        fwd = wday - first + 1
      elsif first > wday
        fwd = (wday+7) - first + 1
      end
      target = fwd + (n-1)*7
      begin
        t2 = Time.zone.local year, month, target
      rescue ArgumentError
        return nil
      end
      if t2.mday == target
        t2
      else
        nil
      end
    end
def nth_wday(n, aWday, month, year)
      wday = aWday - 1
      if (!n.between? 1,5) or
         (!wday.between? 0,6) or
         (!month.between? 1,12)
        raise ArgumentError
      end
      t = Time.zone.local year, month, 1
      first = t.wday
      if first == wday
        fwd = 1
      elsif first < wday
        fwd = wday - first + 1
      elsif first > wday
        fwd = (wday+7) - first + 1
      end
      target = fwd + (n-1)*7
      begin
        t2 = Time.zone.local year, month, target
      rescue ArgumentError
        return nil
      end
      if t2.mday == target
        t2
      else
        nil
      end
    end
compliments of the ruby way