emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Calc: Multiply time (hours) with a float


From: Nick Dokos
Subject: Re: [O] Calc: Multiply time (hours) with a float
Date: Mon, 19 Sep 2016 11:33:41 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1.50 (gnu/linux)

Michael Welle <address@hidden> writes:

> Hello,
>
> Karl Voit <address@hidden> writes:
>
>> * Michael Welle <address@hidden> wrote:
>>>
>>> Karl Voit <address@hidden> writes:
>>>
>>>>> [...]
>>>>>> #+TBLFM: @2$3='(* $2 (org-time-string-to-hours "$1"));L
>>>>> or converting $2 to a number before the calculation should work also,
>>>>> but is more to write ;).
>>>>
>>>> Hm. I still got #ERROR. I reduced my issue to calculate the float (for 
>>>> hours)
>>>> without multiplying it with "value":
>>> strange. I use the current Org version from git and Emacs 25.1.
>>>
>>>> | time [h:m:s] |   value | product |
>>>> |--------------+---------+---------|
>>>> |     09:15:00 | 2.54321 | #ERROR  |
>>>>
>>>> #+TBLFM: @2$3='(org-time-string-to-hours $1)
>>> That works for me. The default interpretation of $1 is used and
>>> therefore the value is fed into o-t-s-t-h as string.
>>
>> OK, this is my current issue then.

FWIW, I used the above table with the definitions in an org file, C-C C-c'd the 
code,
and C-c C-c'd the table formula - I get no error:

--8<---------------cut here---------------start------------->8---
* table error
strange. I use the current Org version from git and Emacs 25.1.

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

* code

#+BEGIN_SRC elisp
  (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)
    )

#+END_SRC

#+RESULTS:
: org-time-string-to-hours

--8<---------------cut here---------------end--------------->8---

Org-mode version 8.3.6 (release_8.3.6-1135-g0ba465
GNU Emacs 25.1.50.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.20.9) of 
2016-08-31

-- 
Nick




reply via email to

[Prev in Thread] Current Thread [Next in Thread]