13

The format-time-string function takes a time argument like '(21761 64499 350937 0) and a format string like "%y-%m-%d %a %H:%M" and returns something like "15-03-12 Thu 16:49".

What is the inverse function, which takes "15-03-12 Thu 16:49" and "%y-%m-%d %a %H:%M", and returns '(21761 64499 350937 0)?

I am trying to parse times out of a website so as to convert them into org-mode timestamps, but I'm having trouble finding the right function in the emacs documentation.

Edit I'm promoting this from my comment to erikstoke's answer. parse-time-string and org-parse-time-string are insufficient because while they parse many standard time strings, they do not allow for specified formats. Both functions are rather incomprehensive. In particular, they lack ISO8601 support.

Matthew Piziak
  • 5,958
  • 3
  • 29
  • 77

2 Answers2

5

parse-time-string parses "standard" time strings, as does org-parse-time-string. The documentation claims the latter will be faster. Neither gives a way to specify the format of the string.

There is also an undocumented function parse-iso8601-time-string that parses ISO8601 into something. It isn't clear to me what the output is supposed to be.

erikstokes
  • 12,686
  • 2
  • 34
  • 56
2

I had a similar situation but couldn't find anything working out of the box. But since org-time-stamp handles all kinds of formats, I figured the code was there. The function below will insert an org time stamp from a provided time string. I haven't tested it for all kinds of times, but for "15-03-12 Thu 16:49" it will give "[2015-03-12 tor 16:49]". ("tor" is Thursday in my locale).

So this answer is not fully matching the question, since you don't have a way of providing the format string, but hopefully it will work out anyway. It should understand all formats that org-time-stamp handles.

(defun jk-org-insert-time-stamp (time-string)
  (interactive "sTime: ")
  (let ((default-time (apply 'encode-time (decode-time))))
    (org-insert-time-stamp
     (apply 'encode-time (org-read-date-analyze time-string default-time default-time)) t t)))
Sean Allred
  • 6,861
  • 16
  • 85
  • this does not answer the question. The question was, `how can I parse a string with a specified format?` This function inserts a timestring with a specified format. – Cheeso Nov 21 '17 at 20:10