diff options
| author | Lars Ingebrigtsen | 2020-08-21 00:38:23 +0200 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2020-08-21 00:38:23 +0200 |
| commit | cb9fc5e7731e506e4e0facd3d060d19e388b32ac (patch) | |
| tree | dfe4e0d9258238d5cbd62c6c74383692b921a035 | |
| parent | 9b277a2f8e5c22a05830e61d83876571b8eb56b4 (diff) | |
| download | emacs-cb9fc5e7731e506e4e0facd3d060d19e388b32ac.tar.gz emacs-cb9fc5e7731e506e4e0facd3d060d19e388b32ac.zip | |
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.
| -rw-r--r-- | lisp/calendar/time-date.el | 6 | ||||
| -rw-r--r-- | 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." | |||
| 401 | (when (decoded-time-year delta) | 401 | (when (decoded-time-year delta) |
| 402 | (cl-incf (decoded-time-year time) (decoded-time-year delta))) | 402 | (cl-incf (decoded-time-year time) (decoded-time-year delta))) |
| 403 | 403 | ||
| 404 | ;; Months are pretty simple. | 404 | ;; Months are pretty simple, but start at 1 (for January). |
| 405 | (when (decoded-time-month delta) | 405 | (when (decoded-time-month delta) |
| 406 | (let ((new (+ (decoded-time-month time) (decoded-time-month delta)))) | 406 | (let ((new (+ (1- (decoded-time-month time)) (decoded-time-month delta)))) |
| 407 | (setf (decoded-time-month time) (mod new 12)) | 407 | (setf (decoded-time-month time) (1+ (mod new 12))) |
| 408 | (cl-incf (decoded-time-year time) (/ new 12)))) | 408 | (cl-incf (decoded-time-year time) (/ new 12)))) |
| 409 | 409 | ||
| 410 | ;; Adjust for month length (as described in the doc string). | 410 | ;; 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 @@ | |||
| 123 | (should (equal (decoded-time-period '((135 . 10) 0 0 0 0 0 nil nil nil)) | 123 | (should (equal (decoded-time-period '((135 . 10) 0 0 0 0 0 nil nil nil)) |
| 124 | 13.5))) | 124 | 13.5))) |
| 125 | 125 | ||
| 126 | (ert-deftest test-time-wrap-addition () | ||
| 127 | (should (equal (decoded-time-add '(0 0 0 1 11 2008 nil nil nil) | ||
| 128 | (make-decoded-time :month 1)) | ||
| 129 | '(0 0 0 1 12 2008 nil nil nil))) | ||
| 130 | (should (equal (decoded-time-add '(0 0 0 1 12 2008 nil nil nil) | ||
| 131 | (make-decoded-time :month 1)) | ||
| 132 | '(0 0 0 1 1 2009 nil nil nil))) | ||
| 133 | (should (equal (decoded-time-add '(0 0 0 1 11 2008 nil nil nil) | ||
| 134 | (make-decoded-time :month 12)) | ||
| 135 | '(0 0 0 1 11 2009 nil nil nil))) | ||
| 136 | (should (equal (decoded-time-add '(0 0 0 1 11 2008 nil nil nil) | ||
| 137 | (make-decoded-time :month 13)) | ||
| 138 | '(0 0 0 1 12 2009 nil nil nil))) | ||
| 139 | (should (equal (decoded-time-add '(0 0 0 30 12 2008 nil nil nil) | ||
| 140 | (make-decoded-time :day 1)) | ||
| 141 | '(0 0 0 31 12 2008 nil nil nil))) | ||
| 142 | (should (equal (decoded-time-add '(0 0 0 30 12 2008 nil nil nil) | ||
| 143 | (make-decoded-time :day 2)) | ||
| 144 | '(0 0 0 1 1 2009 nil nil nil)))) | ||
| 145 | |||
| 126 | ;;; time-date-tests.el ends here | 146 | ;;; time-date-tests.el ends here |