From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christian Moe Subject: Re: Time calculation: vsum? Date: Sun, 17 Jul 2011 15:27:32 +0200 Message-ID: <4E22E344.5050503@christianmoe.com> References: <4E2278C6.1020407@christianmoe.com> <87wrfhum6g.wl%markert.michael@googlemail.com> Reply-To: mail@christianmoe.com Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Return-path: Received: from eggs.gnu.org ([140.186.70.92]:38009) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QiRKn-0000fU-07 for emacs-orgmode@gnu.org; Sun, 17 Jul 2011 09:24:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QiRKl-0005Ch-KJ for emacs-orgmode@gnu.org; Sun, 17 Jul 2011 09:24:36 -0400 Received: from mars.hitrost.net ([91.185.211.18]:51735) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QiRKl-0005CY-3T for emacs-orgmode@gnu.org; Sun, 17 Jul 2011 09:24:35 -0400 In-Reply-To: <87wrfhum6g.wl%markert.michael@googlemail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Michael Markert Cc: Org Mode Hi, Michael, First, I just realized I was part wrong: vsum of time values DOES work (sorry, Eric!). | Task 1 | Task 2 | Task 3 | Total | |--------+--------+--------+---------| | 35:00 | 35:00 | 4:00 | 1:14:00 | #+TBLFM: @2$4=vsum($1..$3);T | Task | Time | |--------+-------| | Task 1 | 35:00 | | Task 2 | 35:00 | | Task 3 | 4:00 | |--------+-------| | Total | 1:14 | #+TBLFM: @5$2=vsum(@I..@II);T But in the vertical vsum in the second example above, the right answer is misleadingly formatted, so you'd read it as 1 min 14 seconds, not 1 hr 14 mins. And I'm still failing to get the right vsum /vertically/ with times one hour or above, e.g.: | Task | Time | |--------+---------| | Task 1 | 1:35:00 | | Task 2 | 1:35:00 | | Task 3 | 1:04:00 | |--------+---------| | Total | 3 | #+TBLFM: @5$2=vsum(@I..@II);T Meanwhile, Michael, thanks for your elisp solution -- it's more compact than mine, and I'll be happy to steal it! But support for time calculations -- in calc formulas too -- has been added in 7.6. Yours, Christian On 7/17/11 2:27 PM, Michael Markert wrote: > Hi Christian, > > On 17 Jul 2011, Christian Moe wrote: >> Hi, >> >> Time calculations don't seem to work with vsum (or vmean). >> >> >> | Time | >> |---------| >> | 1:06:00 | >> | 0:52:30 | >> | 2:00:00 | >> |---------| >> | 3 | >> #+TBLFM: @5$1=vsum(@I..@II);T >> >> >> Am I doing something wrong? > > Yes, `vsum' works just on numbers, not times. > >> Could this be made to work? > Here's a elisp function that does what you want (assuming it's > hours:minutes:seconds -- if not tweak the numbers): > > #+begin_src elisp > (defun h-m-s-vsum (times) > (loop for (h m s) in (mapcar (lambda (time) > (mapcar #'string-to-int > (split-string time ":" 'omit-nulls))) > times) > collect (+ (* 3600 h) (* 60 m) s) into seconds > finally (return (let* ((second-sum (apply #'+ seconds)) > (seconds (let ((s (% second-sum 60))) > (decf second-sum s) > s)) > (minutes (let ((m (% second-sum 3600))) > (decf second-sum m) > (truncate (/ m 60)))) > (hours (/ second-sum 3600))) > (format "%s:%s:%s" hours minutes seconds))))) > #+end_src elisp > > The table has to be > > | Time | > |---------| > | 1:06:00 | > | 0:52:30 | > | 2:00:00 | > |---------| > | 3:58:30 | > #+TBLFM: @5$1='(h-m-s-vsum '(@I..@II)) > > Hope that helps. > Maybe there is an easier way. I hope there is ;) > > Michael