emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Max Nikulin <manikulin@gmail.com>
To: Ihor Radchenko <yantar92@gmail.com>
Cc: emacs-orgmode@gnu.org, Paul Eggert <eggert@cs.ucla.edu>
Subject: Re: [PATCH v4] org-encode-time compatibility and convenience helper
Date: Thu, 5 May 2022 22:22:21 +0700	[thread overview]
Message-ID: <2661a0c6-a6a3-6934-16fe-7c987a6a1684@gmail.com> (raw)
In-Reply-To: <8735hpk4ef.fsf@localhost>

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

On 04/05/2022 16:56, Ihor Radchenko wrote:
> Max Nikulin writes:
> 
> 1 unexpected results:
>     FAILED  test-org-clock/clocktable/ranges

Resetting timezone to UTC should be fixed in timestamps generated by a 
testing helper function. I was disappointed that `mapcar' can not be 
used with multiple lists, but I have found an old function created 
namely for zeroing nils in timestamps.

> Since it is expected to fail in some Emacs versions, you can just wrap
> the call into with-no-warnings:
> 
> (ignore-errors (with-no-warnings (encode-time '(0 0 0 1 1 1971))))

Thank you, I have added this trick.

I have created one more patch with tests quite close to the case caused 
this series.

It seems, no tests fail even in Australia/Eucla timezone famous for its 
45 min offset.

[-- Attachment #2: 0001-Use-unknown-DST-instead-of-standard-time-in-timestam.patch --]
[-- Type: text/x-patch, Size: 2862 bytes --]

From 485cd21366b651093c9c9c75e7398d662c99e92f Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Sat, 9 Apr 2022 00:17:09 -0700
Subject: [PATCH 1/7] Use unknown DST instead of standard time in timestamps

* lisp/ol.el (org-store-link): Prefer plain (encode-time ...)
to (apply 'encode-time ...), for speed.
* lisp/org-macs.el (org-parse-time-string): Return unknown DST,
not standard time.
* lisp/org.el (org-read-date-analyze): Return a timestamp with a DST
flag of -1 (unknown) rather than nil (standard time).

Max Nikulin:
A larger patch "Improve Org usage of timestamps" was suggested in
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=54764#10

Changes selected for this patch normalizes timestamp format:
if it is a list than it should contain 9 elements to be compatible
with Emacs-27 and Emacs-28 `encode-time' single argument, nil should not
be used for DST field since it means standard time while actual value
is unknown and should be guessed.

