emacs-orgmode
[Top][All Lists]
Advanced

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

[O] org-clock-time% does not parse "1d 12:50". This is a fix. (org-mode


From: Pierre-Henry Frohring
Subject: [O] 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

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 via email to

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