Просмотр исходного кода

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>
Nicolas Goaziou 8 лет назад
Родитель
Сommit
47138a986e
2 измененных файлов с 35 добавлено и 12 удалено
  1. 16 12
      lisp/org-table.el
  2. 19 0
      testing/lisp/test-org-table.el

+ 16 - 12
lisp/org-table.el

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

+ 19 - 0
testing/lisp/test-org-table.el

@@ -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'."