Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compute sleep duration in an emacs table?

Tags:

emacs

org-mode

I have a table containing as inputs the Date, Waketime, and Bedtime. Based on this data I want to calculate the sleep duration of the night before. Below you can see an example of the table I would like to obtain:

| Date             | Waketime | Bedtime | Sleep |
| <2012-09-24 Mon> |     6:00 |   22:00 |       |
| <2012-09-25 Tue> |     8:00 |   01:00 | 10:00 |
| <2012-09-26 Wed> |     7:00 |   23:00 |  6:00 |

When I apply the table formula: #+TBLFM: $4=if((24-$3)<12, (24-$3)+@-1$2, $3+@-1$2);t this doesn't give the wanted result, due to the integers (24, 12...) being interpreted as seconds.

Erroneous result:

| Date             | Waketime | Bedtime |  Sleep |
| <2012-09-24 Mon> |     6:00 |   22:00 |        |
| <2012-09-25 Tue> |     8:00 |   01:00 |   5.01 |
| <2012-09-26 Wed> |     7:00 |   23:00 | -14.99 |

   #+TBLFM: $4=if((24-$3)<12, (24-$3)+@-1$2, $3+@-1$2);t

How can I adjust this formula so it returns the correct sleep duration?

Notice that for computing sleep duration, I have an if-statement that computes the sleep duration differently, depending on the value of Bedtime. If bedtime is before 12h, Bedtime and Waketime are added. If bedtime is past 12h, 24 is first subtracted by Bedtime and subsequently added to Waketime

like image 934
smoens Avatar asked Oct 07 '22 12:10

smoens


1 Answers

It's a hack, because org-mode does not seem to understand calc's HMS notation when creating formulas, but here you are:

   #+TBLFM: $4=if($3 - (12 * 3600) < 0, (24 * 3600), 0) + $2 - $3;T
like image 181
rwb Avatar answered Oct 10 '22 01:10

rwb