I have this

(defun insert-dateutc ()
  (shell-command "printf %s $(date +\"%Y-%m-%dT%H:%M:%S\")" t))

which I want to use in this

(defun foo1 ()
  (format "this is the date-time %s" (insert-dateutc)))

but the output is the elisp id number of the function call (yes?)

==> "this is the date-time 1185"
2015-11-18T20:31:30 ; <-- inserted

with the actual desired return left outside. I need to suppress that number and get the actual date-time output. Gosh, I know it's something simple...

  • 1
    Is your question really about getting something from the shell process, to use in Emacs, or is it just about being able to display or return or insert a timestamp string in Emacs? If the latter, just use current-time-string or use current-time with format-time-string. – Drew Nov 18 '15 at 20:54
  • I want to use the output of that shell-command in the first function, i.e., not just any timestamp. Obviously I could just put the (shell-command ...) part in foo1, but it does the same thing AFAIK. – 147pm Nov 18 '15 at 20:57
  • You can use call-process or start-process, to get more control. Otherwise, the shell-command call you are using puts the output of the printf command in the current buffer, at point. You could wrap the call in with-current-buffer-window or similar (in which case you don't need the second arg), to recuperate the output. – Drew Nov 18 '15 at 21:01
  • (defun foo1 () (format "this is the date-time %S" (shell-command "date +\"%Y-%m-%dT%H:%M:%S\"" t))) is without the printf, but with the same bad results. The printf was only to avoid a newline. What exactly should I do with call-process? – 147pm Nov 18 '15 at 21:08
  • . . . of course if I can get ISO 8601 format of UTC out of format-time-string I'm home free. . . – 147pm Nov 18 '15 at 21:14

If what you want is just a timestamp, you can use

(defvar user-time-format
  (eval-when-compile (or (getenv "STRFTIME") "%Y-%m-%d %a %H:%M:%S %Z"))
  "The time format for the user to set.")
(defun user-time-format (&optional insert)
  "Return or insert the time string according to `user-time-format'."
  (interactive "P")
  (let ((str (format-time-string user-time-format)))
    (if insert (insert str)
        (if (called-interactively-p 'interactive) (message "%s" str) str))))

except you would set user-time-format to "%Y-%m-%dT%H-%M-%S".

However, your question raises an interesting issue, so let us discuss it too.

Your insert-dateutc is fine (but see below), but it inserts the date instead of returning it, so foo has to correct that:

(defun foo ()

(format "timestamp = [%s]" (foo))
==> "timestamp = [2015-11-18T16:37:12]"

Finally, take a look at C-h f shell-command RET. It says discourages the use of this function in lisp code, recommending call-process instead:

  (call-process "date" nil t nil "+%Y-%m-%dT%H:%M:%S")
  (buffer-substring-no-properties 1 (line-end-position 0)))
==> "2015-11-18T16:47:27"
  • Your foo (with the format bundled inside a cl-flet) works fine -- in my scratch, but when I call it in my org buffer . . . nothing appears. Apparently the format form doesn't "print"? – 147pm Nov 19 '15 at 1:10
  • format converts its arguments to a string. you might want to pass it to insert which will insert the string into the buffer or use message instead of format to report the text in the echo area. – sds Nov 19 '15 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.