emacs-orgmode
[Top][All Lists]
Advanced

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

[O] [BUG] org-time-clock%


From: Pierre-Henry Frohring
Subject: [O] [BUG] org-time-clock%
Date: Wed, 11 Sep 2013 14:42:05 +0200

Hi !

org-time-clock% gives me unexpected result.  
One could reproduce the bug using the code below or attached file.

configuration :
    emacs-version : GNU Emacs 24.3.1 (x86_64-apple-darwin, NS 
apple-appkit-1038.36) of 2013-03-13 on bob.porkrind.org
    org-version : Org-mode version 8.1.1

* results
** expected result
   #+BEGIN: clocktable :maxlevel 1 :scope file :formula %
   #+CAPTION: Clock summary at [2013-09-11 Mer 14:31]
   | Headline     | Time      |     % |
   |--------------+-----------+-------|
   | *Total time* | *4d 0:00* | 100.0 |
   |--------------+-----------+-------|
   | task_1       | 2d 0:00   |  50.0 |
   | task_2       | 1d 0:00   |  25.0 |
   | task_3       | 1d 0:00   |  25.0 |
   #+TBLFM: $3='(org-clock-time% @2$2 $2..$2);%.1f
   #+END:

** result obtained
   #+BEGIN: clocktable :maxlevel 1 :scope file :formula %
   #+CAPTION: Clock summary at [2013-09-11 Mer 14:39]
   | Headline     | Time      |   % |
   |--------------+-----------+-----|
   | *Total time* | *4d 0:00* | 0.0 |
   |--------------+-----------+-----|
   | task_1       | 2d 0:00   | 0.0 |
   | task_2       | 1d 0:00   | 0.0 |
   | task_3       | 1d 0:00   | 0.0 |
   #+TBLFM: $3='(org-clock-time% @2$2 $2..$2);%.1f
   #+END:




=====================




#+BEGIN: clocktable :maxlevel 1 :scope file :formula %
#+CAPTION: Clock summary at [2013-09-11 Mer 14:31]
| Headline     | Time      |     % |
|--------------+-----------+-------|
| *Total time* | *4d 0:00* | 100.0 |
|--------------+-----------+-------|
| task_1       | 2d 0:00   |  50.0 |
| task_2       | 1d 0:00   |  25.0 |
| task_3       | 1d 0:00   |  25.0 |
#+TBLFM: $3='(org-clock-time% @2$2 $2..$2);%.1f
#+END:


* task_1
  CLOCK: [2013-09-09 Lun 14:16]--[2013-09-11 Mer 14:16] => 48:00

* task_2
  CLOCK: [2013-09-10 Mar 14:16]--[2013-09-11 Mer 14:16] => 24:00

* task_3
  CLOCK: [2013-09-10 Mar 14:16]--[2013-09-11 Mer 14:16] => 24:00

* org-clock-time%
** org-mode-8.1.1/lisp/org-clock.el

 #+BEGIN_SRC elisp

   (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))))

 #+END_SRC

 #+RESULTS:
 : org-clock-time%

** fixed/org-clock-time%
#+BEGIN_SRC elisp

  (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 (str-to-min
          (tot 0)
          s)

      ;; "([0-9]+d)? [0-9]+:[0-9]+" -> minutes
      (setq str-to-min
            (lambda (clock)
              (let ((re 
"\\(\\([0-9]+\\)d[[:space:]]\\)?\\([0-9]+\\):\\([0-9]+\\)"))
                (save-match-data
                  (catch 'exit
                    (if (not (string-match re clock))
                        ;; ill formatted => 0
                        (throw 'exit 0.)

                      (+ (string-to-number (match-string 4 clock))
                         (* 60 (string-to-number (match-string 3 clock)))
                         (* 60 24 (if (match-string 2 clock)
                                      (string-to-number (match-string 2 clock))
                                    0)))))))))

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

#+END_SRC

#+RESULTS:
: org-clock-time%

Attachment: test-org-clocktable.org
Description: Binary data


reply via email to

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