From 76b416013ee4c9a492c8ddced57727215165c298 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 21 Mar 2011 19:43:19 -0600 Subject: [PATCH] org-table: convert times to integers on table formula evaluation * lisp/org-table.el (org-table-to-time): If cell contents look like a time string, then converts to an integer. (org-table-eval-formula): Convert times to integers on table formula evaluation. --- lisp/org-table.el | 26 ++++++++++++++++++++++---- 1 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lisp/org-table.el b/lisp/org-table.el index 3573032..3674b53 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -2273,6 +2273,21 @@ of the new mark." (cons var (cons value modes))) modes) +(defun org-table-to-time (s) + "Convert cell to numerical time if contents look like a time string." + (cond + ((and (stringp s) + (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)" s)) + (let ((hour (string-to-number (match-string 1 s))) + (min (string-to-number (match-string 2 s))) + (sec (string-to-number (match-string 3 s)))) + (+ (* hour 3600) (* min 60) sec))) + ((and (stringp s) + (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)) + (let ((min (string-to-number (match-string 1 s))) + (sec (string-to-number (match-string 2 s)))) + (+ (* min 60) sec))))) + (defun org-table-eval-formula (&optional arg equation suppress-align suppress-const suppress-store suppress-analysis) @@ -2369,10 +2384,13 @@ not overwrite the stored one." (setq formula (org-table-formula-substitute-names formula))) (setq orig (or (get-text-property 1 :orig-formula formula) "?")) (while (> ndown 0) - (setq fields (org-split-string - (org-no-properties - (buffer-substring (point-at-bol) (point-at-eol))) - " *| *")) + (setq fields (mapcar (lambda (cell) + (let ((time (org-table-to-time cell))) + (if time (number-to-string time) cell))) + (org-split-string + (org-no-properties + (buffer-substring (point-at-bol) (point-at-eol))) + " *| *"))) (if (eq numbers t) (setq fields (mapcar (lambda (x) (number-to-string (string-to-number x))) -- 1.7.1