]> git.eshelyaron.com Git - emacs.git/commitdiff
Subtracting “now” from “now” should yield zero
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 17 Aug 2019 01:08:23 +0000 (18:08 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 17 Aug 2019 01:12:24 +0000 (18:12 -0700)
* src/timefns.c (time_arith): Arrange for (time-subtract nil
nil) to yield 0, to be consistent with (time-equal-p nil nil).
* test/lisp/calendar/time-date-tests.el (test-time-since): New test.

src/timefns.c
test/lisp/calendar/time-date-tests.el

index a4c1c4cb2842b0b0431c3311e2a47eb7f986972e..979550c84311672e446e32ec86fb130b6552f43b 100644 (file)
@@ -1040,7 +1040,16 @@ time_arith (Lisp_Object a, Lisp_Object b, bool subtract)
 
   enum timeform aform, bform;
   struct lisp_time ta = lisp_time_struct (a, &aform);
-  struct lisp_time tb = lisp_time_struct (b, &bform);
+
+  /* Subtract nil from nil correctly, and handle other eq values
+     quicker while we're at it.  Compare here rather than earlier, to
+     handle NaNs and check formats.  */
+  struct lisp_time tb;
+  if (EQ (a, b))
+    bform = aform, tb = ta;
+  else
+    tb = lisp_time_struct (b, &bform);
+
   Lisp_Object ticks, hz;
 
   if (FASTER_TIMEFNS && EQ (ta.hz, tb.hz))
@@ -1125,8 +1134,9 @@ time_cmp (Lisp_Object a, Lisp_Object b)
 
   struct lisp_time ta = lisp_time_struct (a, 0);
 
-  /* Compare nil to nil correctly, and other eq values while we're at it.
-     Compare here rather than earlier, to handle NaNs and check formats.  */
+  /* Compare nil to nil correctly, and handle other eq values quicker
+     while we're at it.  Compare here rather than earlier, to handle
+     NaNs and check formats.  */
   if (EQ (a, b))
     return 0;
 
index b46a247cd30c8f00719aa03d9399959b009b1d8b..827d2c9800a56c0ac98a7ab20d65ab7604f399ba 100644 (file)
     (should (equal (decoded-time-add time (mdec :zone -7200))
                    '(12 15 14 8 7 2019 1 t 7200)))))
 
-(require 'ert)
+(ert-deftest test-time-since ()
+  (should (time-equal-p 0 (time-since nil))))
 
 ;;; time-date-tests.el ends here