emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Eric Schulte" <schulte.eric@gmail.com>
To: mail@christianmoe.com
Cc: Bastien <bzg@altern.org>,
	emacs-orgmode@gnu.org, Martin Halder <martin.halder@gmail.com>
Subject: Re: org table calc and lisp for hh:mm timetable
Date: Sun, 20 Mar 2011 17:43:53 -0600	[thread overview]
Message-ID: <87fwqhl5mu.fsf@gmail.com> (raw)
In-Reply-To: <4D866AD7.4020701@christianmoe.com> (Christian Moe's message of "Sun, 20 Mar 2011 22:00:07 +0100")

Christian Moe <mail@christianmoe.com> writes:

> Hi,
>
> Returning to this thread:
>
> 1. I love Eric's macro wrapper idea -- now time arithmetic in tables
> gets truly manageable. If it's not included into Org-mode, it's a must
> for Worg!
>

Great, if no Org-mode changes result, then I will certainly post this
code up to Worg.

>
> 2. There's duplication with org-timer-hms-to-secs and
> org-timer-secs-to-hms. (Cf. my
> http://permalink.gmane.org/gmane.emacs.orgmode/39501.)
>

I believe these new functions are slightly more forgiving of alternate
time format strings, than the strict format expected by the org-timer-*
functions, however maybe it makes sense to consolidate these and the
org-timer-* functions around a set of core org-time functions.

>
> Do Martin's/Bastien's/Eric's hms/seconds conversion functions add
> value that should be patched into org-timer?
>

I think of these as separate from org-timer given that they mainly deal
with table formulas, however maybe both these and org-timer-* could
benefit from a centralized org-time-* functionality.

>
> 3. One thing Eric's converters do and org-timer doesn't is to handle a
> string with only two two-digit groups (e.g. 12:45).
>
> Eric's converters interpret it as 12m 45s -- good for running
> times. The functions I posted (see link above) interpreted such
> strings as 12h 45m -- good for time of day.
>
> I suggest the latter is more convenient for most use cases: When I'm
> working with seconds (running times, audio track durations) it's sort
> of a technical use, so I'm prepared to add 0 hours in front. When a
> time of day like 12:45 is good enough, I don't want to have to add 00
> seconds in back. (And am/pm is not used in my locale.)
>

I think the best would be for these functions (at least the table
formula functions) to remain agnostic as to the actual denomination of
the time, but rather just parse *:* as base sixty digits.  That way a
string like "1:20" could mean 80 minutes or 80 seconds, the parser needs
not know which, and the formula writer would /hopefully/ get back what's
expected.

While this topic is raised, would it make sense for Org-mode table
formula to automatically parse any time-like string into time units
(i.e., base sixty).  That would be the easiest for most users, and (I
imagine) would rarely result in surprising and unexpected behavior.

Best -- Eric

>
> Yours,
> Christian
>
> On 3/20/11 6:50 PM, Eric Schulte wrote:
>> I wrapped Bastien's functions below in a simple macro, which IMO results
>> in a very nice way to handle time values in Org-mode tables as shown
>> below.
>>
>> Note, the first argument to the `with-time' macro controls whether
>> results are returned as a time string or a numerical value.  That
>> argument may be followed by any number of expressions.
>>
>> |  time | miles | minutes/mile |
>> |-------+-------+--------------|
>> | 34:43 |   2.9 |        11:58 |
>> | 56:00 |   5.5 |        10:10 |
>> | 31:00 |  3.04 |        10:11 |
>> | 32:15 |  2.77 |        11:38 |
>> | 33:56 |   3.0 |        11:18 |
>> | 72:00 |  6.74 |        10:40 |
>> | 52:22 |  4.62 |        11:20 |
>> #+TBLFM: $3='(with-time t (/ $1 $2))
>>
>> #+begin_src emacs-lisp
>>    (defun org-time-string-to-seconds (s)
>>      "Convert a string HH:MM:SS to a number of seconds."
>>      (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-seconds-to-string (secs)
>>      "Convert a number of seconds to a time string."
>>      (cond ((>= secs 3600) (format-seconds "%h:%.2m:%.2s" secs))
>>            ((>= secs 60) (format-seconds "%m:%.2s" secs))
>>            (t (format-seconds "%s" secs))))
>>
>>    (defmacro with-time (time-output-p&rest exprs)
>>      "Evaluate an org-table formula, converting all fields that look
>>    like time data to integer seconds.  If TIME-OUTPUT-P then return
>>    the result as a time value."
>>      (list
>>       (if time-output-p 'org-time-seconds-to-string 'identity)
>>       (cons 'progn
>>             (mapcar
>>              (lambda (expr)
>>                `,(cons (car expr) (mapcar #'org-time-string-to-seconds (cdr expr))))
>>              `,@exprs))))
>> #+end_src
>>
>> Bastien<bzg@altern.org>  writes:
>>
>>> Hi Martin,
>>>
>>> Martin Halder<martin.halder@gmail.com>  writes:
>>>
>>>> this is fantastic, already love lisp, thanks a lot.. now I have exactly
>>>> what I wanted.. additionally I needed the time format in industrial mode
>>>> (1h = 100m = 100s), implemented in ihms.
>>>
>>> thanks for these functions -- I allowed myself to add them to
>>> Worg/org-hacks.html, in a new "Times computation" section:
>>>
>>>    http://orgmode.org/worg/org-hacks.html
>>>
>>> I added these functions I myself wrote for a particular purpose:
>>>
>>> #+begin_src emacs-lisp
>>> (defun org-hh:mm:ss-string-to-seconds (s)
>>>    "Convert a string HH:MM:SS to a number of seconds."
>>>    (when (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))))
>>>
>>> (defun org-subtract-hh:mm:ss-time (t1 t2)
>>>    "Substract two hh:mm:ss time values."
>>>    (let* ((sec (- (org-hh:mm:ss-string-to-seconds t2)
>>> 		 (org-hh:mm:ss-string-to-seconds t1)))
>>> 	 (hour (floor (/ sec 3600)))
>>> 	 (min (floor (/ (- sec (* 3600 hour)) 60)))
>>> 	 (secs (round (- sec (* 3600 hour) (* 60 min)))))
>>>      (format "%.2d:%.2d:%.2d" hour min secs)))
>>> #+end_src
>>>
>>> With these function, you can subtract durations in a table like this:
>>>
>>> | Part  |    Begin |      End | Duration |
>>> |-------+----------+----------+----------|
>>> | One   | 00:00:00 | 00:01:11 | 00:01:11 |
>>> | Two   | 00:01:12 | 00:02:00 | 00:00:48 |
>>> | Three | 00:02:05 | 00:16:06 | 00:14:01 |
>>> #+TBLFM: $4='(org-subtract-hh:mm:ss-time $2 $3)
>>>
>>> Which was useful for me when I had to derush video files.
>>>
>>> HTH,
>>
>>

  reply	other threads:[~2011-03-20 23:44 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
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 [this message]
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=87fwqhl5mu.fsf@gmail.com \
    --to=schulte.eric@gmail.com \
    --cc=bzg@altern.org \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@christianmoe.com \
    --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).