[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master cb9fc5e: Fix off-by-one error in decoded-time-add (with months)
From: |
Lars Ingebrigtsen |
Subject: |
master cb9fc5e: Fix off-by-one error in decoded-time-add (with months) |
Date: |
Thu, 20 Aug 2020 18:38:42 -0400 (EDT) |
branch: master
commit cb9fc5e7731e506e4e0facd3d060d19e388b32ac
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>
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 125f9ac..638d8c1 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 fe1460c..233d43c 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
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- master cb9fc5e: Fix off-by-one error in decoded-time-add (with months),
Lars Ingebrigtsen <=