Skip to content

Instantly share code, notes, and snippets.

@osmatsuda
Last active November 20, 2018 02:31
Show Gist options
  • Save osmatsuda/cb4d127b7962429ed3171f943b9252f0 to your computer and use it in GitHub Desktop.
Save osmatsuda/cb4d127b7962429ed3171f943b9252f0 to your computer and use it in GitHub Desktop.
;; org-time-stamp-this-week.el
;; use `org-agenda-start-on-weekday`
(require 'org-agenda)
(defun org-time-stamp-this-week (&optional from-start-on-weekday)
"Insert a date stamp between a week like a command `org-time-stamp`.
By default, a week start from today to a weekend defined by Org Agenda.
When called with a prefix argument, the start day is the day
`org-agenda-start-on-weekday` on this week."
(interactive "P")
(let* ((ts-range (when (or (org-at-date-range-p)
(org-at-timestamp-p 'lax))
(cons (match-beginning 0) (match-end 0))))
(now (current-time))
(current-dow (nth 6 (decode-time now)))
(start-time (if (>= current-dow org-agenda-start-on-weekday)
(time-add now (* 86400 (- org-agenda-start-on-weekday current-dow)))
(time-add now (* 86400 (- (- org-agenda-start-on-weekday current-dow) 7)))))
(end-time (time-add start-time (* 86400 6)))
(stamp-format "<%Y-%m-%d %a>"))
(unless from-start-on-weekday
(setq start-time now))
(when ts-range
(goto-char (min (car ts-range) (cdr ts-range)))
(delete-region (car ts-range) (cdr ts-range)))
(insert
(if (> 86400 (float-time (time-subtract end-time start-time)))
(format-time-string stamp-format end-time)
(format "%s--%s"
(format-time-string stamp-format start-time)
(format-time-string stamp-format end-time))))))
(provide 'org-time-stamp-this-week)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment