Go back to the main page.

lirons tildelog

html test

Skipping weekends when scheduling items with org-mode

December 05, 2024 — ~liron

I am using the agenda feature of org-mode quite heavily to plan the things I need to do. This includes recurring items that I need to do every day or every other day. For these, I use the +1d or +2d syntax respectively. However, this sometimes causes items to being scheduled on Saturday or Sunday, which is something I don’t want for things I do at the office.

Interestingly, I could not find anything about scheduling items only from Mon-Fri, besides non-solutions such as “create 5 items for each day and schedule them weekly”.

So I had to come up with my own solution:

(defun my-org-hook-for-repeat-not-on-weekend()
  (when (org-property-values "NO_WEEKEND")
    ;; Get time from item at POINT
    (let* ((scheduled-time (org-get-scheduled-time (point)))
           ;; Convert to timestamp - required for the next step
           (seconds-timestamp (time-to-seconds scheduled-time))
           ;; Convert to decoded time - required to find out the weekday
           (decoded-time (decode-time seconds-timestamp))
           ;; Get weekday
           (weekday (decoded-time-weekday decoded-time)))

      (when (> weekday 5) ;; Saturday -> move to Sunday
        (setq decoded-time
              (decoded-time-add decoded-time (make-decoded-time :day 2))))

      (when (> weekday 6) ;; Sunday - move to Monday
        (setq decoded-time
              (decoded-time-add decoded-time (make-decoded-time :day 1))))

      (let ((encoded-time (encode-time decoded-time)))
            (org-schedule nil encoded-time))

    ))
  )

(add-hook ‘org-todo-repeat-hook 'my-org-hook-for-repeat-not-on-weekend) ```

This code snippet adds a hook, which is executed whenever a repeated item is re-scheduled. If it has the property NO_WEEKEND, it checks the scheduled date and moves it appropriately. Feel free to copy it under the GPLv3 if this is useful for you.

tags: emacs