emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Christian Moe <mail@christianmoe.com>
To: Martin Halder <martin.halder@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: org table calc and lisp for hh:mm timetable
Date: Tue, 15 Mar 2011 22:47:56 +0100	[thread overview]
Message-ID: <4D7FDE8C.1030103@christianmoe.com> (raw)
In-Reply-To: <0DDD90AF-9B9A-4FE3-8080-74EF01E6E292@gmail.com>

Hi,

This is ingenious! But I have a different solution that borrows 
conversion functions from org-timer.el.

To avoid an insanely long formula, I'll alias those functions with 
shorter names which don't seem to colide with anything in my Emacs.

#+begin_src emacs-lisp
   (defun sec (arg)
     (org-timer-hms-to-secs arg))

   (defun hms (arg)
     (org-timer-secs-to-hms arg))
#+end_src

Now, just do this:

|    Start |    Lunch |     Back |      End |  Sum    |
|----------+----------+----------+----------+---------|
| 08:00:00 | 12:20:00 | 13:00:00 | 17:00:00 | 8:20:00 |
#+TBLFM: $5='(hms (+ (- (sec $4) (sec $3)) (- (sec $2) (sec $1))))

This already works for me, because my main interest here is in keeping 
track of my jogging, but those seconds are spurious precision for your 
use case, and take up space. So rewrite sec and hms:

#+begin_src emacs-lisp
   (defun sec (arg)
     (if (string-match org-timer-re arg)
         (org-timer-hms-to-secs arg)
       (org-timer-hms-to-secs (concat arg ":00"))))

   (defun hms (arg)
     (if (integerp (/ arg 60))
         (substring (org-timer-secs-to-hms arg) 0 -3)
       (org-timer-secs-to-hms arg)))
#+end_src

Now sec will correctly convert hh:mm stamps, too, and hms will convert 
to hh:mm format if the argument is in whole minutes, otherwise to 
hh:mm:ss. So:

| Start | Lunch |  Back |   End |  Sum |
|-------+-------+-------+-------+------|
| 08:00 | 12:20 | 13:00 | 17:00 | 8:20 |
#+TBLFM: $5='(hms (+ (- (sec $4) (sec $3)) (- (sec $2) (sec $1))))


Yours,
Christian


On 3/15/11 8:49 PM, Martin Halder wrote:
>
>>> I was trying to generate a simple table with time format "hh:mm" and
>>> auto calculate daily sum.. clocking working time was too much so I
>>> thought this would be easy but ended up with the following.. it works
>>> but is not beautiful (apply formula twice and same information
>>> multiple times) and I would like to get rid of the "hms", "hh" and
>>> "mm" columns and therefore call "hmconcat" directly somehow.. Any help
>>> is highly appreciated..
>>>
>>> Thanks,
>>> Martin
>>>
>>> | Date             | Start | Lunch |  Back |   End |   Sum | hms       | hh | mm |
>>> |------------------+-------+-------+-------+-------+-------+-----------+----+----|
>>> | [2011-03-01 Tue] | 08:00 | 12:20 | 13:00 | 17:00 | 08:20 | 8@ 20' 0" |  8 | 20 |
>>> #+TBLFM: $6='(hmconcat $8 $9)::$7=time(<2010-01-01 $5>)-time(<2010-01-01 $4>)+time(<2010-01-01 $3>)-time(<2010-01-01 $2>)::$8=hour($7)::$9=minute($7)
>>>
>>> (defun hmconcat (hh mm) (interactive)
>>>     (if (>  (length hh) 1)
>>>         (setq temp (concat hh ":")) (setq temp (concat "0" hh ":")))
>>>     (if (>  (length mm) 1)
>>>         (concat temp mm) (concat temp "0" mm)))
>>
>> Martin,
>>
>> glad to see you got further with this!
>>
>> You can definitely get rid of hmconcat by using a combination of
>> string-to-number and format (and I'm sure it's possible to get this done
>> with simpler elisp):
>>
>> --8<---------------cut here---------------start------------->8---
>>
>> | Date             | Start | Lunch |  Back |   End |   Sum | hms       | hh | mm |
>> |------------------+-------+-------+-------+-------+-------+-----------+----+----|
>> | [2011-03-01 Tue] | 08:00 | 12:20 | 13:00 | 17:00 | 08:20 | 8@ 20' 0" |  8 | 20 |
>> #+TBLFM: $6='(format "%02d:%02d" (string-to-number $8) (string-to-number $9))::$7=time(<2010-01-01 $5>)-time(<2010-01-01 $4>)+time(<2010-01-01 $3>)-time(<2010-01-01 $2>)::$8=hour($7)::$9=minute($7)
>> --8<---------------cut here---------------end--------------->8---
>
> Hi Eric,
>
> yes and thanks for the previous help, too.. the good old printf.. I would like to pass the result of time() directly to a lisp function, like:
> #+TBLFM: $6='(coolfunc (time(...$5)-time(...$4)))
>
> If I would know how to pass the result, eg as a string, to a lisp function I could sort it out, I guess.
>
> Thanks,
> Martin
>

  parent reply	other threads:[~2011-03-15 21:45 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-15 18:32 org table calc and lisp for hh:mm timetable Martin Halder
2011-03-15 19:22 ` Eric S Fraga
2011-03-15 19:49   ` Martin Halder
2011-03-15 20:37     ` Eric S Fraga
2011-03-15 21:47     ` Christian Moe [this message]
2011-03-16  9:22       ` Martin Halder
2011-03-17  7:49         ` Bastien
2011-03-20 17:50           ` Eric Schulte
2011-03-20 19:57             ` Eric S Fraga
2011-03-20 17:50           ` Eric Schulte
2011-03-20 21:00             ` Christian Moe
2011-03-20 23:43               ` Eric Schulte
2011-03-22  4:40                 ` Eric Schulte
2011-03-22  9:36                   ` Christian Moe
2011-03-24  1:18                     ` Eric Schulte
2011-03-24 18:35                       ` Martin Halder
2011-03-22 10:52                   ` Carsten Dominik
2011-07-02 11:38                     ` Bastien
2011-03-16  9:28       ` Eric S Fraga

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4D7FDE8C.1030103@christianmoe.com \
    --to=mail@christianmoe.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=martin.halder@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).