From cb9fc5e7731e506e4e0facd3d060d19e388b32ac Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Fri, 21 Aug 2020 00:38:23 +0200 Subject: [PATCH] Fix off-by-one error in decoded-time-add (with months) * lisp/calendar/time-date.el (decoded-time-add): Fix month addition, which was off-by-one. --- lisp/calendar/time-date.el | 6 +++--- test/lisp/calendar/time-date-tests.el | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lisp/calendar/time-date.el b/lisp/calendar/time-date.el index 125f9acc705..638d8c1f884 100644 --- a/lisp/calendar/time-date.el +++ b/lisp/calendar/time-date.el @@ -401,10 +401,10 @@ changes in daylight saving time are not taken into account." (when (decoded-time-year delta) (cl-incf (decoded-time-year time) (decoded-time-year delta))) - ;; Months are pretty simple. + ;; Months are pretty simple, but start at 1 (for January). (when (decoded-time-month delta) - (let ((new (+ (decoded-time-month time) (decoded-time-month delta)))) - (setf (decoded-time-month time) (mod new 12)) + (let ((new (+ (1- (decoded-time-month time)) (decoded-time-month delta)))) + (setf (decoded-time-month time) (1+ (mod new 12))) (cl-incf (decoded-time-year time) (/ new 12)))) ;; Adjust for month length (as described in the doc string). diff --git a/test/lisp/calendar/time-date-tests.el b/test/lisp/calendar/time-date-tests.el index fe1460cf29e..233d43cd01a 100644 --- a/test/lisp/calendar/time-date-tests.el +++ b/test/lisp/calendar/time-date-tests.el @@ -123,4 +123,24 @@ (should (equal (decoded-time-period '((135 . 10) 0 0 0 0 0 nil nil nil)) 13.5))) +(ert-deftest test-time-wrap-addition () + (should (equal (decoded-time-add '(0 0 0 1 11 2008 nil nil nil) + (make-decoded-time :month 1)) + '(0 0 0 1 12 2008 nil nil nil))) + (should (equal (decoded-time-add '(0 0 0 1 12 2008 nil nil nil) + (make-decoded-time :month 1)) + '(0 0 0 1 1 2009 nil nil nil))) + (should (equal (decoded-time-add '(0 0 0 1 11 2008 nil nil nil) + (make-decoded-time :month 12)) + '(0 0 0 1 11 2009 nil nil nil))) + (should (equal (decoded-time-add '(0 0 0 1 11 2008 nil nil nil) + (make-decoded-time :month 13)) + '(0 0 0 1 12 2009 nil nil nil))) + (should (equal (decoded-time-add '(0 0 0 30 12 2008 nil nil nil) + (make-decoded-time :day 1)) + '(0 0 0 31 12 2008 nil nil nil))) + (should (equal (decoded-time-add '(0 0 0 30 12 2008 nil nil nil) + (make-decoded-time :day 2)) + '(0 0 0 1 1 2009 nil nil nil)))) + ;;; time-date-tests.el ends here -- 2.39.2