emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Pierre-Henry Frohring <frohring.pierrehenry@gmail.com>
To: emacs-orgmode@gnu.org
Subject: org-clock-time% does not parse "1d 12:50". This is a fix. (org-mode 8.1.1)
Date: Mon, 9 Sep 2013 20:10:20 +0200	[thread overview]
Message-ID: <C9E08C21-C874-48AC-B4F1-B90B8D712840@gmail.com> (raw)

Dear all,

While I was experimenting with clock tables

  #+BEGIN: clocktable :maxlevel 2 :scope file :formula %

I found a surprising result : if *total time* is formatted as "1d 12:50" then
the percentages column gives wrong results.  

The reason is that org-clock-time% does not take "1d 12:50" format into account
but only "12:50" format.

You will find my fix of org-clock-time% below.


Thank you.
PH


my version
----


(defun org-clock-time% (total &rest strings)
  "Compute a time fraction in percent.
TOTAL is a time string like '1d 10:21' specifying the total times.
STRINGS is a list of strings that should be checked for a time.
The first string that does have a time will be used.
This function is made for clock tables."

  (let ((re "\\(\\([0-9]+\\)d[[:space:]]\\)?\\([0-9]+\\):\\([0-9]+\\)")
        clock-str-to-minutes
        (tot 0)
        s)

    ;; "00:10" -> 10
    ;; "5d 00:00" -> 7200
    (setq clock-str-to-minutes
          (lambda (clock)
            (let ((minutes 0)
                  (hours 0)
                  (days 0)
                  (tot 0))
              (save-match-data
                (catch 'exit
                  (if (not (string-match re clock))
                      ;; ill formatted => 0
                      (throw 'exit 0.)

                    ;; parse minutes, hours, days
                    (setq minutes (string-to-number (match-string 4 clock)))
                    (setq hours (string-to-number (match-string 3 clock)))
                    (if (match-string 2 clock)
                        (setq days (string-to-number (match-string 2 clock))))

                    ;; compute the time in minutes
                    (setq tot (+ minutes (* 60 hours) (* 60 24 days)))))))))

    ;; compute time fraction in percent
    (save-match-data
      (catch 'exit
        (setq tot (funcall clock-str-to-minutes total))
        (if (= tot 0.) (throw 'exit 0.))
        (while (setq s (pop strings))
          (throw 'exit
                 (/ (* 100.0 (funcall clock-str-to-minutes s))
                    tot)))
        0))))



original version : org-8.1.1/lisp/org-clock.el
----


(defun org-clock-time% (total &rest strings)
  "Compute a time fraction in percent.
TOTAL s a time string like 10:21 specifying the total times.
STRINGS is a list of strings that should be checked for a time.
The first string that does have a time will be used.
This function is made for clock tables."
  (let ((re "\\([0-9]+\\):\\([0-9]+\\)")
	tot s)
    (save-match-data
      (catch 'exit
	(if (not (string-match re total))
	    (throw 'exit 0.)
	  (setq tot (+ (string-to-number (match-string 2 total))
		       (* 60 (string-to-number (match-string 1 total)))))
	  (if (= tot 0.) (throw 'exit 0.)))
	(while (setq s (pop strings))
	  (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
	      (throw 'exit
		     (/ (* 100.0 (+ (string-to-number (match-string 2 s))
				    (* 60 (string-to-number
					   (match-string 1 s)))))
			tot))))
	0))))

                 reply	other threads:[~2013-09-09 18:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=C9E08C21-C874-48AC-B4F1-B90B8D712840@gmail.com \
    --to=frohring.pierrehenry@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /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).