0

Question: How can I retrieve, over a set of files S (in particular: over my agenda files + their archive files) the total time clocked (as the return value of an elisp function; not as a clock table)? So for example, if I have:

* Header1
  :LOGBOOK:
  CLOCK: [2019-05-01 Sat 18:10]--[2019-05-01 Sat 18:30] =>  0:20
  :END:

# File2
* Header2
  :LOGBOOK:
  CLOCK: [2019-05-11 Sat 18:10]--[2019-05-11 Sat 18:11] =>  0:01
  :END:
* Header3
  :LOGBOOK:
  CLOCK: [2019-05-11 Sat 18:10]--[2019-05-11 Sat 18:20] =>  0:10
  :END:

I'd like to run a function like (sum-all-time-from "[2019-05-10]" "[2019-05-12]") to get as answer ~0:11~.

2

Here's how you can sum the times of each subtree within a time range (deprecated):

(org-map-entries
 (lambda ()
   (org-duration-from-minutes
    (org-clock-sum "[2019-05-10]" "[2019-05-12]")))
 nil 'agenda-with-archives)

The org-clock-sum function is run on every subtree in the buffer, but since we are using org-map-entries we probably only need to run org-clock-sum once with the subtree narrowed. If anything this function should run a lot faster:

(org-duration-from-minutes
 (apply '+ (org-map-entries
            (lambda ()
              (save-restriction
                (org-narrow-to-subtree)
                (org-clock-sum "[2019-05-10]" "[2019-05-12]")))
            nil 'agenda-with-archives)))

Here's an explanation of what each part of the function does: First, org-map-entries calls org-clock-sum at each headline of the agenda files and any archive files associated with them, but to avoid repetition, org-clock-sum is run with the subtree narrowed. Lastly, we add the time sum of all subtrees and use org-duration-from-minutes to get the duration string of the number of minutes, so that 61 minutes becomes "1:01" and so on.

  • I think this is correct but it is a bit tricky: in particular, it depends on what org-clock-sum returns, something which is not specified in the docstring of the function (arguably, a documentation bug). I would encourage you to add a couple of short paragraphs of explanation (I would suggest one for org-map-entries explaining the arguments and one for org-clock-sum explaining what it does and what it returns, plus a line of explanation for the much simpler org-minutest-to-clocksum-string). That would make the answer much more self-contained and therefore more valuable. – NickD May 13 at 3:07
  • This function overcounts time that is nested in subtrees (the further nesting, the more it overcounts). I think I fixed it here. – George May 13 at 18:31
  • 1
    Can you provide an example where it overcounts? Maybe edit your question and add it? – NickD May 14 at 22:28
  • I updated the answer. Please test and report back (cc: @NickD, @George). – jagrg May 17 at 13:15
  • Very nice: thanks! I've already upvoted, so cannot do so again. Let's see what @George sees in his tests. – NickD May 29 at 22:36

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.