branch: externals/compat
commit f388ef2ba65fcd776c4051e291f8907603f6d11d
Author: Philip Kaludercic <phil...@posteo.net>
Commit: Philip Kaludercic <phil...@posteo.net>
    Add time-equal-p
---
 MANUAL          |  2 +-
 compat-27.el    | 18 ++++++++++++++++++
 compat-tests.el | 21 +++++++++++++++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/MANUAL b/MANUAL
index 328bd08562..a2397ba9c7 100644
--- a/MANUAL
+++ b/MANUAL
@@ -302,6 +302,7 @@ provided by Compat by default:
 - Function: decoded-time-dst :: Defined in ~simple.el~.
 - Function: decoded-time-zone :: Defined in ~simple.el~.
 - Function: package-get-version :: Defined in ~package.el~.
+- Function: time-equal-p :: See [[info:elisp#Time Calculations][(elisp) Time 
Calculations]].
 
 These functions are prefixed with ~compat~ prefix, and are only loaded
 when ~compat-27~ is required:
@@ -337,7 +338,6 @@ implemented in 27.1:
 - Bigint support.
 - The function ~time-convert~.
 - All ~iso8601-*~ functions.
-- The function ~time-equal-p~.
 - The function ~date-days-in-month~.
 - The macro ~benchmark-progn~.
 - The function ~read-char-from-minibuffer~.
diff --git a/compat-27.el b/compat-27.el
index 42f39a3ebe..acb7ffdbfa 100644
--- a/compat-27.el
+++ b/compat-27.el
@@ -265,6 +265,24 @@ represent a JSON false value.  It defaults to `:false'."
         (json-read))
     (json-error (signal 'json-parse-buffer err))))
 
+;;;; Defined in timefns.c
+
+(compat-defun time-equal-p (t1 t2)
+  "Return non-nil if time value T1 is equal to time value T2.
+A nil value for either argument stands for the current time."
+  :note "This function is not as accurate as the actual `time-equal-p'."
+  (cond
+   ((eq t1 t2))
+   ((and (consp t1) (consp t2))
+    (equal t1 t2))
+   ((let ((now (current-time)))
+      ;; Due to inaccuracies and the relatively slow evaluating of
+      ;; Emacs Lisp compared to C, we allow for slight inaccuracies
+      ;; (less than a millisecond) when comparing time values.
+      (< (abs (- (float-time (or t1 now))
+                 (float-time (or t2 now))))
+         1e-5)))))
+
 ;;;; Defined in subr.el
 
 (compat-defmacro setq-local (&rest pairs)
diff --git a/compat-tests.el b/compat-tests.el
index 777c7bb161..05b75bcb99 100644
--- a/compat-tests.el
+++ b/compat-tests.el
@@ -1632,5 +1632,26 @@ being compared against."
   (ought (expand-file-name "bar/.#b") "bar/b")
   (ought (expand-file-name "bar/.#foo") "bar/foo"))
 
+(compat-deftest time-equal-p
+  (ought t nil nil)
+  (ought t (current-time) nil)
+  (ought t nil (current-time))
+  ;; While `sleep-for' returns nil, indicating the current time, this
+  ;; behaviour seems to be undefined.  Relying on it is therefore not
+  ;; advised.
+  (ought nil (current-time) (ignore (sleep-for 0.01)))
+  (ought nil (current-time) (progn
+                              (sleep-for 0.01)
+                              (current-time)))
+  (ought t '(1 2 3 4) '(1 2 3 4))
+  (ought nil '(1 2 3 4) '(1 2 3 5))
+  (ought nil '(1 2 3 5) '(1 2 3 4))
+  (ought nil '(1 2 3 4) '(1 2 4 4))
+  (ought nil '(1 2 4 4) '(1 2 3 4))
+  (ought nil '(1 2 3 4) '(1 3 3 4))
+  (ought nil '(1 3 3 4) '(1 2 3 4))
+  (ought nil '(1 2 3 4) '(2 2 3 4))
+  (ought nil '(2 2 3 4) '(1 2 3 4)))
+
 (provide 'compat-tests)
 ;;; compat-tests.el ends here

Reply via email to