## Find the most recent timestamp that is not in the future

| categories: orgmode | tags: | View Comments

In a previous post, we looked at a way to jump to a timestamp (post ). A limitation of that post was that our definition of "most recent" included timestamps in the future. Here we further constrain the definition to mean the most recent that is not in the future.

We will do that by getting the list of timestamps, then filtering out future ones.

Here is a list of time-stamps

1. [2014-08-17 Sun]
2. <2014-08-17 Sun 08:23>
3. <2014-08-17 Sun 09:23>
4. [2014-08-18 Mon]
5. [2014-08-17 Sun 13:00]
6. [2014-08-16 Sat 13:00]

Here is the code that filters out future timestamps. As I write this it is <2014-08-17 Sun 08:31>.

(let ((now (with-temp-buffer
;; sachac suggested this way to get a timestamp of "now"
(org-insert-time-stamp (org-read-date t t ".") t)
(buffer-string))))
(remove-if (lambda (entry) (org-time> (car entry) now))
(cl-sort
(org-element-map (org-element-parse-buffer) 'timestamp
(lambda (timestamp)
(cons (org-element-property :raw-value timestamp) (org-element-property :begin timestamp))))
'org-time> :key 'car)))


((<2014-08-17 Sun 08:31> . 811) (<2014-08-17 Sun 08:23> . 613) ([2014-08-17 Sun] . 593) ([2014-08-16 Sat 13:00] . 712))

You can see that only past timestamps show up in the list, and they are sorted with the most recent timestamp. The remove-if function is in 'cl, which is loaded in my init files. You may have to require that if you don't load it in your init files.