From f6dd46cba8b144cf1653f8314a4b629beee11be3 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 16 Aug 2019 18:08:23 -0700 Subject: [PATCH] =?utf8?q?Subtracting=20=E2=80=9Cnow=E2=80=9D=20from=20?= =?utf8?q?=E2=80=9Cnow=E2=80=9D=20should=20yield=20zero?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * 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 | 16 +++++++++++++--- test/lisp/calendar/time-date-tests.el | 3 ++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/timefns.c b/src/timefns.c index a4c1c4cb284..979550c8431 100644 --- a/src/timefns.c +++ b/src/timefns.c @@ -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; diff --git a/test/lisp/calendar/time-date-tests.el b/test/lisp/calendar/time-date-tests.el index b46a247cd30..827d2c9800a 100644 --- a/test/lisp/calendar/time-date-tests.el +++ b/test/lisp/calendar/time-date-tests.el @@ -104,6 +104,7 @@ (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 -- 2.39.5