(Emacs+Org-Mode) Hydra To Insert Timestamps

What time is it when you have nine ways to insert timestamps with Emacs?

It is time to write a Hydra.

Here is a hydra and some functions for it, and here is how they look:

2018-05-07
05/07/18
05/07/2018
05-07-2018
Monday May 07, 2018
2018-05-07T21-23-59-05-00
2018-05-07T21:24:03-05:00
<2018-05-07 Mon 21:24>
<2018-05-07 Mon>
(defhydra help/hydra/timestamp (:color blue :hint nil)
  "
Timestamps: (_q_uit)
  Date: _I_SO, _U_S, US With _Y_ear and _D_ashes, US In _W_ords
   Date/Time: _N_o Colons or _w_ith
    Org-Mode: _R_ight Now or _c_hoose
"
  ("q" nil)
  ("I" help/insert-datestamp)
  ("U" help/insert-datestamp-us)
  ("Y" help/insert-datestamp-us-full-year)
  ("D" help/insert-datestamp-us-full-year-and-dashes)
  ("W" help/insert-datestamp-us-words)
  ("N" help/insert-timestamp-no-colons)
  ("w" help/insert-timestamp)
  ("R" help/org-time-stamp-with-seconds-now)
  ("c" org-time-stamp))
(global-set-key (kbd "C-t") #'help/hydra/timestamp/body)
(defun help/insert-datestamp ()
  "Produces and inserts a partial ISO 8601 format timestamp."
  (interactive)
  (insert (format-time-string "%F")))
(defun help/insert-datestamp-us ()
  "Produces and inserts a US datestamp."
  (interactive)
  (insert (format-time-string "%m/%d/%y")))
(defun help/insert-datestamp-us-full-year-and-dashes ()
  "Produces and inserts a US datestamp with full year and dashes."
  (interactive)
  (insert (format-time-string "%m-%d-%Y")))
(defun help/insert-datestamp-us-full-year ()
  "Produces and inserts a US datestamp with full year."
  (interactive)
  (insert (format-time-string "%m/%d/%Y")))
(defun help/insert-datestamp-us-words ()
  "Produces and inserts a US datestamp using words."
  (interactive)
  (insert (format-time-string "%A %B %d, %Y")))
(defun help/insert-timestamp-no-colons ()
  "Inserts a full ISO 8601 format timestamp with colons replaced by hyphens."
  (interactive)
  (insert (help/get-timestamp-no-colons)))
(defun help/insert-datestamp ()
  "Produces and inserts a partial ISO 8601 format timestamp."
  (interactive)
  (insert (format-time-string "%F")))
(defun help/get-timestamp-no-colons ()
  "Produces a full ISO 8601 format timestamp with colons replaced by hyphens."
  (interactive)
  (let* ((timestamp (help/get-timestamp))
         (timestamp-no-colons (replace-regexp-in-string ":" "-" timestamp)))
    timestamp-no-colons))
(defun help/get-timestamp ()
  "Produces a full ISO 8601 format timestamp."
  (interactive)
  (let* ((timestamp-without-timezone (format-time-string "%Y-%m-%dT%T"))
         (timezone-name-in-numeric-form (format-time-string "%z"))
         (timezone-utf-offset
          (concat (substring timezone-name-in-numeric-form 0 3)
                  ":"
                  (substring timezone-name-in-numeric-form 3 5)))
         (timestamp (concat timestamp-without-timezone
                            timezone-utf-offset)))
    timestamp))
(defun help/insert-timestamp ()
  "Inserts a full ISO 8601 format timestamp."
  (interactive)
  (insert (help/get-timestamp)))
(defun help/org-time-stamp-with-seconds-now ()
  (interactive)
  (let ((current-prefix-arg '(16)))
    (call-interactively 'org-time-stamp)))

2 thoughts on “(Emacs+Org-Mode) Hydra To Insert Timestamps”

  1. Thanks! One thing I noticed is that when I use
    * HEADING
    SCHEDULED:
    Leaving no space between heading and timestamp, the item appears in the agenda as a scheduled item. however, if I leave a space or have text in between heading and timestamp, like this:
    * HEADING
    Text Text Text
    SCHEDULED:
    Then it appears in the agenda as a one-time timestamp, and will behave like a one-time timestamp, not like an scheduled item (meaning, it will behave as if I had written and not SCHEDULED: .
    Any insights about this?
    Thanks in advance

    1. Although I’m not an agenda user my gut feeling is that when we insert a timestamp using the method above that “it is not part of the Agenda machinery”, so “Agenda doesn’t know that it is a scheduled item”. Thoughts?

Leave a Reply

Your email address will not be published. Required fields are marked *