Skip to content

Instantly share code, notes, and snippets.

@Anmint
Last active June 30, 2020 07:59
Show Gist options
  • Save Anmint/56e8877e6d49ce463d44077cef60a194 to your computer and use it in GitHub Desktop.
Save Anmint/56e8877e6d49ce463d44077cef60a194 to your computer and use it in GitHub Desktop.
more precise percentage for clock table in org-mode
;; Copyright (C) 2017 by Anmint
;; Author: Anmint <anmint at outlook.com>
;; URL:
;; Version: 0.01
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This code modify behavior of org-clock-time% which calc worktime to percent.
;; Present clock table can show worktime as fraction by ":formula %" in header,
;; but this function cannot be operated correctly when time strings include day such as "1d 12:34".
;; This gist can do this calc even if string have information of day.
;;; Usage:
;; Copy to your init.el and then evaluate the file.
;; This code override to original code with nadvice.el, so you do not have to change your clock table.
;; If you want to stop this function, please evaluate advice-remove.
;;; Code
(defun org-clock-time%-mod (total &rest strings)
"Compute a time fraction in percent.
TOTAL is a time string like 1d 10:21 or only time (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 \\)?\\([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 4 total))
(* 60 (string-to-number (match-string 3 total)))
(* 1440 (string-to-number
(if (null (match-string 2 total))
"0"
(match-string 2 total))))))
(if (= tot 0.) (throw 'exit 0.)))
(while (setq s (pop strings))
(if (string-match re s)
(throw 'exit
(/ (* 100.0 (+ (string-to-number (match-string 4 s))
(* 60 (string-to-number (match-string 3 s)))
(* 1440 (string-to-number
(if (null (match-string 2 s))
"0"
(match-string 2 s))))))
tot))))
0))))
;; nadvice
(advice-add 'org-clock-time% :override #'org-clock-time%-mod)
;(advice-remove 'org-clock-time% '#org-clock-time%-mod)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment