Function: icalendar-nth-weekday-in

icalendar-nth-weekday-in is a byte-compiled function defined in icalendar-utils.el.gz.

Signature

(icalendar-nth-weekday-in N WEEKDAY YEAR &optional MONTH)

Documentation

Return the Nth WEEKDAY in YEAR or MONTH.

If MONTH is specified, it refers to MONTH in YEAR, and N acts as an index for WEEKDAYs within the month. Otherwise, N acts as an index for WEEKDAYs within the entire YEAR.

N should be an integer. If N<0, it counts from the end of the month or year: if N=-1, it refers to the last WEEKDAY in the month or year, if N=-2 the second to last, and so on.

Source Code

;; Defined in /usr/src/emacs/lisp/calendar/icalendar-utils.el.gz
(defun ical:nth-weekday-in (n weekday year &optional month)
  "Return the Nth WEEKDAY in YEAR or MONTH.

If MONTH is specified, it refers to MONTH in YEAR, and N acts as an
index for WEEKDAYs within the month.  Otherwise, N acts as an index for
WEEKDAYs within the entire YEAR.

N should be an integer.  If N<0, it counts from the end of the month or
year: if N=-1, it refers to the last WEEKDAY in the month or year, if
N=-2 the second to last, and so on."
  (if month
      (calendar-nth-named-day n weekday month year)
    (let* ((jan1 (calendar-absolute-from-gregorian (list 1 1 year)))
           (dec31 (calendar-absolute-from-gregorian (list 12 31 year))))
      ;; Adapted from `calendar-nth-named-absday'.
      ;; TODO: we could generalize that function to make month an optional
      ;; argument, but that would mean changing its interface.
      (calendar-gregorian-from-absolute
       (if (> n 0)
           (+ (* 7 (1- n))
              (calendar-dayname-on-or-before
               weekday
               (+ 6 jan1)))
         (+ (* 7 (1+ n))
            (calendar-dayname-on-or-before
             weekday
             dec31)))))))