emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Karl Voit <devnull@Karl-Voit.at>
To: emacs-orgmode@gnu.org
Subject: Re: Calc: Multiply time (hours) with a float
Date: Sun, 18 Sep 2016 18:36:19 +0200	[thread overview]
Message-ID: <2016-09-18T18-32-24@devnull.Karl-Voit.at> (raw)
In-Reply-To: 87oa3u1vpn.fsf@luisa.c0t0d0s0.de

* Michael Welle <mwe012008@gmx.net> wrote:
> Hello,

Hello Michael,

> Karl Voit <devnull@Karl-Voit.at> writes:
>
>> * Karl Voit <devnull@Karl-Voit.at> wrote:
>>> Hi!
>>>
>>> I do have following situation: I'd like to multiply a float with hours
>>> which results in an error, obviously. However, I was not able to find
>>> out how to do it.
>>>
>>> Minimal example with expected product:
>>> 
>>> | time [h:m:s] |   value | h * value |
>>> |--------------+---------+-----------|
>>> |     09:15:00 | 2.54321 | 23.524693 |
>>>  #+TBLFM: @2$3=$2*$1
>>
> do you really need the result to be in decimal notation? 

If I want to implement my original idea (see my original posting):
yes.

> If not, 
> #+TBLFM:@2$3=$2*$1;T would work. If you need decimal notation you have
> to provide your own function, I think.

So I wrote some Elisp code:

  (defun org-time-string-to-seconds (s)
    "Convert a string HH:MM:SS to a number of seconds.
     Omitted third element will be interpreted as MM:SS with missing hours."
    ;; test with:
    ;; (message (concat "result is: " (number-to-string (org-time-string-to-seconds "57:45:03"))))
    ;; (message (concat "result is: " (number-to-string (org-time-string-to-seconds "57:45"))))
    (cond
     ((and (stringp s)
       (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)" s))
      (let ((hour (string-to-number (match-string 1 s)))
        (min (string-to-number (match-string 2 s)))
        (sec (string-to-number (match-string 3 s))))
    (+ (* hour 3600) (* min 60) sec)))
     ((and (stringp s)
       (string-match "\\([0-9]+\\):\\([0-9]+\\)" s))
      (let ((min (string-to-number (match-string 1 s)))
        (sec (string-to-number (match-string 2 s))))
    (+ (* min 60) sec)))
     ;;((stringp s) (string-to-number s))
     ;;(t s)
     )
    )

  (defun org-time-string-to-hours (s)
    "Convert a string HH:MM:SS to hours (float).
     When only two values given, they will be interpreted as MM:SS with missing hours."
    ;; test via:
    ;; (message (concat "result is: " (number-to-string (org-time-string-to-hours "57:45:03"))))
    ;; (message (concat "result is: " (number-to-string (org-time-string-to-hours "57:45"))))
    (/ (org-time-string-to-seconds s) 3600.0)
    )

... which is working:

| time [h:m:s] |   value | hours [float] |   product |
|--------------+---------+---------------+-----------|
|     09:15:00 | 2.54321 |          9.25 | 23.524693 |
#+TBLFM: $4=$2*$3::@2$3='(org-time-string-to-hours $1)

However, when I try to calculate the product in one step, I get an error:

| time [h:m:s] |   value | product |
|--------------+---------+---------|
|     09:15:00 | 2.54321 | #ERROR  |
#+TBLFM: @2$3='(* $2 (org-time-string-to-hours $1))

When I replace "$2" in TBLFM with an integer value, it works.

How can I calculate the product in /one/ step?

Thanks again!

-- 
mail|git|SVN|photos|postings|SMS|phonecalls|RSS|CSV|XML to Org-mode:
       > get Memacs from https://github.com/novoid/Memacs <

https://github.com/novoid/extract_pdf_annotations_to_orgmode + more on github

  reply	other threads:[~2016-09-18 16:36 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-08  8:33 Calc: Multiply time (hours) with a float Karl Voit
2016-09-11 11:45 ` Karl Voit
2016-09-11 12:12   ` Michael Welle
2016-09-18 16:36     ` Karl Voit [this message]
2016-09-19  6:49       ` Michael Welle
2016-09-19  6:55         ` Michael Welle
2016-09-19 11:43           ` Karl Voit
2016-09-19 12:41             ` Michael Welle
2016-09-19 13:18               ` Karl Voit
2016-09-19 13:49                 ` Michael Welle
2016-09-19 15:33                   ` Nick Dokos
2016-09-19 17:45                     ` Michael Welle
2016-09-20 14:17                       ` Karl Voit
2016-09-19 15:33                 ` Bernhard Pröll
2016-09-20 14:23                 ` Calc: Multiply time (hours) with a float - solved by updating Org Karl Voit
2016-09-20 14:58                   ` Michael Welle

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=2016-09-18T18-32-24@devnull.Karl-Voit.at \
    --to=devnull@karl-voit.at \
    --cc=emacs-orgmode@gnu.org \
    --cc=news1142@Karl-Voit.at \
    /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).