9

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.

4

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.

  • Indeed, I checked those out. They even return "Invalid time specification" when fed ISO8601, which is why I'm still looking. – Matthew Piziak Mar 12 '15 at 21:06
  • (parse-time-string (current-time-string)) works here. – wasamasa Mar 12 '15 at 21:17
  • There is a function that claims to do this, but I don't see what the output is supposed to mean. I edited it into my answer. – erikstokes Mar 12 '15 at 21:22
1

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)))
  • 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

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.