all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Pierre-Henry Frohring <frohring.pierrehenry@gmail.com>
To: emacs-orgmode@gnu.org
Subject: [BUG] org-time-clock%
Date: Wed, 11 Sep 2013 14:42:05 +0200	[thread overview]
Message-ID: <40DB3824-FBEA-4F93-81D7-CC0192AB8C35@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 4753 bytes --]

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 #2: test-org-clocktable.org --]
[-- Type: application/octet-stream, Size: 3264 bytes --]

#+BEGIN: clocktable :maxlevel 1 :scope file :formula %
#+CAPTION: Clock summary at [2013-09-11 Mer 14:19]
| 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:


* 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

** 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

                 reply	other threads:[~2013-09-11 12:42 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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=40DB3824-FBEA-4F93-81D7-CC0192AB8C35@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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.