org-table: Fix calculations with locale specific time-stamps

* lisp/org-table.el (org-table-eval-formula): Fix calculations with
  locale specific time-stamps.
* testing/lisp/test-org-table.el (test-org-table/time-stamps): New test.

Reported-by: "Ulrich J. Herter" <ujh@posteo.de>
<http://permalink.gmane.org/gmane.emacs.orgmode/108165>
This commit is contained in:
Nicolas Goaziou 2016-07-21 11:49:15 +02:00
parent 8fc25eb409
commit 47138a986e
2 changed files with 35 additions and 12 deletions

View File

@ -2847,18 +2847,22 @@ not overwrite the stored one. SUPPRESS-ANALYSIS prevents any call to
(string-to-number ev)
duration-output-format) ev))
;; Use <...> time-stamps so that Calc can handle them
(while (string-match (concat "\\[" org-ts-regexp1 "\\]") form)
(setq form (replace-match "<\\1>" nil nil form)))
;; I18n-ize local time-stamps by setting (system-time-locale "C")
(when (string-match org-ts-regexp2 form)
(let* ((ts (match-string 0 form))
(tsp (apply 'encode-time (save-match-data (org-parse-time-string ts))))
(system-time-locale "C")
(tf (or (and (save-match-data (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts))
(cdr org-time-stamp-formats))
(car org-time-stamp-formats))))
(setq form (replace-match (format-time-string tf tsp) t t form))))
;; Use <...> time-stamps so that Calc can handle them.
(setq form
(replace-regexp-in-string org-ts-regexp-inactive "<\\1>" form))
;; Internationalize local time-stamps by setting locale to
;; "C".
(setq form
(replace-regexp-in-string
org-ts-regexp
(lambda (ts)
(let ((system-time-locale "C"))
(format-time-string
(org-time-stamp-format
(string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts))
(apply #'encode-time
(save-match-data (org-parse-time-string ts))))))
form t t))
(setq ev (if (and duration (string-match "^[0-9]+:[0-9]+\\(?::[0-9]+\\)?$" form))
form

View File

@ -1923,6 +1923,25 @@ is t, then new columns should be added as needed"
(org-table-calc-current-TBLFM)
(buffer-string)))))
(ert-deftest test-org-table/time-stamps ()
"Test time-stamps handling."
;; Standard test.
(should
(string-match-p
"| 1 |"
(org-test-with-temp-text
"| <2016-07-07 Sun> | <2016-07-08 Fri> | |\n<point>#+TBLFM: $3=$2-$1"
(org-table-calc-current-TBLFM)
(buffer-string))))
;; Handle locale specific time-stamps.
(should
(string-match-p
"| 1 |"
(org-test-with-temp-text
"| <2016-07-07 Do> | <2016-07-08 Fr> | |\n<point>#+TBLFM: $3=$2-$1"
(org-table-calc-current-TBLFM)
(buffer-string)))))
(ert-deftest test-org-table/orgtbl-ascii-draw ()
"Test `orgtbl-ascii-draw'."