Ignacio Casso reported a problem with agenda
https://list.orgmode.org/PAXPR06MB7760238F410CBE3203F78EE0C61E9@PAXPR06MB7760.eurprd06.prod.outlook.com
due to Emacs commit dd0727e1ec1 changing Org code.  It was mostly reverted
by 8ef37913d3 (bug#54731).  Code in the Org repository did not have
the bug, but it safer to add protection against similar refactoring.
---
 lisp/ol.el       | 4 +---
 lisp/org-macs.el | 2 +-
 lisp/org.el      | 2 +-
 3 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/lisp/ol.el b/lisp/ol.el
index d4bd0e40c..0de9c921b 100644
--- a/lisp/ol.el
+++ b/lisp/ol.el
@@ -1618,9 +1618,7 @@ non-nil."
 	  (setq link
 		(format-time-string
 		 (car org-time-stamp-formats)
-		 (apply 'encode-time
-			(list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
-			      nil nil nil))))
+		 (encode-time 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd))))
 	  (org-link-store-props :type "calendar" :date cd)))
 
        ((eq major-mode 'w3-mode)
diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 8535bf2cd..241155064 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -1412,7 +1412,7 @@ This should be a lot faster than the `parse-time-string'."
 	(string-to-number (match-string 4 s))
 	(string-to-number (match-string 3 s))
 	(string-to-number (match-string 2 s))
-	nil nil nil))
+	nil -1 nil))
 
 (defun org-matcher-time (s)
   "Interpret a time comparison value S as a floating point time.
diff --git a/lisp/org.el b/lisp/org.el
index 1d5fc3903..165c83609 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -13663,7 +13663,7 @@ user."
 	 (setq year (nth 5 org-defdecode))
 	 (setq org-read-date-analyze-forced-year t))))
     (setq org-read-date-analyze-futurep futurep)
-    (list second minute hour day month year)))
+    (list second minute hour day month year nil -1 nil)))
 
 (defvar parse-time-weekdays)
 (defun org-read-date-get-relative (s today default)
-- 
2.25.1


[-- Attachment #3: 0002-Fix-tests-for-org-parse-time-string-and-org-clock.patch --]
[-- Type: text/x-patch, Size: 2570 bytes --]

From 3a6d7320be7f56949be968fa49e1fb6a7fcb0732 Mon Sep 17 00:00:00 2001
From: Max Nikulin <manikulin@gmail.com>
Date: Tue, 3 May 2022 17:59:11 +0700
Subject: [PATCH 2/7] Fix tests for `org-parse-time-string' and `org-clock'

* testing/lisp/test-org.el (ert-deftest test-org/org-parse-time-string):
Update test expectations to use DST of -1 (guess) after fix of
`org-parse-time-string'.
* testing/lisp/test-org-clock.el (org-test-clock-create-timestamp):
Do not change timezone from nil to 0.  Prevent test failures in zones
other than UTC.
---
 testing/lisp/test-org-clock.el |  6 +++---
 testing/lisp/test-org.el       | 10 +++++-----
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/testing/lisp/test-org-clock.el b/testing/lisp/test-org-clock.el
index d2179e1ec..89e0e2ade 100644
--- a/testing/lisp/test-org-clock.el
+++ b/testing/lisp/test-org-clock.el
@@ -27,9 +27,9 @@ Return the timestamp as a string."
   (org-element-interpret-data
    (let ((time (decode-time
                 (apply #'encode-time
-                       (mapcar (lambda (el) (or el 0))
-                               (org-read-date-analyze
-                                input nil (decode-time (current-time))))))))
+                       (org-fix-decoded-time
+                        (org-read-date-analyze
+                         input nil (decode-time (current-time))))))))
      (list 'timestamp
            (list :type (if inactive 'inactive 'active)
                  :minute-start (and with-time (nth 1 time))
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index ca0dc676b..4b5105a57 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -268,15 +268,15 @@
 (ert-deftest test-org/org-parse-time-string ()
   "Test `org-parse-time-string'."
   (should (equal (org-parse-time-string "2012-03-29 16:40")
-		 '(0 40 16 29 3 2012 nil nil nil)))
+		 '(0 40 16 29 3 2012 nil -1 nil)))
   (should (equal (org-parse-time-string "[2012-03-29 16:40]")
-		 '(0 40 16 29 3 2012 nil nil nil)))
+		 '(0 40 16 29 3 2012 nil -1 nil)))
   (should (equal (org-parse-time-string "<2012-03-29 16:40>")
-		 '(0 40 16 29 3 2012 nil nil nil)))
+		 '(0 40 16 29 3 2012 nil -1 nil)))
   (should (equal (org-parse-time-string "<2012-03-29>")
-		 '(0 0 0 29 3 2012 nil nil nil)))
+		 '(0 0 0 29 3 2012 nil -1 nil)))
   (should (equal (org-parse-time-string "<2012-03-29>" t)
-		 '(0 nil nil 29 3 2012 nil nil nil))))
+		 '(0 nil nil 29 3 2012 nil -1 nil))))
 
 (ert-deftest test-org/closest-date ()
   "Test `org-closest-date' specifications."
-- 
2.25.1


[-- Attachment #4: 0003-Use-higher-level-helpers-instead-of-encode-time.patch --]
[-- Type: text/x-patch, Size: 3019 bytes --]

From a582bd2efadbb88f79592efd31023c816a86a3ae Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Sat, 9 Apr 2022 00:17:09 -0700
Subject: [PATCH 3/7] Use higher level helpers instead of `encode-time'

* lisp/org-clock.el (org-clock-sum)
(org-clock-update-time-maybe):
Prefer org-time-string-to-seconds to doing it by hand.
* lisp/org-macs.el (org-2ft):
Prefer org-time-string-to-seconds to doing it by hand.
* lisp/org-table.el (org-table-eval-formula):
Prefer org-time-string-to-time to doing it by hand.

Max Nikulin:
A larger patch "Improve Org usage of timestamps" was suggested in
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=54764#10

Only cosmetic changes are selected for this patch.
---
 lisp/org-clock.el | 17 ++++++-----------
 lisp/org-macs.el  |  2 +-
 lisp/org-table.el |  3 +--
 3 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index ec87aaf8a..9f80dea04 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -1913,13 +1913,10 @@ PROPNAME lets you set a custom text property instead of :org-clock-minutes."
 	  (cond
 	   ((match-end 2)
 	    ;; Two time stamps.
-	    (let* ((ts (float-time
-			(apply #'encode-time
-			       (save-match-data
-				 (org-parse-time-string (match-string 2))))))
-		   (te (float-time
-			(apply #'encode-time
-			       (org-parse-time-string (match-string 3)))))
+	    (let* ((ss (match-string 2))
+		   (se (match-string 3))
+		   (ts (org-time-string-to-seconds ss))
+		   (te (org-time-string-to-seconds se))
 		   (dt (- (if tend (min te tend) te)
 			  (if tstart (max ts tstart) ts))))
 	      (when (> dt 0) (cl-incf t1 (floor dt 60)))))
@@ -3051,10 +3048,8 @@ Otherwise, return nil."
 	  (end-of-line 1)
 	  (setq ts (match-string 1)
 		te (match-string 3))
-	  (setq s (- (float-time
-		      (apply #'encode-time (org-parse-time-string te)))
-		     (float-time
-		      (apply #'encode-time (org-parse-time-string ts))))
+	  (setq s (- (org-time-string-to-seconds te)
+		     (org-time-string-to-seconds ts))
 		neg (< s 0)
 		s (abs s)
 		h (floor (/ s 3600))
diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 241155064..435c9ea30 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -1355,7 +1355,7 @@ nil, just return 0."
    ((numberp s) s)
    ((stringp s)
     (condition-case nil
-	(float-time (apply #'encode-time (org-parse-time-string s)))
+	(org-time-string-to-seconds s)
       (error 0)))
    (t 0)))
 
diff --git a/lisp/org-table.el b/lisp/org-table.el
index 2f31bef56..40a425197 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -2607,8 +2607,7 @@ location of point."
 		     (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))))))
+		      (save-match-data (org-time-string-to-time ts)))))
 		 form t t))
 
 	  (setq ev (if (and duration (string-match "^[0-9]+:[0-9]+\\(?::[0-9]+\\)?$" form))
-- 
2.25.1


[-- Attachment #5: 0004-testing-lisp-Use-org-time-string-to-time.patch --]
[-- Type: text/x-patch, Size: 7991 bytes --]

From 69c6fe5f3e8c640b5050e4374b4f9d421752e1b3 Mon Sep 17 00:00:00 2001
From: Max Nikulin <manikulin@gmail.com>
Date: Mon, 2 May 2022 17:41:34 +0700
Subject: [PATCH 4/7] testing/lisp: Use `org-time-string-to-time'

* testing/lisp/test-org.el (test-org/org-read-date, test-org/deadline)
(test-org/schedule, test-org/time-stamp, test-org/timestamp-from-time):
* testing/org-test.el (org-test-at-time): Use `org-time-string-to-time'
instead of composition of `org-parse-time-string' and `encode-time'.

The actual goal is to prepare to replace `encode-time' by
`org-encode-time' compatibility and convenience macro.
---
 testing/lisp/test-org.el | 44 ++++++++++++++++------------------------
 testing/org-test.el      |  2 +-
 2 files changed, 19 insertions(+), 27 deletions(-)

diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index 4b5105a57..8748cfa75 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -201,14 +201,14 @@
     (org-test-at-time "2014-03-04"
       (org-read-date
        t nil "+1y" nil
-       (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
+       (org-time-string-to-time "2012-03-29")))))
   (should
    (equal
     "2013-03-29"
     (org-test-at-time "2014-03-04"
       (org-read-date
        t nil "++1y" nil
-       (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
+       (org-time-string-to-time "2012-03-29")))))
   ;; When `org-read-date-prefer-future' is non-nil, prefer future
   ;; dates (relatively to now) when incomplete.  Otherwise, use
   ;; default date.
@@ -255,7 +255,7 @@
       (let ((org-read-date-prefer-future t))
 	(org-read-date
 	 t nil "1" nil
-	 (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
+	 (org-time-string-to-time "2012-03-29"))))))
   (should
    (equal
     "2014-03-25"
@@ -263,7 +263,7 @@
       (let ((org-read-date-prefer-future t))
 	(org-read-date
 	 t nil "25" nil
-	 (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
+	 (org-time-string-to-time "2012-03-29")))))))
 
 (ert-deftest test-org/org-parse-time-string ()
   "Test `org-parse-time-string'."
@@ -5443,8 +5443,7 @@ Paragraph<point>"
    (equal "* H\nDEADLINE: <2012-03-29 -705d>"
 	  (cl-letf (((symbol-function 'org-read-date)
 		     (lambda (&rest args)
-		       (apply #'encode-time
-			      (org-parse-time-string "2014-03-04")))))
+		       (org-time-string-to-time "2014-03-04"))))
 	    (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
 	      (let ((org-adapt-indentation nil)
 		    (org-last-inserted-timestamp nil))
@@ -5453,8 +5452,7 @@ Paragraph<point>"
   (should-error
    (cl-letf (((symbol-function 'org-read-date)
 	      (lambda (&rest args)
-		(apply #'encode-time
-		       (org-parse-time-string "2014-03-04")))))
+		(org-time-string-to-time "2014-03-04"))))
      (org-test-with-temp-text "* H"
        (let ((org-adapt-indentation nil)
 	     (org-last-inserted-timestamp nil))
@@ -5557,8 +5555,7 @@ Paragraph<point>"
    (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
 	  (cl-letf (((symbol-function 'org-read-date)
 		     (lambda (&rest args)
-		       (apply #'encode-time
-			      (org-parse-time-string "2014-03-04")))))
+		       (org-time-string-to-time "2014-03-04"))))
 	    (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
 	      (let ((org-adapt-indentation nil)
 		    (org-last-inserted-timestamp nil))
@@ -5567,8 +5564,7 @@ Paragraph<point>"
   (should-error
    (cl-letf (((symbol-function 'org-read-date)
 	      (lambda (&rest args)
-		(apply #'encode-time
-		       (org-parse-time-string "2014-03-04")))))
+		(org-time-string-to-time "2014-03-04"))))
      (org-test-with-temp-text "* H"
        (let ((org-adapt-indentation nil)
 	     (org-last-inserted-timestamp nil))
@@ -7786,7 +7782,7 @@ CLOSED: %s
     (org-test-with-temp-text "Te<point>xt"
       (cl-letf (((symbol-function 'org-read-date)
 		 (lambda (&rest args)
-		   (apply #'encode-time (org-parse-time-string "2014-03-04")))))
+		   (org-time-string-to-time "2014-03-04"))))
 	(org-time-stamp nil)
 	(buffer-string)))))
   ;; With a prefix argument, also insert time.
@@ -7796,8 +7792,7 @@ CLOSED: %s
     (org-test-with-temp-text "Te<point>xt"
       (cl-letf (((symbol-function 'org-read-date)
 		 (lambda (&rest args)
-		   (apply #'encode-time
-			  (org-parse-time-string "2014-03-04 00:41")))))
+		   (org-time-string-to-time "2014-03-04 00:41"))))
 	(org-time-stamp '(4))
 	(buffer-string)))))
   ;; With two universal prefix arguments, insert an active timestamp
@@ -7816,7 +7811,7 @@ CLOSED: %s
     (org-test-with-temp-text "Te<point>xt"
       (cl-letf (((symbol-function 'org-read-date)
 		 (lambda (&rest args)
-		   (apply #'encode-time (org-parse-time-string "2014-03-04")))))
+		   (org-time-string-to-time "2014-03-04"))))
 	(org-time-stamp nil t)
 	(buffer-string)))))
   ;; When called from a timestamp, replace existing one.
@@ -7826,7 +7821,7 @@ CLOSED: %s
     (org-test-with-temp-text "<2012-03-29<point> thu.>"
       (cl-letf (((symbol-function 'org-read-date)
 		 (lambda (&rest args)
-		   (apply #'encode-time (org-parse-time-string "2014-03-04")))))
+		   (org-time-string-to-time "2014-03-04"))))
 	(org-time-stamp nil)
 	(buffer-string)))))
   (should
@@ -7835,7 +7830,7 @@ CLOSED: %s
     (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
       (cl-letf (((symbol-function 'org-read-date)
 		 (lambda (&rest args)
-		   (apply #'encode-time (org-parse-time-string "2014-03-04")))))
+		   (org-time-string-to-time "2014-03-04"))))
 	(org-time-stamp nil)
 	(buffer-string)))))
   ;; When replacing a timestamp, preserve repeater, if any.
@@ -7845,7 +7840,7 @@ CLOSED: %s
     (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
       (cl-letf (((symbol-function 'org-read-date)
 		 (lambda (&rest args)
-		   (apply #'encode-time (org-parse-time-string "2014-03-04")))))
+		   (org-time-string-to-time "2014-03-04"))))
 	(org-time-stamp nil)
 	(buffer-string)))))
   ;; When called twice in a raw, build a date range.
@@ -7855,7 +7850,7 @@ CLOSED: %s
     (org-test-with-temp-text "<2012-03-29 thu.><point>"
       (cl-letf (((symbol-function 'org-read-date)
 		 (lambda (&rest args)
-		   (apply #'encode-time (org-parse-time-string "2014-03-04")))))
+		   (org-time-string-to-time "2014-03-04"))))
 	(let ((last-command 'org-time-stamp)
 	      (this-command 'org-time-stamp))
 	  (org-time-stamp nil))
@@ -8030,8 +8025,7 @@ CLOSED: %s
     "<2012-03-29 .+>"
     (org-element-interpret-data
      (org-timestamp-from-time
-      (apply #'encode-time
-	     (org-parse-time-string "<2012-03-29 Thu 16:40>"))))))
+      (org-time-string-to-time "<2012-03-29 Thu 16:40>")))))
   ;; When optional argument WITH-TIME is non-nil, provide time
   ;; information.
   (should
@@ -8039,8 +8033,7 @@ CLOSED: %s
     "<2012-03-29 .+ 16:40>"
     (org-element-interpret-data
      (org-timestamp-from-time
-      (apply #'encode-time
-	     (org-parse-time-string "<2012-03-29 Thu 16:40>"))
+      (org-time-string-to-time "<2012-03-29 Thu 16:40>")
       t))))
   ;; When optional argument INACTIVE is non-nil, return an inactive
   ;; timestamp.
@@ -8049,8 +8042,7 @@ CLOSED: %s
     "[2012-03-29 .+]"
     (org-element-interpret-data
      (org-timestamp-from-time
-      (apply #'encode-time
-	     (org-parse-time-string "<2012-03-29 Thu 16:40>"))
+      (org-time-string-to-time "<2012-03-29 Thu 16:40>")
       nil t)))))
 
 (ert-deftest test-org/timestamp-to-time ()
diff --git a/testing/org-test.el b/testing/org-test.el
index 0f1e254aa..d19c7f044 100644
--- a/testing/org-test.el
+++ b/testing/org-test.el
@@ -455,7 +455,7 @@ TIME can be a non-nil Lisp time value, or a string specifying a date and time."
 	(at (cl-gensym)))
     `(let* ((,tm ,time)
 	    (,at (if (stringp ,tm)
-		     (apply #'encode-time (org-parse-time-string ,tm))
+		     (org-time-string-to-time ,tm)
 		   ,tm)))
        (cl-letf
 	   ;; Wrap builtins whose behavior can depend on the current time.
-- 
2.25.1


[-- Attachment #6: 0005-org-macs.el-Introduce-a-helper-for-encode-time.patch --]
[-- Type: text/x-patch, Size: 6221 bytes --]

From da62b7543f0b74e016f314d67e774ae094d93034 Mon Sep 17 00:00:00 2001
From: Max Nikulin <manikulin@gmail.com>
Date: Fri, 8 Apr 2022 23:10:50 +0700
Subject: [PATCH 5/7] org-macs.el: Introduce a helper for `encode-time'

* lisp/org-macs.el (org-encode-time): New compatibility and convenience
helper macro to allow a list for time components or separate arguments
independently of Emacs version.
* testing/lisp/test-org.el (org-test-with-timezone): New macro to ensure
that some code is executed with certain TZ environment value and thus
particular daylight saving time or other time shift rules are active.
* testing/lisp/test-org.el (test-org/org-encode-time): Tests for various
ways to call `org-encode-time'.

Ensure recommended way to call `encode-time' for Emacs-27 and newer with
hope to avoid bugs due to attempts to modernize the code similar to
bug#54731.  6-elements list may be allowed as `encode-time' argument
since Emacs-29, see bug#54764.
---
 lisp/org-macs.el         | 35 ++++++++++++++++++++
 testing/lisp/test-org.el | 71 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+)

diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 435c9ea30..8b6e8f351 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -1389,6 +1389,41 @@ nil, just return 0."
 	(b (org-2ft b)))
     (and (> a 0) (> b 0) (\= a b))))
 
+(if (version< emacs-version "27.1")
+    (defmacro org-encode-time (&rest time)
+      (if (cdr time)
+          `(encode-time ,@time)
+        `(apply #'encode-time ,@time)))
+  (if (ignore-errors (with-no-warnings (encode-time '(0 0 0 1 1 1971))))
+      (defmacro org-encode-time (&rest time)
+        (pcase (length time) ; Emacs-29 since d75e2c12eb
+          (1 `(encode-time ,@time))
+          ((or 6 9) `(encode-time (list ,@time)))
+          (_ (error "`org-encode-time' may be called with 1, 6, or 9 arguments but %d given"
+                    (length time)))))
+    (defmacro org-encode-time (&rest time)
+      (pcase (length time)
+        (1 `(encode-time ,@time))
+        (6 `(encode-time (list ,@time nil -1 nil)))
+        (9 `(encode-time (list ,@time)))
+        (_ (error "`org-encode-time' may be called with 1, 6, or 9 arguments but %d given"
+                  (length time)))))))
+(put 'org-encode-time 'function-documentation
+     "Compatibility and convenience helper for `encode-time'.
+May be called with 9 components list (SECONDS ... YEAR IGNORED DST ZONE)
+as the recommended way since Emacs-27 or with 6 or 9 separate arguments
+similar to the only possible variant for Emacs-26 and earlier.
+6 elements list as the only argument causes wrong type argument till Emacs-29.
+
+Warning: use -1 for DST to guess the actual value, nil means no
+daylight saving time and may be wrong at particular time.
+
+DST value is ignored prior to Emacs-27.  Since Emacs-27 DST value matters
+even when multiple arguments is passed to this macro and such
+behavior is different from `encode-time'. See
+Info node `(elisp)Time Conversion' for details and caveats,
+preferably the latest version.")
+
 (defun org-parse-time-string (s &optional nodefault)
   "Parse Org time string S.
 
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index 8748cfa75..6e5be2299 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -24,6 +24,20 @@
 
 (eval-and-compile (require 'cl-lib))
 
+\f
+;;; Helpers
+
+(defmacro org-test-with-timezone (tz &rest body)
+  "Evaluate BODY with TZ environment temporary set to the passed value."
+  (declare (indent 1))
+  (org-with-gensyms (tz-saved)
+    `(let ((,tz-saved (getenv "TZ")))
+       (unwind-protect
+           (progn
+             (setenv "TZ" ,tz)
+             ,@body)
+             (setenv "TZ" ,tz-saved)))))
+
 \f
 ;;; Comments
 
@@ -179,6 +193,63 @@
 \f
 ;;; Date and time analysis
 
+(ert-deftest test-org/org-encode-time ()
+  "Test various ways to call `org-encode-time'"
+  (org-test-with-timezone "UTC"
+    ;; list as the sole argument
+    (should (string-equal
+             "2022-03-24 23:30:01"
+             (format-time-string
+              "%F %T"
+              (org-encode-time '(1 30 23 24 3 2022 nil -1 nil)))))
+    ;; SECOND...YEAR
+    (should (string-equal
+             "2022-03-24 23:30:02"
+             (format-time-string
+              "%F %T"
+              (org-encode-time 2 30 23 24 3 2022))))
+    ;; SECOND...YEAR IGNORED DST ZONE
+    (should (string-equal
+             "2022-03-24 23:30:03"
+             (format-time-string
+              "%F %T"
+              (org-encode-time 3 30 23 24 3 2022 nil -1 nil))))
+    ;; function call
+    (should (string-equal
+             "2022-03-24 23:30:04"
+             (format-time-string
+              "%F %T"
+              (org-encode-time (apply #'list 4 30 23 '(24 3 2022 nil -1 nil))))))
+    ;; wrong number of arguments
+    (if (not (version< emacs-version "27.1"))
+        (should-error (string-equal
+                       "2022-03-24 23:30:05"
+                       (format-time-string
+                        "%F %T"
+                        (org-encode-time 5 30 23 24 3 2022 nil))))))
+  ;; daylight saving time
+  (if (not (version< emacs-version "27.1"))
+      ;; DST value is not ignored for multiple arguments unlike for `encode-time'
+      (should (string-equal
+               "2022-04-01 00:30:06 +0200 CEST"
+               (format-time-string
+                "%F %T %z %Z"
+                (org-encode-time 6 30 23 31 3 2022 nil nil "Europe/Madrid")
+                "Europe/Madrid")))
+    (should (string-equal
+             "2022-03-31 23:30:07 +0200 CEST"
+             (format-time-string
+              "%F %T %z %Z"
+              (org-encode-time 7 30 23 31 3 2022 nil t "Europe/Madrid")
+              "Europe/Madrid"))))
+  (org-test-with-timezone "Europe/Madrid"
+    ;; Standard time is not forced when DST is not specified
+    (should (string-equal
+             "2022-03-31 23:30:08"
+             (format-time-string
+              "%F %T"
+              (org-encode-time 8 30 23 31 3 2022))))))
+
 (ert-deftest test-org/org-read-date ()
   "Test `org-read-date' specifications."
   ;; Parse ISO date with abbreviated year and month.
-- 
2.25.1


[-- Attachment #7: 0006-Use-org-encode-time-helper-macro.patch --]
[-- Type: text/x-patch, Size: 27826 bytes --]

From 72d5936d1021ce0f5f81ad6feb8153009b91ade0 Mon Sep 17 00:00:00 2001
From: Max Nikulin <manikulin@gmail.com>
Date: Mon, 2 May 2022 21:19:59 +0700
Subject: [PATCH 6/7] Use `org-encode-time' helper macro

* lisp/ol.el (org-store-link):
* lisp/org-agenda.el (org-agenda-get-timestamps)
(org-agenda-get-progress, agenda-bulk-action):
* lisp/org-capture.el (org-capture-fill-template):
* lisp/org-clock.el (org-clock-get-sum-start)
(org-clock-special-range, org-clocktable-shift)
(org-clocktable-steps):
* lisp/org-colview.el (org-colview-construct-allowed-dates):
* lisp/org-datetree.el (org-datetree-find-iso-week-create)
(org-datetree-insert-line):
* lisp/org-element.el (org-element-timestamp-interpreter):
* lisp/org-macro.el (org-macro--vc-modified-time):
* lisp/org-macs.el (org-matcher-time):
* lisp/org.el (org-current-time, org-current-effective-time)
(org-add-planning-info, org-read-date, org-read-date-display)
(org-read-date-analyze, org-eval-in-calendar)
(org-calendar-select, org-display-custom-time)
(org-calendar-select-mouse, org-time-string-to-time)
(org-time-from-absolute, org-at-clock-log-p)
(org-date-from-calendar, org-get-cursor-date)
(org-timestamp-to-time):
* testing/lisp/test-org-clock.el (org-test-clock-create-timestamp):
* lisp/ox-icalendar.el (org-icalendar-convert-timestamp):
Avoid direct calls of `encode-time', use `org-encode-time' instead.

Org supports Emacs-26, but the recommended way to call `encode-time'
changed in Emacs-27.  In Emacs-29 DST and TZ elements of the single list
arguments became optional.  In Org it is still convenient to call the
function with separate arguments without explicit DST and TZ arguments.
The `org-encode-time' should mitigate attempts to modernize Org code
directly in the Emacs repository.
---
 lisp/ol.el                     |  2 +-
 lisp/org-agenda.el             |  8 ++--
 lisp/org-capture.el            |  9 ++--
 lisp/org-clock.el              | 57 ++++++++++++------------
 lisp/org-colview.el            |  2 +-
 lisp/org-datetree.el           |  8 ++--
 lisp/org-element.el            | 27 ++++++------
 lisp/org-macro.el              |  2 +-
 lisp/org-macs.el               |  4 +-
 lisp/org.el                    | 79 ++++++++++++++++++----------------
 lisp/ox-icalendar.el           |  2 +-
 testing/lisp/test-org-clock.el |  8 ++--
 12 files changed, 108 insertions(+), 100 deletions(-)

diff --git a/lisp/ol.el b/lisp/ol.el
index 0de9c921b..3c3c71170 100644
--- a/lisp/ol.el
+++ b/lisp/ol.el
@@ -1618,7 +1618,7 @@ non-nil."
 	  (setq link
 		(format-time-string
 		 (car org-time-stamp-formats)
-		 (encode-time 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd))))
+		 (org-encode-time 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd))))
 	  (org-link-store-props :type "calendar" :date cd)))
 
        ((eq major-mode 'w3-mode)
diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
index 0479a0e1f..afc2a1015 100644
--- a/lisp/org-agenda.el
+++ b/lisp/org-agenda.el
@@ -5740,7 +5740,7 @@ displayed in agenda view."
 	    (substring
 	     (format-time-string
 	      (car org-time-stamp-formats)
-	      (encode-time	; DATE bound by calendar
+	      (org-encode-time	; DATE bound by calendar
 	       0 0 0 (nth 1 date) (car date) (nth 2 date)))
 	     1 11))
 	   "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
@@ -6012,7 +6012,7 @@ then those holidays will be skipped."
 		   (substring
 		    (format-time-string
 		     (car org-time-stamp-formats)
-		     (encode-time  ; DATE bound by calendar
+		     (org-encode-time  ; DATE bound by calendar
 		      0 0 0 (nth 1 date) (car date) (nth 2 date)))
 		    1 11))))
 	 (org-agenda-search-headline-for-time nil)
@@ -11077,8 +11077,8 @@ The prefix arg is passed through to the command if possible."
 		     (ignore-errors
 		       (let* ((date (calendar-gregorian-from-absolute
 				     (+ (org-today) distance)))
-			      (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
-						 (nth 2 date))))
+			      (time (org-encode-time
+                                     0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
 			 (org-agenda-schedule nil time))))))))
 
 	(?f
diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 068e3eda2..eb45f7ef5 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -1050,9 +1050,10 @@ Store them in the capture property list."
                       prompt-time
                     ;; Use 00:00 when no time is given for another
                     ;; date than today?
-                    (apply #'encode-time 0 0
-                           org-extend-today-until
-                           (cl-cdddr (decode-time prompt-time)))))
+                    (org-encode-time
+                     (apply #'list
+                            0 0 org-extend-today-until
+                            (cl-cdddr (decode-time prompt-time))))))
 		 (time-to-days prompt-time)))
 	      (t
 	       ;; Current date, possibly corrected for late night
@@ -1579,7 +1580,7 @@ by their respective `org-store-link-plist' properties if present."
 	 (time (let* ((c (or (org-capture-get :default-time) (current-time)))
 		      (d (decode-time c)))
 		 (if (< (nth 2 d) org-extend-today-until)
-		     (encode-time 0 59 23 (1- (nth 3 d)) (nth 4 d) (nth 5 d))
+		     (org-encode-time 0 59 23 (1- (nth 3 d)) (nth 4 d) (nth 5 d))
 		   c)))
 	 (v-t (format-time-string (org-time-stamp-format nil) time))
 	 (v-T (format-time-string (org-time-stamp-format t) time))
diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 9f80dea04..da5c310b7 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -1520,7 +1520,7 @@ The time is always returned as UTC."
 	     (day (nth 3 dt)))
 	(if (< hour org-extend-today-until) (setf (nth 3 dt) (1- day)))
 	(setf (nth 2 dt) org-extend-today-until)
-	(apply #'encode-time 0 0 (nthcdr 2 dt))))
+	(org-encode-time (apply #'list 0 0 (nthcdr 2 dt)))))
      ((or (equal cmt "all")
 	  (and (or (not cmt) (equal cmt "auto"))
 	       (not lr)))
@@ -2351,16 +2351,16 @@ have priority."
     (let* ((start (pcase key
 		    (`interactive (org-read-date nil t nil "Range start? "))
 		    (`untilnow nil)
-		    (_ (encode-time 0 m h d month y))))
+		    (_ (org-encode-time 0 m h d month y))))
 	   (end (pcase key
 		  (`interactive (org-read-date nil t nil "Range end? "))
 		  (`untilnow (current-time))
-		  (_ (encode-time 0
-				  m ;; (or m1 m)
-				  (or h1 h)
-				  (or d1 d)
-				  (or month1 month)
-				  (or y1 y)))))
+		  (_ (org-encode-time 0
+                                      m ;; (or m1 m)
+                                      (or h1 h)
+                                      (or d1 d)
+                                      (or month1 month)
+                                      (or y1 y)))))
 	   (text
 	    (pcase key
 	      ((or `day `today) (format-time-string "%A, %B %d, %Y" start))
@@ -2428,14 +2428,14 @@ the currently selected interval size."
 	  (cond
 	   (d (setq ins (format-time-string
 			 "%Y-%m-%d"
-			 (encode-time 0 0 0 (+ d n) nil y)))) ;; m
+			 (org-encode-time 0 0 0 (+ d n) nil y)))) ;; m
 	   ((and wp (string-match "w\\|W" wp) mw (> (length wp) 0))
 	    (require 'cal-iso)
 	    (setq date (calendar-gregorian-from-absolute
 			(calendar-iso-to-absolute (list (+ mw n) 1 y))))
 	    (setq ins (format-time-string
 		       "%G-W%V"
-		       (encode-time 0 0 0 (nth 1 date) (car date) (nth 2 date)))))
+		       (org-encode-time 0 0 0 (nth 1 date) (car date) (nth 2 date)))))
 	   ((and wp (string-match "q\\|Q" wp) mw (> (length wp) 0))
 	    (require 'cal-iso)
 					; if the 4th + 1 quarter is requested we flip to the 1st quarter of the next year
@@ -2452,11 +2452,11 @@ the currently selected interval size."
 			(calendar-iso-to-absolute (org-quarter-to-date (+ mw n) y))))
 	    (setq ins (format-time-string
 		       (concat (number-to-string y) "-Q" (number-to-string (+ mw n)))
-		       (encode-time 0 0 0 (nth 1 date) (car date) (nth 2 date)))))
+		       (org-encode-time 0 0 0 (nth 1 date) (car date) (nth 2 date)))))
 	   (mw
 	    (setq ins (format-time-string
 		       "%Y-%m"
-		       (encode-time 0 0 0 1 (+ mw n) y))))
+		       (org-encode-time 0 0 0 1 (+ mw n) y))))
 	   (y
 	    (setq ins (number-to-string (+ y n))))))
 	 (t (user-error "Cannot shift clocktable block")))
@@ -2844,7 +2844,7 @@ a number of clock tables."
           (pcase (if range (car range) (plist-get params :tstart))
             ((and (pred numberp) n)
              (pcase-let ((`(,m ,d ,y) (calendar-gregorian-from-absolute n)))
-               (apply #'encode-time (list 0 0 org-extend-today-until d m y))))
+               (org-encode-time 0 0 org-extend-today-until d m y)))
             (timestamp
 	     (seconds-to-time
 	      (org-matcher-time (or timestamp
@@ -2854,7 +2854,7 @@ a number of clock tables."
           (pcase (if range (nth 1 range) (plist-get params :tend))
             ((and (pred numberp) n)
              (pcase-let ((`(,m ,d ,y) (calendar-gregorian-from-absolute n)))
-               (apply #'encode-time (list 0 0 org-extend-today-until d m y))))
+               (org-encode-time 0 0 org-extend-today-until d m y)))
             (timestamp (seconds-to-time (org-matcher-time timestamp))))))
     (while (time-less-p start end)
       (unless (bolp) (insert "\n"))
@@ -2866,20 +2866,21 @@ a number of clock tables."
       ;; Compute NEXT, which is the end of the current clock table,
       ;; according to step.
       (let* ((next
-              (apply #'encode-time
-                     (pcase-let
-                         ((`(,_ ,_ ,_ ,d ,m ,y ,dow . ,_) (decode-time start)))
-                       (pcase step
-                         (`day (list 0 0 org-extend-today-until (1+ d) m y))
-                         (`week
-                          (let ((offset (if (= dow week-start) 7
-                                          (mod (- week-start dow) 7))))
-                            (list 0 0 org-extend-today-until (+ d offset) m y)))
-                         (`semimonth (list 0 0 0
-                                           (if (< d 16) 16 1)
-                                           (if (< d 16) m (1+ m)) y))
-                         (`month (list 0 0 0 month-start (1+ m) y))
-                         (`year (list 0 0 org-extend-today-until 1 1 (1+ y)))))))
+              ;; In Emacs-27 and Emacs-28 `encode-time' does not support 6 elements
+              ;; list argument so `org-encode-time' can not be outside of `pcase'.
+              (pcase-let
+                  ((`(,_ ,_ ,_ ,d ,m ,y ,dow . ,_) (decode-time start)))
+                (pcase step
+                  (`day (org-encode-time 0 0 org-extend-today-until (1+ d) m y))
+                  (`week
+                   (let ((offset (if (= dow week-start) 7
+                                   (mod (- week-start dow) 7))))
+                     (org-encode-time 0 0 org-extend-today-until (+ d offset) m y)))
+                  (`semimonth (org-encode-time 0 0 0
+                                               (if (< d 16) 16 1)
+                                               (if (< d 16) m (1+ m)) y))
+                  (`month (org-encode-time 0 0 0 month-start (1+ m) y))
+                  (`year (org-encode-time 0 0 org-extend-today-until 1 1 (1+ y))))))
              (table-begin (line-beginning-position 0))
 	     (step-time
               ;; Write clock table between START and NEXT.
diff --git a/lisp/org-colview.el b/lisp/org-colview.el
index c8443c135..c29a29d51 100644
--- a/lisp/org-colview.el
+++ b/lisp/org-colview.el
@@ -784,7 +784,7 @@ around it."
       (setq time-after (copy-sequence time))
       (setf (nth 3 time-before) (1- (nth 3 time)))
       (setf (nth 3 time-after) (1+ (nth 3 time)))
-      (mapcar (lambda (x) (format-time-string fmt (apply #'encode-time x)))
+      (mapcar (lambda (x) (format-time-string fmt (org-encode-time x)))
 	      (list time-before time time-after)))))
 
 (defun org-columns-open-link (&optional arg)
diff --git a/lisp/org-datetree.el b/lisp/org-datetree.el
index 30f5f99ae..f0c6bb7ba 100644
--- a/lisp/org-datetree.el
+++ b/lisp/org-datetree.el
@@ -137,7 +137,7 @@ will be built under the headline at point."
     (let* ((year (calendar-extract-year d))
 	   (month (calendar-extract-month d))
 	   (day (calendar-extract-day d))
-	   (time (encode-time 0 0 0 day month year))
+	   (time (org-encode-time 0 0 0 day month year))
 	   (iso-date (calendar-iso-from-absolute
 		      (calendar-absolute-from-gregorian d)))
 	   (weekyear (nth 2 iso-date))
@@ -197,14 +197,14 @@ inserted into the buffer."
     (when month
       (insert
        (if day
-	   (format-time-string "-%m-%d %A" (encode-time 0 0 0 day month year))
-	 (format-time-string "-%m %B" (encode-time 0 0 0 1 month year))))))
+	   (format-time-string "-%m-%d %A" (org-encode-time 0 0 0 day month year))
+	 (format-time-string "-%m %B" (org-encode-time 0 0 0 1 month year))))))
   (when (and day org-datetree-add-timestamp)
     (save-excursion
       (insert "\n")
       (org-indent-line)
       (org-insert-time-stamp
-       (encode-time 0 0 0 day month year)
+       (org-encode-time 0 0 0 day month year)
        nil
        (eq org-datetree-add-timestamp 'inactive))))
   (beginning-of-line))
diff --git a/lisp/org-element.el b/lisp/org-element.el
index 3856079aa..c40761657 100644
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -4041,12 +4041,12 @@ Assume point is at the beginning of the timestamp."
 				     (/= minute-start minute-end)))))
 	 (funcall
 	  build-ts-string
-	  (encode-time 0
-		       (or minute-start 0)
-		       (or hour-start 0)
-		       (org-element-property :day-start timestamp)
-		       (org-element-property :month-start timestamp)
-		       (org-element-property :year-start timestamp))
+	  (org-encode-time 0
+                           (or minute-start 0)
+                           (or hour-start 0)
+                           (org-element-property :day-start timestamp)
+                           (org-element-property :month-start timestamp)
+                           (org-element-property :year-start timestamp))
 	  (eq type 'active)
 	  (and hour-start minute-start)
 	  (and time-range-p hour-end)
@@ -4058,7 +4058,7 @@ Assume point is at the beginning of the timestamp."
 	     (hour-end (org-element-property :hour-end timestamp)))
 	 (concat
 	  (funcall
-	   build-ts-string (encode-time
+	   build-ts-string (org-encode-time
 			    0
 			    (or minute-start 0)
 			    (or hour-start 0)
@@ -4069,12 +4069,13 @@ Assume point is at the beginning of the timestamp."
 	   (and hour-start minute-start))
 	  "--"
 	  (funcall build-ts-string
-		   (encode-time 0
-				(or minute-end 0)
-				(or hour-end 0)
-				(org-element-property :day-end timestamp)
-				(org-element-property :month-end timestamp)
-				(org-element-property :year-end timestamp))
+		   (org-encode-time
+                    0
+                    (or minute-end 0)
+                    (or hour-end 0)
+                    (org-element-property :day-end timestamp)
+                    (org-element-property :month-end timestamp)
+                    (org-element-property :year-end timestamp))
 		   (eq type 'active-range)
 		   (and hour-end minute-end)))))
       (_ (org-element-property :raw-value timestamp)))))
diff --git a/lisp/org-macro.el b/lisp/org-macro.el
index 1d5cbe1b4..04e1ef36e 100644
--- a/lisp/org-macro.el
+++ b/lisp/org-macro.el
@@ -387,7 +387,7 @@ Return value as a string."
 				  (buffer-substring
 				   (point) (line-end-position)))))
 		       (when (cl-some #'identity time)
-			 (setq date (apply #'encode-time time))))))))
+			 (setq date (org-encode-time time))))))))
 	      (let ((proc (get-buffer-process buf)))
 		(while (and proc (accept-process-output proc .5 nil t)))))
 	  (kill-buffer buf))
diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 8b6e8f351..886a95bb2 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -1457,8 +1457,8 @@ following special strings: \"<now>\", \"<today>\",
 \"<tomorrow>\", and \"<yesterday>\".
 
 Return 0. if S is not recognized as a valid value."
-  (let ((today (float-time (apply #'encode-time
-				  (append '(0 0 0) (nthcdr 3 (decode-time)))))))
+  (let ((today (float-time (org-encode-time
+                            (append '(0 0 0) (nthcdr 3 (decode-time)))))))
     (save-match-data
       (cond
        ((string= s "<now>") (float-time))
diff --git a/lisp/org.el b/lisp/org.el
index 165c83609..3288bb7b8 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -4767,8 +4767,10 @@ the rounding returns a past time."
     (if (< r 1)
 	now
       (let* ((time (decode-time now))
-	     (res (apply #'encode-time 0 (* r (round (nth 1 time) r))
-			 (nthcdr 2 time))))
+	     (res (org-encode-time
+                   (apply #'list
+                          0 (* r (round (nth 1 time) r))
+                          (nthcdr 2 time)))))
 	(if (or (not past) (time-less-p res now))
 	    res
 	  (time-subtract res (* r 60)))))))
@@ -8778,7 +8780,7 @@ nil or a string to be used for the todo mark." )
 	   (org-use-last-clock-out-time-as-effective-time
 	    (or (org-clock-get-last-clock-out-time) ct))
 	   ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
-	    (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
+	    (org-encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
 	   (t ct))))
     ct1))
 
@@ -9931,9 +9933,9 @@ WHAT entry will also be removed."
 	        (if (stringp time)
 		    ;; This is a string (relative or absolute), set
 		    ;; proper date.
-		    (apply #'encode-time
-		           (org-read-date-analyze
-			    time default-time (decode-time default-time)))
+		    (org-encode-time
+                     (org-read-date-analyze
+                      time default-time (decode-time default-time)))
 	          ;; If necessary, get the time from the user
 	          (or time (org-read-date nil 'to-time nil
 				       (cl-case what
@@ -13315,7 +13317,7 @@ user."
     (when (< (nth 2 org-defdecode) org-extend-today-until)
       (setf (nth 2 org-defdecode) -1)
       (setf (nth 1 org-defdecode) 59)
-      (setq org-def (apply #'encode-time org-defdecode))
+      (setq org-def (org-encode-time org-defdecode))
       (setq org-defdecode (decode-time org-def)))
     (let* ((timestr (format-time-string
 		     (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d")
@@ -13388,7 +13390,7 @@ user."
 		 "range representable on this machine"))
       (ding))
 
-    (setq final (apply #'encode-time final))
+    (setq final (org-encode-time final))
 
     (setq org-read-date-final-answer ans)
 
@@ -13425,7 +13427,7 @@ user."
 			  (and (boundp 'org-time-was-given) org-time-was-given))
 		      (cdr fmts)
 		    (car fmts)))
-	     (txt (format-time-string fmt (apply #'encode-time f)))
+	     (txt (format-time-string fmt (org-encode-time f)))
 	     (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
 	     (txt (concat "=> " txt)))
 	(when (and org-end-time-was-given
@@ -13643,7 +13645,7 @@ user."
      ((and wday (not (nth 3 tl)))
       ;; Weekday was given, but no day, so pick that day in the week
       ;; on or after the derived date.
-      (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
+      (setq wday1 (nth 6 (decode-time (org-encode-time 0 0 0 day month year))))
       (unless (equal wday wday1)
 	(setq day (+ day (% (- wday wday1 -7) 7))))))
     (when (and (boundp 'org-time-was-given)
@@ -13658,7 +13660,7 @@ user."
 	  (when (> year 2037)
 	    (setq year 2037 org-read-date-analyze-forced-year t)))
       (condition-case nil
-	  (ignore (encode-time second minute hour day month year))
+	  (ignore (org-encode-time second minute hour day month year))
 	(error
 	 (setq year (nth 5 org-defdecode))
 	 (setq org-read-date-analyze-forced-year t))))
@@ -13723,7 +13725,7 @@ Unless KEEPDATE is non-nil, update `org-ans2' to the cursor date."
     (eval form t)
     (when (and (not keepdate) (calendar-cursor-to-date))
       (let* ((date (calendar-cursor-to-date))
-	     (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
+	     (time (org-encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
 	(setq org-ans2 (format-time-string "%Y-%m-%d" time))))
     (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
     (select-window sw)
@@ -13735,7 +13737,7 @@ This is used by `org-read-date' in a temporary keymap for the calendar buffer."
   (interactive)
   (when (calendar-cursor-to-date)
     (let* ((date (calendar-cursor-to-date))
-	   (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
+	   (time (org-encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
       (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
     (when (active-minibuffer-window) (exit-minibuffer))))
 
@@ -13800,7 +13802,7 @@ The command returns the inserted time stamp."
 	  time (org-fix-decoded-time t1)
 	  str (org-add-props
 		  (format-time-string
-		   (substring tf 1 -1) (apply 'encode-time time))
+		   (substring tf 1 -1) (org-encode-time time))
 		  nil 'mouse-face 'highlight))
     (put-text-property beg end 'display str)))
 
@@ -13854,7 +13856,7 @@ This is used by `org-read-date' in a temporary keymap for the calendar buffer."
   (mouse-set-point ev)
   (when (calendar-cursor-to-date)
     (let* ((date (calendar-cursor-to-date))
-	   (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
+	   (time (org-encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
       (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
     (when (active-minibuffer-window) (exit-minibuffer))))
 
@@ -14055,7 +14057,7 @@ days in order to avoid rounding problems."
 
 (defun org-time-string-to-time (s)
   "Convert timestamp string S into internal time."
-  (apply #'encode-time (org-parse-time-string s)))
+  (org-encode-time (org-parse-time-string s)))
 
 (defun org-time-string-to-seconds (s)
   "Convert a timestamp string S into a number of seconds."
@@ -14118,7 +14120,7 @@ into a past one.  Any year larger than 99 is returned unchanged."
   "Return the time corresponding to date D.
 D may be an absolute day number, or a calendar-type list (month day year)."
   (when (numberp d) (setq d (calendar-gregorian-from-absolute d)))
-  (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
+  (org-encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
 
 (defvar org-agenda-current-date)
 (defun org-calendar-holiday ()
@@ -14472,14 +14474,15 @@ When SUPPRESS-TMP-DELAY is non-nil, suppress delays like
 	  (setcar (cdr time0) (+ (nth 1 time0)
 				 (if (> n 0) (- rem) (- dm rem))))))
       (setq time
-	    (apply #'encode-time
-		   (or (car time0) 0)
-		   (+ (if (eq timestamp? 'minute) n 0) (nth 1 time0))
-		   (+ (if (eq timestamp? 'hour) n 0)   (nth 2 time0))
-		   (+ (if (eq timestamp? 'day) n 0)    (nth 3 time0))
-		   (+ (if (eq timestamp? 'month) n 0)  (nth 4 time0))
-		   (+ (if (eq timestamp? 'year) n 0)   (nth 5 time0))
-		   (nthcdr 6 time0)))
+	    (org-encode-time
+             (apply #'list
+                    (or (car time0) 0)
+                    (+ (if (eq timestamp? 'minute) n 0) (nth 1 time0))
+                    (+ (if (eq timestamp? 'hour) n 0)   (nth 2 time0))
+                    (+ (if (eq timestamp? 'day) n 0)    (nth 3 time0))
+                    (+ (if (eq timestamp? 'month) n 0)  (nth 4 time0))
+                    (+ (if (eq timestamp? 'year) n 0)   (nth 5 time0))
+                    (nthcdr 6 time0))))
       (when (and (memq timestamp? '(hour minute))
 		 extra
 		 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
@@ -14497,7 +14500,7 @@ When SUPPRESS-TMP-DELAY is non-nil, suppress delays like
 	  (setcar time0 (or (car time0) 0))
 	  (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
 	  (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
-	  (setq time (apply 'encode-time time0))))
+	  (setq time (org-encode-time time0))))
       ;; Insert the new time-stamp, and ensure point stays in the same
       ;; category as before (i.e. not after the last position in that
       ;; category).
@@ -14643,7 +14646,7 @@ If there is already a time stamp at the cursor position, update it."
       (org-timestamp-change 0 'calendar)
     (let ((cal-date (org-get-date-from-calendar)))
       (org-insert-time-stamp
-       (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
+       (org-encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
 
 (defcustom org-image-actual-width t
   "When non-nil, use the actual width of images when inlining them.
@@ -18204,14 +18207,14 @@ earliest time on the cursor date that Org treats as that date
     (cond
      ((eq major-mode 'calendar-mode)
       (setq date (calendar-cursor-to-date)
-	    defd (encode-time 0 (or mod 0) (or hod org-extend-today-until)
-			      (nth 1 date) (nth 0 date) (nth 2 date))))
+	    defd (org-encode-time 0 (or mod 0) (or hod org-extend-today-until)
+                                  (nth 1 date) (nth 0 date) (nth 2 date))))
      ((eq major-mode 'org-agenda-mode)
       (setq day (get-text-property (point) 'day))
       (when day
 	(setq date (calendar-gregorian-from-absolute day)
-	      defd (encode-time 0 (or mod 0) (or hod org-extend-today-until)
-				(nth 1 date) (nth 0 date) (nth 2 date))))))
+	      defd (org-encode-time 0 (or mod 0) (or hod org-extend-today-until)
+                                    (nth 1 date) (nth 0 date) (nth 2 date))))))
     (or defd (current-time))))
 
 (defun org-mark-subtree (&optional up)
@@ -19276,12 +19279,14 @@ return an active timestamp."
   "Convert TIMESTAMP object into an Emacs internal time value.
 Use end of date range or time range when END is non-nil.
 Otherwise, use its start."
-  (apply #'encode-time 0
-	 (mapcar
-	  (lambda (prop) (or (org-element-property prop timestamp) 0))
-	  (if end '(:minute-end :hour-end :day-end :month-end :year-end)
-	    '(:minute-start :hour-start :day-start :month-start
-			    :year-start)))))
+  (org-encode-time
+   (append '(0)
+           (mapcar
+            (lambda (prop) (or (org-element-property prop timestamp) 0))
+            (if end '(:minute-end :hour-end :day-end :month-end :year-end)
+              '(:minute-start :hour-start :day-start :month-start
+                              :year-start)))
+           '(nil -1 nil))))
 
 (defun org-timestamp-has-time-p (timestamp)
   "Non-nil when TIMESTAMP has a time specified."
diff --git a/lisp/ox-icalendar.el b/lisp/ox-icalendar.el
index abf31ce65..daac323cb 100644
--- a/lisp/ox-icalendar.el
+++ b/lisp/ox-icalendar.el
@@ -430,7 +430,7 @@ format (e.g. \"Europe/London\").  In either case, the value of
 					 t)))
       ;; Convert timestamp into internal time in order to use
       ;; `format-time-string' and fix any mistake (i.e. MI >= 60).
-      (encode-time 0 mi h d m y)
+      (org-encode-time 0 mi h d m y)
       (and (or (string-equal tz "UTC")
 	       (and (null tz)
 		    with-time-p
diff --git a/testing/lisp/test-org-clock.el b/testing/lisp/test-org-clock.el
index 89e0e2ade..14c9ec274 100644
--- a/testing/lisp/test-org-clock.el
+++ b/testing/lisp/test-org-clock.el
@@ -26,10 +26,10 @@ insert hours and minutes.
 Return the timestamp as a string."
   (org-element-interpret-data
    (let ((time (decode-time
-                (apply #'encode-time
-                       (org-fix-decoded-time
-                        (org-read-date-analyze
-                         input nil (decode-time (current-time))))))))
+                (org-encode-time
+                 (org-fix-decoded-time
+                  (org-read-date-analyze
+                   input nil (decode-time (current-time))))))))
      (list 'timestamp
            (list :type (if inactive 'inactive 'active)
                  :minute-start (and with-time (nth 1 time))
-- 
2.25.1


[-- Attachment #8: 0007-test-org.el-Add-some-tests-for-org-test-with-timezon.patch --]
[-- Type: text/x-patch, Size: 1672 bytes --]

From a66b15b87d06d087e4d82c226dd5f18a3c478af0 Mon Sep 17 00:00:00 2001
From: Max Nikulin <manikulin@gmail.com>
Date: Thu, 5 May 2022 21:54:03 +0700
Subject: [PATCH 7/7] test-org.el: Add some tests for `org-test-with-timezone'

* testing/lisp/test-org.el (test-org/org-time-string-to-time):
Check that no daylight saving time value is forced for
`org-parse-time-string' and `org-encode-time' calls.
---
 testing/lisp/test-org.el | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index 6e5be2299..f79d00c88 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -250,6 +250,26 @@
               "%F %T"
               (org-encode-time 8 30 23 31 3 2022))))))
 
+(ert-deftest test-org/org-time-string-to-time ()
+  "Test `org-time-string-to-time' around DST transition."
+  (org-test-with-timezone "UTC"
+    (should (string-equal
+             "2022-03-31 23:31:00"
+             (format-time-string
+              "%F %T"
+              (org-time-string-to-time "2022-03-31 23:31")))))
+  (org-test-with-timezone "Europe/Madrid"
+    (should (string-equal
+             "2022-03-24 23:32:00 +0100 CET"
+             (format-time-string
+              "%F %T %z %Z"
+              (org-time-string-to-time "2022-03-24 23:32"))))
+    (should (string-equal
+             "2022-03-31 23:33:00 +0200 CEST"
+             (format-time-string
+              "%F %T %z %Z"
+              (org-time-string-to-time "2022-03-31 23:33"))))))
+
 (ert-deftest test-org/org-read-date ()
   "Test `org-read-date' specifications."
   ;; Parse ISO date with abbreviated year and month.
-- 
2.25.1


  parent reply	other threads:[~2022-05-05 15:23 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-11 15:22 [DRAFT][PATCH] " Max Nikulin
2022-04-11 17:43 ` Paul Eggert
2022-04-23  8:25 ` Ihor Radchenko
2022-04-23 19:37   ` Paul Eggert
2022-04-24  3:35     ` Ihor Radchenko
2022-04-24 11:34   ` [DRAFT][PATCH v2] " Max Nikulin
2022-04-26  9:07     ` Ihor Radchenko
2022-05-03 12:14       ` [PATCH v3] " Max Nikulin
2022-05-04  9:56         ` Ihor Radchenko
2022-05-04 16:49           ` Max Nikulin
2022-05-05 15:22           ` Max Nikulin [this message]
2022-05-07  4:46             ` [PATCH v4] " Ihor Radchenko
2022-05-10 14:31             ` Max Nikulin
2022-05-11 13:20               ` Ihor Radchenko
2022-05-13 15:14                 ` Max Nikulin
2022-05-14  6:06                   ` Ihor Radchenko

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

  List information: https://www.orgmode.org/

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

  git send-email \
    --in-reply-to=2661a0c6-a6a3-6934-16fe-7c987a6a1684@gmail.com \
    --to=manikulin@gmail.com \
    --cc=eggert@cs.ucla.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@gmail.com \
    --subject='Re: [PATCH v4] org-encode-time compatibility and convenience helper' \
    /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

Code repositories for project(s) associated with this inbox:

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).