Using a formula to compute tasks duration in org-mode tables
I recently became more mindful about how I spend my time, because time is so precious. Every night, I plan how I will be spending time the next day. As you may expect if you have been reading my blog, I am using org-mode to track that; more specifically, I put the entries in a table like this one:
|---------------+--------------+------------|
| Time | Activity | Duration |
|---------------+--------------+------------|
| 7:30 - 8:00 | Breakfast | 30 minutes |
| 8:00 - 12:00 | Work | 4 hours |
| 12:00 - 12:30 | Go on a walk | 30 minutes |
| 12:30 - 13:00 | Eat lunch | 30 minutes |
I don't like to do the manual work of figuring out the duration from the time. Following one of my favorite xkcd, "is it worth the time?" I analyzed the problem:
I am probably doing this 5 to 50 times per day depending on how many entries I have, and it takes me 5 seconds. So if I can solve the problem and automate it in less than 12 hours, it is worth it if I keep that routine for 5 years. Even if I keep the routine for 6 months, I can spend over an hour solving the problem! It took me a few minutes to come up with the following emacs-lisp snippet to compute a time difference:
(require 'ts)
(defun to-duration (tdiff)
(let* ((a (s-split " - " tdiff))
(t1 (ts-parse (nth 0 a)))
(t2 (ts-parse (nth 1 a))))
(if (> (ts-unix t2) (ts-unix t1))
(ts-human-format-duration
(ts-difference t2 t1))
(ts-human-format-duration
(ts-difference (ts-adjust 'hour +12 t2) (ts-adjust 'hour -12 t1))))))
(to-duration "8:30 - 9:00") ;;-> 30 minutes
(to-duration "20:30 - 2:00") ;;-> 5 hours, 30 minutes
Then all that's left to do is adding a formula to the table to get the duration computed automatically:
|---------------+--------------+------------|
| Time | Activity | Duration |
|---------------+--------------+------------|
| 7:30 - 8:00 | Breakfast | 30 minutes |
| 8:00 - 12:00 | Work | 4 hours |
| 12:00 - 12:30 | Go on a walk | 30 minutes |
| 12:30 - 13:00 | Eat lunch | 30 minutes |
#+TBLFM: $3='(to-duration $1)'